Problem with winspool OpenPrinter DllImport in 2005  
Author Message
-Antilles-





PostPosted: Visual C# General, Problem with winspool OpenPrinter DllImport in 2005 Top

Hi,

i have a component which scans the spooler of a printer.

in Framework 1.1 it works fine but in 2.0 i got the following exception when i open the printer:

PInvokeStackImbalance was detected
Message: A call to PInvoke function 'Dornbracht.Odis.Server.PdfPrinter!Dornbracht.Odis.Server.PdfPrinter.Spooler.PrinterSpooler::OpenPrinter' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

My import looks like that:

[DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]

static extern int OpenPrinter(string pPrinterName, out IntPtr phPrinter, PRINTER_DEFAULTS pDefault);

[DllImport("winspool.drv", SetLastError=true)]

static extern int ClosePrinter(IntPtr hPrinter);

 

public struct PRINTER_DEFAULTS

{

public IntPtr pDatatype;

public IntPtr pDevMode;

public int DesiredAccess;

}

 

can anyone show me the right signature

regards

Antilles

 



Visual C#12  
 
 
molecula





PostPosted: Visual C# General, Problem with winspool OpenPrinter DllImport in 2005 Top

i get the same error after migration with dll import

PInvokeStackImbalance in .NET 2.0

please help us!


 
 
HadEnoughWithDotNet





PostPosted: Visual C# General, Problem with winspool OpenPrinter DllImport in 2005 Top

I just converted some vb.net 1.1 programs to vb.net 2.0, and hit the same probem of PInvokeStackImabalance when calling "openprinter.


 
 
-Antilles-





PostPosted: Visual C# General, Problem with winspool OpenPrinter DllImport in 2005 Top

Hi,

i've found a working solution for this problem. It's C# but perhaps you can convert it to vb.net:

[DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, SetLastError=true)]

public static extern int OpenPrinter(string pPrinterName, out IntPtr phPrinter, IntPtr pDefault);

int result;

IntPtr handle;

PRINTER_DEFAULTS pd = new PRINTER_DEFAULTS();

int rawsize = Marshal.SizeOf(pd);

IntPtr pdPtr = Marshal.AllocHGlobal(rawsize);

pd.DesiredAccess = PRINTER_ALL_ACCESS;

Marshal.StructureToPtr(pd, pdPtr, true);

result = OpenPrinter(printerName, out handle, pdPtr);

I hope it's a solution for you too.

Patrick


 
 
rcdias





PostPosted: Visual C# General, Problem with winspool OpenPrinter DllImport in 2005 Top

Hi,

I had the same problem. Now it's finally working. I got the code from http://support.microsoft.com/ id=322090 and replaced the OpenPrinter declaration with the following:

<DllImport("winspool.Drv", EntryPoint:="OpenPrinterW", _

SetLastError:=True, CharSet:=CharSet.Unicode, _

ExactSpelling:=False, CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function OpenPrinter(ByVal src As String, ByRef hPrinter As Integer, ByVal pd As Integer) As Boolean

End Function

Regards,

Ricardo


 
 
janverge





PostPosted: Visual C# General, Problem with winspool OpenPrinter DllImport in 2005 Top

Hi,

I would also like to add in some questions to this subject. May I ask who knows how to detect a network printer's IP address or Hostname using C# application. I would like to read the IP address on the prnter(s) installed in my PC.

Thank You...

 
 
GOAT v2b





PostPosted: Visual C# General, Problem with winspool OpenPrinter DllImport in 2005 Top

I found the issue behind this problem, thanks to Recardo's post. (I tend to want to know the why, not just what to do to fix something.)

Most likely, this was overlooked by the people at Microsoft.  A Long value in C++ has the same capacity as .NET's current Int32, int (C#), and Integer (VB.NET) values, which is -2,147,483,648 through 2,147,483,647. 

A long value in .NET is -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807 (9.2...E+18 ), which is a considerably larger value.

 

(Edited: Added a reference to what used to be declared as a long value)

The original kb article on how to print directly to a printer declared the OpenPrinter function as

Public Shared Function OpenPrinter(ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Long) As Boolean

By changing this to

Public Shared Function OpenPrinter(ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Integer) As Boolean, I no longer had a stack issue.


 
 
Glamiac





PostPosted: Visual C# General, Problem with winspool OpenPrinter DllImport in 2005 Top

I've tried each of these and I still can't get it to print. Can someone post their c# version It seems everyone changes a few different items, and getting it to work. I'm working with the Epson TM-T88IIR Partial Cut.

Thanks,

Josh