P/Invoked OpenPrinter fails when opening network printer

G

Guest

I have local printer connected to PC_1 (lpt/usb). It’s shared (no pwd, ACL
for printing is set to Everyone). I want to print data from PC_2, via
\\PC_1\<printerName>.

OpenPrinter and StartDocPrinter fail with GetLastError == 997
(ERROR_IO_PENDING - Overlapped I/O operation is in progress). I’m able to
recreate this problem on following configurations :

PC_1 = NT 4.0
PC_2 = W2k3

PC_1 = WXP
PC_2 = W2k3

Everything works fine when printing on local printer or via print server
(that is direct to printer, not through PC_1).

I’m using C# and .NET 1.1. I didn’t try to recreate this from C++ yet.

Any suggestions? Thank you for any given answer...

Here is my code (simplified):

public class PrintDirect
{
[DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern int OpenPrinter(string pPrinterName, out IntPtr
phPrinter, IntPtr pDefault);

[DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern int StartDocPrinter(IntPtr hPrinter, int Level, ref
DOCINFO pDocInfo);

[DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern int StartPagePrinter(IntPtr hPrinter);

[DllImport("winspool.drv", CharSet = CharSet.Ansi, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern int WritePrinter(IntPtr hPrinter, IntPtr pBuf, int buf,
ref int pcWritten);

[DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern int EndPagePrinter(IntPtr hPrinter);

[DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern int EndDocPrinter(IntPtr hPrinter);
[DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern int ClosePrinter(IntPtr hPrinter);
}

[StructLayout(LayoutKind.Sequential)]
public struct DOCINFO
{
[MarshalAs(UnmanagedType.LPWStr)] public string pDocName;
[MarshalAs(UnmanagedType.LPWStr)] public string pOutputFile;
[MarshalAs(UnmanagedType.LPWStr)] public string pDataType;
}

System.IntPtr lhPrinter = new System.IntPtr();
DOCINFO di = new DOCINFO();
int pcWritten = 0;
byte[] textBytes;

…

di.pDocName = "Etk";
di.pOutputFile = null;
di.pDataType = null;

PrintDirect.OpenPrinter(printerName, out lhPrinter, new System.IntPtr(0));
PrintDirect.StartDocPrinter(lhPrinter, 1, ref di);
PrintDirect.StartPagePrinter(lhPrinter);
IntPtr buf = Marshal.AllocCoTaskMem(textBytes.Length);
Marshal.Copy(textBytes, 0, buf, textBytes.Length);
PrintDirect.WritePrinter(lhPrinter, buf, textBytes.Length, ref pcWritten);
PrintDirect.EndPagePrinter(lhPrinter);
PrintDirect.EndDocPrinter(lhPrinter);
PrintDirect.ClosePrinter(lhPrinter);
Marshal.FreeCoTaskMem(buf);

…
 

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