Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question for Windows Programmers

Status
Not open for further replies.

MinnisotaFreezing

Programmer
Jun 21, 2001
120
KR
Hi all,
I am learning to program Windows using Charles Petzolds book Programming Windows Fifth Edition, and so far, so good. However, he writes all of his programs in C, and I want to use C++ functions and constructs, specifiacally, fstream functions. However, I cannot use these functions, as I get an error "eh.h is for C++ only!" I did some research, and it turns out VS compils .c files with its C compiler and .cpp files with its C++ compiler. So I figured I would just change the .c files to .cpp. Now things like this:

DWORD dwNeeded, dwReturned ;
HDC hdc ;
PRINTER_INFO_4 * pinfo4 ;
PRINTER_INFO_5 * pinfo5 ;

if (GetVersion () & 0x80000000) // Windows 98
{
EnumPrinters (PRINTER_ENUM_DEFAULT, NULL, 5, NULL,
0, &dwNeeded, &dwReturned) ;

pinfo5 = malloc (dwNeeded) ;

fails, because pinfo5 = malloc (dwNeeded)

cannot convert from void * to PRINTER_INFO_5 *, it requres an explicit cast. So even though the C++ mantra is "Anything written in C will compile in C++", it doesn't seem to work in this case, possibly a peculirarity of VS. Anyway, I changed "malloc (dwNeeded)" to "new (PRINTER_INFO_5)" and it now compiled, but I got a different error when I tried to print.

My question is, what do you Windows programmers use, C or C++, and for thoes of you who use C++, do you run into many compatability problems? I know this forum usually deals with specific code questions, and this is more general, but I like Windows programming, and at the same time want to use C++, I just don't know exactly how to go about it. Is there a way I can force VS to use its C++ compiler, even though the file is .c? Or do you all do it a different way?
 
try it:
pinfo5 = (PRINTER_INFO_5 *)malloc (dwNeeded) ;
or
pinfo5 = new PRINTER_INFO_5[(int)dwNeeded/sizeof(PRINTER_INFO_5)+1]; John Fill
1c.bmp


ivfmd@mail.md
 
Personally I use C++. The problems you are running into are related to the fact that the entire Win32 API is in C, not C++. JohnFill pointed out a couple of ways around your specific problem, so I won't bother with that. My overall solution is to write all of my code in C++, and hide any C specific items (such as malloc() if it's truly necessary) inside my class implementations where they can be isolated safely. Also, if you're using those structures inside functions, then you can (often, but perhaps not always) use automatic variables inside your functions rather than dynamically allocating memory. I suggest this if at all possible, simply because it removes the memory management burden, and if your functions are short lived, it won't tie up resources either.
Also - valid C code will compile in C++. However, C compilers will choke on valid C++ in more ways than I can quickly mention. Wrapping Win32 API calls inside of your C++ implementation should be relatively trivial, and solve most of your problems.
 
Thank you both very much.

Jon - I used your examples, and now the code works. Although I used the first example, and I suppose if I'm trying to write C++, I should use the second.

Thanks for the encurogment Stuck, I was getting frustrated today, and thinking I should just stick to C if that is what the API's were written in, but I'll try and go for C++. The thing I still don't quite understand is, the line
pinfo5 = malloc (dwNeeded) ; compiles under C, but not C++, can you shed any light on that?

CJB
 
function malloc returns void* alwais. In C conversion from void* to other types is no problem, but in C++ you must convert explicit the pointer type to (PRINTER_INFO_5 *) John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top