Jul 18, 2008 at 4:41am
HI
I have written following code to get the default printer name
#include <windows.h>
#include <iostream.h>
#include <conio.h>
#include <winspool.h>
#define MAXPRINTERBUFFERSIZE 250
#define GETDEFAULTPRINTER "GetDefaultPrinterA"
using std::string;
string GetPrinterNameFunc();
int main()
{
string printerName = GetPrinterNameFunc();
cout<<printerName;
getch();
return 0;
}
typedef BOOL (*FNGETPRINTER)(LPTSTR ,DWORD );
string GetPrinterNameFunc()
{
BOOL bRet = FALSE ;
string strPrinterName;
// --- Load library winspool.drv ---
HMODULE hSpoolDrv = LoadLibrary("winspool.drv") ;
FNGETPRINTER fnGetPrinter = (FNGETPRINTER)GetProcAddress( hSpoolDrv, GETDEFAULTPRINTER ) ;
if( fnGetPrinter )
{
LPTSTR szPrinterName[MAX_PATH] ;
DWORD nLen = MAX_PATH ;
bRet = fnGetPrinter((LPTSTR)szPrinterName,nLen);
// --- Function call succeeds, then set the printer name ---
if( bRet )
strPrinterName = (char *)szPrinterName ;
}
FreeLibrary( hSpoolDrv ) ;
return strPrinterName;
}
I am using dev C++
Earlier it was working fine. But now I am getting error. When I open console window then I got an error message.
When I debug the program I am getting a message box saying
" An Access Violation(Segmentation Fault) raised in your program"
The error is coming on this line
"bRet = fnGetPrinter((LPTSTR)szPrinterName,nLen);"
Please advice
Regards
Karan
Last edited on Jul 18, 2008 at 4:46am