Topic: OT: Real help this time  (Read 2411 times)

0 Members and 1 Guest are viewing this topic.

Toasty0

  • Guest
OT: Real help this time
« on: January 23, 2004, 01:38:38 am »
I'm working my way through Petzold's Prtogramming Windows 5th edition and have run into a snag. I'm hoping one of you gurus might be able to explain what it is I'm doing that is a no-no.

first the code:
Code:
#include 

HDC GetPrintDC (void)
{
   DWORD         dwNeeded, dwReturned;
   HDC            hdc;
   PRINTER_INFO_4 * pinfo4;
   PRINTER_INFO_5 * pinfo5;

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

      pinfo5= malloc (dwNeeded);

      EnumPrinters (PRINTER_ENUM_DEFAULT, NULL, 5, (PBYTE) pinfo5, dwNeeded, &dwNeeded, &dwReturned);

      hdc=CreateDC (NULL, pinfo5->pPrinterName,NULL, NULL);

      free (pinfo5);
   }
   else
   {
      EnumPrinters (PRINTER_ENUM_LOCAL, NULL, 4, NULL, 0, &dwNeeded, &dwReturned);

      pinfo4= malloc (dwNeeded);

      EnumPrinters (PRINTER_ENUM_LOCAL, NULL, 4, (PBYTE) pinfo4, dwNeeded, &dwNeeded, &dwReturned);

      hdc= CreateDC (NULL, pinfo4->pPrinterName, NULL, NULL);

      free(pinfo4);
   }

   return hdc;
}



And now the error message:

Code:
--------------------Configuration: GETPRNDC - Win32 Debug--------------------
Compiling...
GETPRNDC.CPP
F:\Games\Fire\GETPRNDC\GETPRNDC.CPP(14) : error C2440: '=' : cannot convert from 'void *' to 'struct _PRINTER_INFO_5A *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
F:\Games\Fire\GETPRNDC\GETPRNDC.CPP(26) : error C2440: '=' : cannot convert from 'void *' to 'struct _PRINTER_INFO_4A *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
Error executing cl.exe.

GETPRNDC.OBJ - 2 error(s), 0 warning(s)



I hope no one minds me posting this here. I fugure it's my best chance of getting an explanation.

Thanks all.

Best,
Jerry  

NuclearWessels

  • Guest
Re: OT: Real help this time
« Reply #1 on: January 23, 2004, 09:40:17 am »
IIRC malloc essentially just returns a ptr to a memory location without identifying the type (i.e. returns it as a void*), then you need to specifyfor the compiler what type of ptr it's to be treated as, e.g.:

pinfo5 = (PRINTER_INFO_5 *)malloc(blah blah blah);

dave
   

Toasty0

  • Guest
Re: OT: Real help this time
« Reply #2 on: January 23, 2004, 10:23:58 am »
Thanks Dave. I had better get back to the basic and reveiw malloc again.

Best,
Jerry  

Toasty0

  • Guest
Re: OT: Real help this time
« Reply #3 on: January 26, 2004, 01:03:06 am »
Yikes.

It compiles just fine, but then I get a linker error.

LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/GETPRNDC.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.


Ouch...I think.

Best,
Jerry