Link to home
Start Free TrialLog in
Avatar of nc
nc

asked on

OpenPrinter()gives me Access Violation!!!

I'm trying to code an printer log app. My printer is connected to an NT server. I'm using a funktion called FindFirstPrinterChangeNotification and before I can use this funktion I need a handle to my printer. I use the OpenPrinter funktion to get this handle but in return I get 0 wich indicates an error. When I check LastErrorCode the number is 87. My call to OpenPrinter is: BOOL bResult = OpenPrinter(PrinterName ,PrintHandle,&PrinterDefault); where PrinterName is "\\\\server\\printername" and PrinterDefault.DesiredAccess = PRINTER_ALL_ACCESS the rest of the struct is set to NULL. In my debug windows I get this message:
First-chance exception in PrinterLog.exe (WINSPOOL.DRV): 0xC0000005: Access Violation.

Can anybody give me a hint off what is wrong? Nikolaj Christoffersen
Avatar of nietod
nietod

I can give you a hint.  error 87 is caused by a bad parameter.  
I see the problem.  You are passing the handle, not a pointer to the handle.
Try

BOOL bResult = OpenPrinter(PrinterName,&PrintHandle,&PrinterDefault)

with a & before the handle.
Avatar of nc

ASKER

>> I can give you a hint.  error 87 is caused by a bad parameter.
You are right the error 87 is a bad parameter but it dosn't solve my Access Violation error and the return value is still 0. Using & before the handle donsn't do anything either. My handle is declared as LPHANDLE if I want to use the & I ned to declare my handle as HANDLE.
I suspect the problem is in how you pass pointers to this procedure.  You declared the PrintHandle as LPHANDLE with is very unusual!  It can be done that way, but it is much harder so you are probably doing it incorrectly.  However, you are probably handling the last parameter PrinterDefault incorrectly as well.   I'll post some code.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Parameter 3:  Must point to a PRINTER_DEFAULTS structure.  Again you can do this several ways.  Thea easiest is

PRINTER_DEFAULTS PrintDefault;

Then pass &PrintDefault.  Or you could do

PRINTER_DEFAULTS PrintDefault;
LPPRINTER_DEFAULTS PrinterDefaultPtr = &PrintDefault;

and then pass PrinterDefaultPtr.  Again, you cannot do

LPPRINTER_DEFAULTS PrintDefault and then pass PrintDefault.  Windows will dereference this pointer and you never made it point to a PRINTER_DEFAULTS for it.

Here is the simplist example.

char * PrinterName = "\\\\server\\printername";
HANDLE PrintHandle;
PRINTER_DEFAULTS PrintDefault;

BOOL bResult = OpenPrinter(PrinterName,&PrintHandle,&PrintDefault);

If this doesn't help, post your code.
Avatar of nc

ASKER

Thank you.