Reputation: 20296
I have a following method declaration in VB and need to translate it into C#:
<DllImport("winspool.Drv", EntryPoint:="OpenPrinterW", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function OpenPrinter(ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Int16) As Boolean
End Function
Particularly I am not sure if it the ByRef
argument specifier is equivalent to ref
is C#.
Also I don't know if Shared == static
and whether it must be extern
.
Probably lot of you are proficient in both VB and C#, so I'd be grateful for providing correct declaration in C#.
Upvotes: 6
Views: 1328
Reputation: 8926
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter(string src, ref IntPtr hPrinter, Int16 pd);
There is a good conversion tool here, it doesn't handle everything, but it is pretty good.
http://www.developerfusion.com/tools/
Upvotes: 0
Reputation: 184
A great translation tool is the .NET reflector. Use it to reverse engineer an EXE or DLL into various languages: http://www.red-gate.com/products/reflector/
VB
Class Demo
<DllImport("winspool.Drv", EntryPoint:="OpenPrinterW", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True,CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function OpenPrinter(ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Int16) As Boolean
End Function
End Class
C#
internal class Demo
{
[DllImport("winspool.Drv", EntryPoint="OpenPrinterW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
public static extern bool OpenPrinter(string src, ref IntPtr hPrinter, short pd);
}
Upvotes: 0
Reputation: 3545
Using this "translator":
[DllImport("winspool.Drv", EntryPoint="OpenPrinterW", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool OpenPrinter(string src, ref IntPtr hPrinter, Int16 pd) {
}
I hope this helps.
Thanks, Damian
Upvotes: 1
Reputation: 545875
Particularly I am not sure if it the
ByRef
argument specifier is equivalent toref
is C#. Also I don't know ifShared
==static
and whether it must beextern
.
Yes, all of these assumtions are correct:
[DllImport("winspool.Drv", EntryPoint="OpenPrinterW",
SetLastError = true, CharSet = CharSet.Unicode,
ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter(string src, ref IntPtr hPrinter, Int16 pd);
(In fact, ByRef
can correspond to either ref
or out
but since I don’t know which is required here I’m going with the more general ref
– this is guaranteed to work).
Upvotes: 1
Reputation: 60075
check signature here: http://pinvoke.net/default.aspx/winspool/OpenPrinter.html
Upvotes: 3