Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using GetDefaultPrinter API

585 views
Skip to first unread message

Ofer Gaatone

unread,
Jul 28, 2008, 4:55:08 AM7/28/08
to
Hi,
i'm using Borland C++ 5.02 under Windows 2000. Trying to use
GetDefaultPrinter i always fail on linking. I added
#include <Winspool.h> to the application.

This is my code:
//============================
void InitPrinter (void)
{
char szPrinterName[255];
unsigned long lPrinterNameLength;
DOCINFO di;
int nError;

GetDefaultPrinter (szPrinterName, &lPrinterNameLength);
hPrinterDC = CreateDC("WINSPOOL\0", szPrinterName, NULL, NULL);

memset( &di, 0, sizeof(DOCINFO) );
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "PrintIt";
di.lpszOutput = (LPTSTR) NULL;
di.lpszDatatype = (LPTSTR) NULL;
di.fwType = 0;

nError = StartDoc(hPrinterDC, &di);
if (nError == SP_ERROR)
{
printf("\nError - please check printer.");
// Handle the error intelligently
}
}
//============================

Should i add any *.lib to the project ? should i use any *.dll ?
Any help, example, link, documents would be great.
Thanks
Ofer

Ed Mulroy [TeamB]

unread,
Jul 28, 2008, 10:20:32 AM7/28/08
to
Your message says

> Trying to use GetDefaultPrinter i always fail on linking

This implies but does not say that the linker error was that
GetDefaultPrinter was not found. By not giving the actual text of the linker
error anyone trying to help you is less certain as to the cause of the
problem.

Assuming that the linker error was a symbol not found error, which of
GetDefaultPrinter, GetDefaultPrinterA or GetDefaultPrinterW was what was
reported as not found?

You do not need to include winspool.h to make the compiler aware of the
function GetDefaultPrinter. Including windows.h already does that for you.
If you have not included windows.h, do so now and do it at the top of the
file.

> Should i add any *.lib to the project ? should i use any *.dll ?
> Any help, example, link, documents would be great.

As long as your project is configured to be for a Win32 target the IDE will
arrange for the correct library to be included in the link. In this case
that library is import32.lib

. Ed

> Ofer Gaatone wrote in message
> news:488d...@newsgroups.borland.com...

Ofer Gaatone

unread,
Jul 29, 2008, 2:07:41 AM7/29/08
to
The linker error was "Unresolved external '_GetDefaultPrinter'
referenced from ....."
I erased the include of winspool.h and as you wrote, and moved the
include windows.h
statement i already had to the top of the file.
Yet, i get same error message from the linker. Any idea ?
Thanks
Ofer

Ray Sittig

unread,
Jul 29, 2008, 8:25:42 AM7/29/08
to
See:

http://www.codeguru.com/forum/archive/index.php/t-137016.html

Also: TPrinter and TPrintDialog have the function without parameters

Ray Sittig


Ed Mulroy [TeamB]

unread,
Jul 29, 2008, 9:43:04 AM7/29/08
to
FYI:
His error is that _GetDefaultPrinter was not found. A LoadLibrary of the
function would need to go for GetDefaultPrinterA, no leading underscore and
added A at the end. No _GetDefaultPrinter symbol is provided by Windows.
His problem stems from the source, not from the link.

. Ed

> Ray Sittig wrote in message
> news:488f0c3f$1...@newsgroups.borland.com...

Ed Mulroy [TeamB]

unread,
Jul 29, 2008, 9:41:06 AM7/29/08
to
> The linker error was "Unresolved external '_GetDefaultPrinter' referenced
> from ....."

Ok.

Windows does not provide a function whose public name is _GetDefaultPrinter.

Pull up winspool.h in the editor and look for for the text GetDefaultPrinter
Do not select "whole word" in your search. You should find something like
this

-------------------
#ifdef UNICODE
#define GetDefaultPrinter GetDefaultPrinterW
#else
#define GetDefaultPrinter GetDefaultPrinterA
#endif
-------------------

Later in the file there should be a function prototype something like this

-------------------
BOOL WINAPI GetDefaultPrinterA(
LPSTR pszBuffer,
LPDWORD pcchBuffer);

-------------------

The idea is that if you are writing a normal (ASCII based) Windows program
then the public symbol used in the program will be GetDefaultPrinterA (A for
ASCII) and if a Unicode program it will be GetDefaultPrinterW (W for Wide
String, essentially what Unicode means).

If a program is C language or if C++ but the function prototype or the
section of the file the prototype is in is decorated with extern "C" then
the external symbol is the same as seen in the source code except that a
leading underscore is added. That is what you reported.

The two problems here are that Winspool.h decorates the function prototype
with WINAPI (a macro for __stdcall) which means the leading underscore is
not used and the actual function prototype is provides ends in A or W which
the error message you show does not have.

The result is that one of
the winspool.h you are using is flawed
or
winspool.h is not being included in a place or manner where
it applies to the program line where you call GetDefaultPrinter

My guess is that winspool.h is just fine. Look for where you put the
include and if it is blocked out by some macro.

. Ed

> Ofer Gaatone wrote in message

> news:488e...@newsgroups.borland.com...

Ed Mulroy [TeamB]

unread,
Jul 29, 2008, 10:03:56 AM7/29/08
to
Ok, I have finally managed to resurect the Win95 machine so that I can look
at the include files that came from the 11-year-old BC++ 5.02.

Winspool.h does NOT have a function prototype for GetDefaultPrinter.

Do this:
Save a copy of BC5\INCLUDE\WIN32\winspool.h
Open BC5\INCLUDE\WIN32\winspool.h in a text editor or the IDE editor
Find this line
#define DeletePrintProvider DeletePrintProviderW
Below it is this line
#endif // !UNICODE
Add a blank line following it and then add these lines

BOOL WINAPI GetDefaultPrinterA(
LPSTR pszBuffer, LPDWORD pcchBuffer );

BOOL WINAPI GetDefaultPrinterW(
LPWSTR pszBuffer, LPDWORD pcchBuffer);

#ifdef UNICODE
#define GetDefaultPrinter GetDefaultPrinterW
#else
#define GetDefaultPrinter GetDefaultPrinterA

#endif // !UNICODE

Please let me know how it works out.

. Ed

> Ofer Gaatone wrote in message

> news:488e...@newsgroups.borland.com...

Wayne A. King

unread,
Jul 31, 2008, 10:58:04 PM7/31/08
to
On Tue, 29 Jul 2008 09:41:06 -0400, "Ed Mulroy [TeamB]"
<dont_e...@bitbuc.ket> wrote:

>The idea is that if you are writing a normal (ASCII based) Windows program
>then the public symbol used in the program will be GetDefaultPrinterA (A for
>ASCII)

Just a little semantic nit-picking:

I believe the "A" is for ANSI, as Windows originally used the ANSI
character set, unlike DOS which used the ASCII character set.

- Wayne
- Wayne A. King
wak...@idirect.com

Ed Mulroy [TeamB]

unread,
Aug 1, 2008, 7:46:26 AM8/1/08
to
Yes, you are correct. It is for ANSI and not ASCII. I had forgotten.

. Ed

> Wayne A. King wrote in message
> news:4tu494to80dlih656...@4ax.com...

Ofer Gaatone

unread,
Aug 3, 2008, 10:23:58 AM8/3/08
to
Although the are many printer's api headers in the file winspool.h i did
not find the text 'GetDefaultPrinter'. Trying to search for
'DefaultPrinter' did not help. Any idea how to proceed ?

Ofer

Ed Mulroy [TeamB]

unread,
Aug 3, 2008, 11:35:38 AM8/3/08
to
I already gave you the text to add to the header file for that function back
on July 29.

Below is a copy of the message.

. Ed

--------------------------------------


Ok, I have finally managed to resurect the Win95 machine so that I can
look at the include files that came from the 11-year-old BC++ 5.02.

Winspool.h does NOT have a function prototype for GetDefaultPrinter.

Do this:
Save a copy of BC5\INCLUDE\WIN32\winspool.h
Open BC5\INCLUDE\WIN32\winspool.h in a text editor or the IDE editor
Find this line
#define DeletePrintProvider DeletePrintProviderW
Below it is this line
#endif // !UNICODE
Add a blank line following it and then add these lines

BOOL WINAPI GetDefaultPrinterA(


LPSTR pszBuffer, LPDWORD pcchBuffer );
BOOL WINAPI GetDefaultPrinterW(
LPWSTR pszBuffer, LPDWORD pcchBuffer);

#ifdef UNICODE


#define GetDefaultPrinter GetDefaultPrinterW
#else
#define GetDefaultPrinter GetDefaultPrinterA

#endif // !UNICODE

Please let me know how it works out.

--------------------------------------

> Ofer Gaatone wrote in message

> news:4895bfa6$1...@newsgroups.borland.com...

0 new messages