简体   繁体   中英

VB to C# rewriting question

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#.

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

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 .

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).

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);
}
[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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM