Hanqian
asked on
How to get default printer?
I use this function:
GetDefaultPrinter(..,..) to get the printer name, I have
include the header files:
#include "winspool.h"
#include "windows.h"
I also upgradte latest SDK, but still GetDefaultPrinter is undefined, My computer is windowXP.
Thanks.
Hanqian
GetDefaultPrinter(..,..) to get the printer name, I have
include the header files:
#include "winspool.h"
#include "windows.h"
I also upgradte latest SDK, but still GetDefaultPrinter is undefined, My computer is windowXP.
Thanks.
Hanqian
ASKER
I added it , but it still doesn't recognize GetDefaultPrinter(..,..).
Thanks.
Hanqian
Thanks.
Hanqian
Made sure that your DevStudio is looking at correct version of header files and library. Check how many copies of them you have on your machine.
Can you give us the *exact* error message, making sure it has the error number? And tell us which version of VisualStudio you are using?
I just created a new SDI project from scratch, added a call to GetDefaultPrinter(), and built it. It worked perfectly without me having to specify any extra libraries. Examination of the exe shows that it pulls it out of winspool.drv . This is on VC++.Net 2003.
I just created a new SDI project from scratch, added a call to GetDefaultPrinter(), and built it. It worked perfectly without me having to specify any extra libraries. Examination of the exe shows that it pulls it out of winspool.drv . This is on VC++.Net 2003.
ASKER
Error:
error C2065: 'GetDefaultPrinter' : undeclared identifier
version of VisualStudio:
MSDEV98.
Thanks.
Hanqian
error C2065: 'GetDefaultPrinter' : undeclared identifier
version of VisualStudio:
MSDEV98.
Thanks.
Hanqian
Open up winspool.h and search for GetPrinterDefault. It's not a supported function on windows 98, so your development environment may not know about it even though you are running XP. An up-to-date SDK probably has it, in which case you need to make sure that the SDK include directories are being checked before the VisualStudios directories.
i agree with wayside, it want working on mine too, until i got the latest sdk from
http://www.microsoft.com/msdownload/platformsdk/sdkupdate/
http://www.microsoft.com/msdownload/platformsdk/sdkupdate/
You can use the function in the following url:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;246772
http://support.microsoft.com/default.aspx?scid=kb;EN-US;246772
This is because this function is Included in Windows 2000 and later so if your application's target OS is not specified explicitly win2k it wan't compile and it gives error because in spool.h definition of this function is conditionally included. better way is to check the os and accordingly call the function. please tell me your application's target os and I will provide you code.
in 9x and NT you have to do little trick( I know it and I did it in my application)
vijay
in 9x and NT you have to do little trick( I know it and I did it in my application)
vijay
ASKER
Hi Wayside,
I have found that in latest installed SDK in my Program File directory, Winspool.h has
GetDefaultPrinter, but not in WinSpool.h which is in Program File/Microsoft Visual Studio/ directory
, how to make sure that the SDK include directories are being checked before the VisualStudios directories??
Why SDK doesn't overwrite the same files in Visual Studio??
Thanks.
Hi Vijay,
I am not quite sure what application's target os means, my application works for Window 2000
, Window NT and WIndowXp.
Thanks.
I have found that in latest installed SDK in my Program File directory, Winspool.h has
GetDefaultPrinter, but not in WinSpool.h which is in Program File/Microsoft Visual Studio/ directory
, how to make sure that the SDK include directories are being checked before the VisualStudios directories??
Why SDK doesn't overwrite the same files in Visual Studio??
Thanks.
Hi Vijay,
I am not quite sure what application's target os means, my application works for Window 2000
, Window NT and WIndowXp.
Thanks.
if(m_nOsVer == NT4)
{
if(!szDefPrnName)
{
WriteLog("Printer name not specified for %s",szPrnName);
return FALSE;
}
HKEY hKey=NULL;
TCHAR valbuff[MAX_PATH];
ZeroMemory(valbuff,MAX_PAT H);
DWORD dwType = REG_SZ;
DWORD cbBuff = MAX_PATH;
char* buff = _T("Software\\Microsoft\\W indows NT\\CurrentVersion\\Device s");
long result = RegOpenKeyEx(HKEY_CURRENT_ USER,buff, NULL,KEY_Q UERY_VALUE |KEY_SET_V ALUE,&hKey );
if(ERROR_SUCCESS != result )
{
WriteLog("GetDefaultPrn: Error accessing Registery ");
return FALSE;
}
bRet = RegQueryValueEx(hKey,szDef PrnName,NU LL,&dwType ,(BYTE*)va lbuff,&cbB uff);
if(ERROR_SUCCESS != bRet)
{
WriteLog("GetDefaultPrn: Error accessing Registery ");
return FALSE;
}
if(!valbuff || _tcslen(valbuff) <= 0)
{
m_bNoDefPrn = TRUE;
return FALSE;
}
_tcscat(szDefPrnName,",");
_tcscat(szDefPrnName,valbu ff);
RegCloseKey(hKey);
buff = _T("Software\\Microsoft\\W indows NT\\CurrentVersion\\Window s");
result = RegOpenKeyEx(HKEY_CURRENT_ USER,buff, NULL,KEY_Q UERY_VALUE |KEY_SET_V ALUE,&hKey );
if(ERROR_SUCCESS != result )
{
WriteLog("GetDefaultPrn: Error accessing Registery ");
return FALSE;
}
bRet= RegSetValueEx(hKey,_T("Dev ice"),NULL ,dwType,(c onst BYTE*)szDefPrnName,_tcslen (szDefPrnN ame));
bRet= (bRet==ERROR_SUCCESS);
RegCloseKey(hKey);
}
else if(m_nOsVer >= WIN2K)
{
typedef BOOL ( WINAPI *SETDEFPRN)(LPCSTR );
//Old version of winspool.drv may not have this function so
//better check it out.
HINSTANCE hSpoolib = LoadLibrary(_T("winspool.d rv"));
if(hSpoolib)
{
SETDEFPRN SetDefPrn = (SETDEFPRN)GetProcAddress( hSpoolib,_ T("SetDefa ultPrinter A"));
if(SetDefPrn)
{
if(!SetDefPrn((LPCSTR)szPr nName))
{
WriteLog("Failed to set printer %s default",szPrnName);
bRet = FALSE;
}
//else// default printer is set
// m_bDefPrnSet = TRUE;//so do not take care about default printer now onward
}
else
{
WriteLog("Function SetDefaultPrinter not found");
bRet = FALSE;
}
FreeLibrary(hSpoolib);
}
else
{
WriteLog("Load library %s failed",_T("winspool.drv") );
bRet = FALSE;
}
}//IF >= WIN2K
{
if(!szDefPrnName)
{
WriteLog("Printer name not specified for %s",szPrnName);
return FALSE;
}
HKEY hKey=NULL;
TCHAR valbuff[MAX_PATH];
ZeroMemory(valbuff,MAX_PAT
DWORD dwType = REG_SZ;
DWORD cbBuff = MAX_PATH;
char* buff = _T("Software\\Microsoft\\W
long result = RegOpenKeyEx(HKEY_CURRENT_
if(ERROR_SUCCESS != result )
{
WriteLog("GetDefaultPrn: Error accessing Registery ");
return FALSE;
}
bRet = RegQueryValueEx(hKey,szDef
if(ERROR_SUCCESS != bRet)
{
WriteLog("GetDefaultPrn: Error accessing Registery ");
return FALSE;
}
if(!valbuff || _tcslen(valbuff) <= 0)
{
m_bNoDefPrn = TRUE;
return FALSE;
}
_tcscat(szDefPrnName,",");
_tcscat(szDefPrnName,valbu
RegCloseKey(hKey);
buff = _T("Software\\Microsoft\\W
result = RegOpenKeyEx(HKEY_CURRENT_
if(ERROR_SUCCESS != result )
{
WriteLog("GetDefaultPrn: Error accessing Registery ");
return FALSE;
}
bRet= RegSetValueEx(hKey,_T("Dev
bRet= (bRet==ERROR_SUCCESS);
RegCloseKey(hKey);
}
else if(m_nOsVer >= WIN2K)
{
typedef BOOL ( WINAPI *SETDEFPRN)(LPCSTR );
//Old version of winspool.drv may not have this function so
//better check it out.
HINSTANCE hSpoolib = LoadLibrary(_T("winspool.d
if(hSpoolib)
{
SETDEFPRN SetDefPrn = (SETDEFPRN)GetProcAddress(
if(SetDefPrn)
{
if(!SetDefPrn((LPCSTR)szPr
{
WriteLog("Failed to set printer %s default",szPrnName);
bRet = FALSE;
}
//else// default printer is set
// m_bDefPrnSet = TRUE;//so do not take care about default printer now onward
}
else
{
WriteLog("Function SetDefaultPrinter not found");
bRet = FALSE;
}
FreeLibrary(hSpoolib);
}
else
{
WriteLog("Load library %s failed",_T("winspool.drv")
bRet = FALSE;
}
}//IF >= WIN2K
ASKER
Hi Vijay,
I tried just this part code: else if(m_nOsVer >= WIN2K), it doesn't work, it returns
printer name with garbage.
Thanks.
I tried just this part code: else if(m_nOsVer >= WIN2K), it doesn't work, it returns
printer name with garbage.
Thanks.
ASKER
Hi Vijay,
Since I am getting Default info. Should this line code :
SETDEFPRN SetDefPrn = (SETDEFPRN)GetProcAddress( hSpoolib,_ T("SetDefa ultPrinter A"));
be:
GETDefPrn = (GETDEFPRN)GetProcAddress( hSpoolib,_ T("GetDefa ultPrinter A")); ??
Hanqian
Since I am getting Default info. Should this line code :
SETDEFPRN SetDefPrn = (SETDEFPRN)GetProcAddress(
be:
GETDefPrn = (GETDEFPRN)GetProcAddress(
Hanqian
When you look at http://support.microsoft.com/default.aspx?scid=kb;EN-US;246772
you will see two functions: DPGetDefaultPrinter() and DPSetDefaultPrinter()
These functions works for all versions of Windows and written by Microsoft, which will work fine (I didn't try yet, 'cos I don't have a chance to).
So those functions will do all *tricks* and stuff..
Caner
you will see two functions: DPGetDefaultPrinter() and DPSetDefaultPrinter()
These functions works for all versions of Windows and written by Microsoft, which will work fine (I didn't try yet, 'cos I don't have a chance to).
So those functions will do all *tricks* and stuff..
Caner
is printspooler service is running ?
because this code is working fine here
because this code is working fine here
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Caner,
I have tried it, it wouldn't complie for me, it complaints:
////////////////////////// /////////
fnGetDefaultPrinter = GetProcAddress(hWinSpool, GETDEFAULTPRINTER);
if (!fnGetDefaultPrinter)
{
FreeLibrary(hWinSpool);
return FALSE;
}
bFlag = fnGetDefaultPrinter(pPrint erName, pdwBufferSize);
////////////////////////// ////////// ////////// /////////
fnGetDefaultPrinter(pPrint erName, pdwBufferSize):too many actual parameters
Thanks.
I have tried it, it wouldn't complie for me, it complaints:
//////////////////////////
fnGetDefaultPrinter = GetProcAddress(hWinSpool, GETDEFAULTPRINTER);
if (!fnGetDefaultPrinter)
{
FreeLibrary(hWinSpool);
return FALSE;
}
bFlag = fnGetDefaultPrinter(pPrint
//////////////////////////
fnGetDefaultPrinter(pPrint
Thanks.
in your projec settings goto c++ and preprocessor catategory set
additional include directories with your path to sdk include like
e:\program files\Microsoft SDK\include
make sure that printspooer service is running and you have some printer set and make it default and just run following program
#include "stdio.h"
#include "windows.h"
#include "winspool.h"
int main(int argc, char* argv[])
{
char szPrnName[250];
DWORD dwSize = 250;
if(!GetDefaultPrinter(szPr nName,&dwS ize))
{
int d = GetLastError();
if(d == ERROR_INSUFFICIENT_BUFFER)
printf("ERROR_INSUFFICIENT _BUFFER");
else
if(d == ERROR_FILE_NOT_FOUND)
printf("ERROR_FILE_NOT_FOU ND");
}
printf("Default printer %s \n",szPrnName);
return 0;
}
additional include directories with your path to sdk include like
e:\program files\Microsoft SDK\include
make sure that printspooer service is running and you have some printer set and make it default and just run following program
#include "stdio.h"
#include "windows.h"
#include "winspool.h"
int main(int argc, char* argv[])
{
char szPrnName[250];
DWORD dwSize = 250;
if(!GetDefaultPrinter(szPr
{
int d = GetLastError();
if(d == ERROR_INSUFFICIENT_BUFFER)
printf("ERROR_INSUFFICIENT
else
if(d == ERROR_FILE_NOT_FOUND)
printf("ERROR_FILE_NOT_FOU
}
printf("Default printer %s \n",szPrnName);
return 0;
}
ASKER
Hi wayside,
Finialy I got it work, you said:
You should see directories for VC++, and then at the bottom a directory for the SDK. For example, mine shows
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE
C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE
C:\Program Files\Microsoft Visual Studio\VC98\ATL\INCLUDE
C:\Program Files\Microsoft SDK\Include
Make sure the directory for the SDK is at the bottom.
But it didn't work until I put the directory for the SDK is at the top. Now i get the function
GetDefaultPrinter works now. Thank you so much.
My thanks also goes all the people who have help me here.
Hanqian.
Finialy I got it work, you said:
You should see directories for VC++, and then at the bottom a directory for the SDK. For example, mine shows
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE
C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE
C:\Program Files\Microsoft Visual Studio\VC98\ATL\INCLUDE
C:\Program Files\Microsoft SDK\Include
Make sure the directory for the SDK is at the bottom.
But it didn't work until I put the directory for the SDK is at the top. Now i get the function
GetDefaultPrinter works now. Thank you so much.
My thanks also goes all the people who have help me here.
Hanqian.
Project Properties->Linker->Input,