Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Can any see what's wrong with the following?
Message
General information
Forum:
Visual C++
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00266137
Message ID:
00268976
Views:
22
See my comments in your code.
#include < WINDOWS.H >
>#include < winspool.h >
>#define DllExport __declspec( dllexport )
>DllExport DWORD PrinterStatus(LPTSTR lpPrinter);
>
>DllExport DWORD PrinterStatus(LPTSTR lpPrinter)
>{

DWORD is an unsigned type (unsigned long). C++ will accept -1 as initial value, but, if you want to initialize it correctly, it should be MAXDWORD. Probably you have a warning on this line. BTW, why don't you initialize it to 0?

>	DWORD iResult = -1;

This should be:
LPBYTE hGlobal = NULL;

>	HGLOBAL hGlobal = NULL;

iHandle will be the output value from OpenPrinter. The param is declared as LPHANDLE (so, a pointer) because it is an output value.
So, it should be declared here:

HANDLE iHandle = NULL;

>	LPHANDLE iHandle = 0;
>	DWORD level = 2, numbytes = 0, dSize;
>	PRINTER_INFO_2 *prninfo = NULL;

The call should be:
if SUCCEEDED(OpenPrinter(lpPrinter, &iHandle, NULL))

>	if SUCCEEDED(OpenPrinter(lpPrinter, iHandle, NULL))
>	{
>		/* Get the necessary size of the structure */
>		GetPrinter(iHandle, level, 0, 0, &numbytes);
>		if (numbytes > 0)
>		{
>			/* Allocate memory for the structure */

A little better:
hGlobal = new BYTE[numbytes];

>			hGlobal = GlobalAlloc(GHND, numbytes);
>			if (hGlobal != NULL)
>			{

I think it would be better:
prninfo = (PRINTER_INFO_2 *)hGlobal;

>				prninfo = (PRINTER_INFO_2 *)GlobalLock(hGlobal);
>				/* Call GetPrinter */

prninfo cannot be NULL

>				if (prninfo != NULL)
>				{
>                                        dSize = sizeof(prninfo);

This should be:
GetPrinter(iHandle, level, hGlobal, numbytes, &numbytes);

>					GetPrinter(iHandle, level, (LPBYTE)prninfo, dSize, &numbytes);
>					/* Set the return value */
>					iResult = prninfo->Status;
>					/* Free the memory */

I don't think you need to lock/unlock the buffer.

>					GlobalUnlock(hGlobal);
>				}
>				/* Release it */

If you used new BYTE[] to allocate memory, then:
delete [] hGlobal;

>				GlobalFree(hGlobal);
>			}
>		}
>		/* Close the printer */
>		ClosePrinter(iHandle);
>	}
>	return iResult;
>}
>It compiles fine, but I'm not certain that it's working correctly. When I change the assignment of the return value to: iResult = prninfo->Attributes, it always returns 0.

I'm guessing a little here, because I haven't tried it:
You declared iHandle as pointer to handle, but never allocated memory to it. When you pass it to the OpenPrinter function (pointer passed by value), C++ will change its value, but only inside the OpenPrinter function. So, you never get back the printer handle. So, the prninfo structure is never initialized. This is why you get always a 0.

BTW, a HANDLE is a kind of pointer. So, its more clear if you initialize it to NULL, not 0.

Hope this helps.

Vlad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform