Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

OpenPrinter()gives me Access Violation!!!

Posted on 1998-05-07
8
Medium Priority
?
1,724 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:nc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1174176
I can give you a hint.  error 87 is caused by a bad parameter.  
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174177
I see the problem.  You are passing the handle, not a pointer to the handle.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174178
Try

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

with a & before the handle.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:nc
ID: 1174179
>> 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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174180
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.
0
 
LVL 22

Accepted Solution

by:
nietod earned 440 total points
ID: 1174181
When you see a parameter of type LPxxxxx it means window is expecting that the paramamter it gets is a pointer to a xxxxx type that it can access and alter.  It doesn't want to access the pointer, in the end it wants to access the xxxx that it points to.

All three parameter to OpenPrinter() are this way.  

Parameter 1:  Should be a pointer to a string.  This  is easy to do in C++ and you probably got it right.  It could be something like

char PrinterName[] = "\\\\server\\printername";
or
char * PrinterName = "\\\\server\\printername";

Eihter will work because the in both cases PrinterName will be a pointer to a string.  (It doesn't look like it in the first case, but it is).

Parameter 2:  Should point to a HANDLE.  Windows will change the value in the handle.
Thus you have several options.  You could do

HANDLE PrintHandle;

then pass &PrintHandle;  which passes a pointer to the handle that you declared

or you could do  

HANDLE PrintHandle;
LPHANDLE PrintHandlePtr = &PrintHandle;

then pass PrintHandlePtr (no &).  This passes a pointer to PrintHandle.  In either case windows will change PrintHandle.  You CANNOT do

LPHANDLE PrintHandle;

then pass PrintHandle.  Windows will try to change what PrintHandle points to. But you never made it point to anything so it will be poiting to a random location in memory.  This will cause a crash.


0
 
LVL 22

Expert Comment

by:nietod
ID: 1174182
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.
0
 

Author Comment

by:nc
ID: 1174183
Thank you.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question