minigw compiling/linking problem using enumprinters function

J

Jason

Hi, I am new to windows api programming and I wanted to create a little
utility program for myself in c using the mini gw compiler. I bought
petzold's book, programming windows 5th edition. I know the book was
written for people using visual c++ 6, win98/NT before xp came along but
thought it would be ok. My problem is that having lifted an example of code
in the GetPrinterDC function(pasted below) the compiler seems to take
exception to the enumprinters function and gives the following message for
each instance of it.

printer.o(.text+0x6c):printer.c: undefined reference to `EnumPrintersA@28'

actually, it compiles ok, but the above message is at the linking stage i
think, having issued this command 'gcc -mwindows -o printer.exe printer.o'.
I included windows.h. can anybody help me on this?

the enumprinters function is described as filling an array of structures
with information on attached printers, allowing the function GetPrinterDC to
return a handle for the printer device context.

Is there a major flaw in my plan to use this book for creating GUI on this
compiler and xp assuming you know the book anyway? I want to learn windows
api stuff, though i find it frustrating that this function doesnt appear to
be known. Does it take a different set of arguments in xp?

Apart from this problem can anyone point me to further resources for doing
windows development on the minigw compiler. If there is a free GUI library
around that allows me to create and destroy windows, print etc and hides all
these details I would be interested to explore that possibility too.

thanks in advance!

HDC GetPrinterDC (void)
{
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) ;

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

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

free (pinfo5) ;
}
else // Windows NT
{
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 ;
}
 
E

Ed Morton

Jason said:
Hi, I am new to windows api programming and I wanted to create a little
utility program for myself in c using the mini gw compiler. I bought
petzold's book, programming windows 5th edition. I know the book was
written for people using visual c++ 6, win98/NT before xp came along but
thought it would be ok. My problem is that having lifted an example of code
in the GetPrinterDC function(pasted below) the compiler seems to take
exception to the enumprinters function and gives the following message for
each instance of it.

printer.o(.text+0x6c):printer.c: undefined reference to `EnumPrintersA@28'

actually, it compiles ok, but the above message is at the linking stage i
think, having issued this command 'gcc -mwindows -o printer.exe printer.o'.
I included windows.h. can anybody help me on this?

the enumprinters function is described as filling an array of structures
with information on attached printers, allowing the function GetPrinterDC to
return a handle for the printer device context.

Is there a major flaw in my plan to use this book for creating GUI on this
compiler and xp assuming you know the book anyway? I want to learn windows
api stuff, though i find it frustrating that this function doesnt appear to
be known. Does it take a different set of arguments in xp?

Apart from this problem can anyone point me to further resources for doing
windows development on the minigw compiler. If there is a free GUI library
around that allows me to create and destroy windows, print etc and hides all
these details I would be interested to explore that possibility too.

You need to post this to a windows programming newsgroup. This NG is for
ANSI C only. Having said that, here's some comments on your code:
thanks in advance!

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

Add a couple of variables and a couple of macros and you don't need all
the duplicated code (see rewritten version at end).
if (GetVersion () & 0x80000000) // Windows 98
{
EnumPrinters (PRINTER_ENUM_DEFAULT, NULL, 5, NULL,
0, &dwNeeded, &dwReturned) ;

pinfo5 = malloc (dwNeeded) ;

Test the return from malloc() for failure.

Here's the rewritten version:

#define ENUMPRINTERS(pi,dw,pdw) EnumPrinters(PRINTER_ENUM_DEFAULT,\
NULL, infoType, (PBYTE)(pi), (dw), (pdw), &dwReturned)

#define PINFO (infoType == 5 ? \
(PRINTER_INFO_5 *)pinfo : (PRINTER_INFO_4 *)pinfo)

HDC GetPrinterDC (void)
{
DWORD dwNeeded, dwReturned ;
HDC hdc ;
void *pinfo;
int infoType = 4;

if (GetVersion () & 0x80000000) { // Windows 98
infoType = 5;
}

ENUMPRINTERS (NULL, 0, &dwNeeded) ;

pinfo = malloc (dwNeeded) ;

if (pinfo != NULL)
{
ENUMPRINTERS(pinfo, dwNeeded, &dwNeeded) ;

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

free (pinfo) ;
}
else
{
/* report that malloc failed and set hdc to a reasonable
* value or exit
*/
}
return hdc ;
}

Regards,

Ed.
 
A

Alan Balmer

You need to post this to a windows programming newsgroup. This NG is for
ANSI C only. Having said that, here's some comments on your code:
Congratulations. You've turned a 70 line off-topic post into a 101
line off-topic post.

I'm responding only because, in the middle of the 101 lines, the OP
may have missed your advice to post to a different newsgroup.
 
E

Ed Morton

Alan said:
Congratulations. You've turned a 70 line off-topic post into a 101
line off-topic post.

The question posted was OT, but my response wasn't. It's common practice
to comment on the ANSI C aspects of any posted code, especially when it
clearly has bugs. Had I addressed the windows-specific issues your above
comment would have been justified.

Ed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top