Direct printing not working

G

Guest

Hi ALL
Please check the code below. OpenPrinter method returns Access is Denied error. Serial Printer is connected to the the IP address with name given at COM1
public enum ACCESS_MAS

PRINTER_ACCESS_ADMINISTER = 4
PRINTER_ACCESS_USE = 8
PRINTER_ALL_ACCESS = 98305

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)
public struct PRINTER_DEFAULTS

public string strDataType
public IntPtr ptrDevMode;
public ACCESS_MASK eDesiredAccess


[DllImport("winspool.drv", EntryPoint="OpenPrinterA", SetLastError=true, CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)
public static extern long OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string strPrinterName, out IntPtr phPrinter, ref PRINTER_DEFAULTS pdDefaults)

public void TestPrint(string strData

IntPtr ptrPrinter;
PRINTER_DEFAULTS pdDefaults = new PRINTER_DEFAULTS();
pdDefaults.eDesiredAccess = ACCESS_MASK.PRINTER_ACCESS_ADMINISTER;

long lReturn = OpenPrinter("\\\\111.11.11.11\\Code 39", out ptrPrinter, ref pdDefaults
if(lReturn == 0 || ptrPrinter == IntPtr.Zero
{
int intError = Marshal.GetLastWin32Error()
Marshal.ThrowExceptionForHR(unchecked((int)0x80070000 | Marshal.GetLastWin32Error()))


Note: Also can anybody tell me the link to get the exact declaratons of these APIs
Please help. Thanx in advance
 
M

Mattias Sjögren

Please check the code below. OpenPrinter method returns Access is Denied error.

Do you have administer access to the printer?

[DllImport("winspool.drv", EntryPoint="OpenPrinterA", SetLastError=true, CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern long OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string strPrinterName, out IntPtr phPrinter, ref PRINTER_DEFAULTS pdDefaults);

The return type here should be a bool, not long. And I wouldn't
recommend mixing CharSet.Auto with explciitly specifying the ANSI
version of the function as the entrypoint.

Note: Also can anybody tell me the link to get the exact declaratons of these APIs.

http://pinvoke.net is a good place to start looking. I'd declare it
like

[DllImport("winspool.drv", SetLastError=true, CharSet=CharSet.Auto)]
public static extern bool OpenPrinter(string strPrinterName, out
IntPtr phPrinter, ref PRINTER_DEFAULTS pdDefaults);



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top