开发者

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


check signature here: http://pinvoke.net/default.aspx/winspool/OpenPrinter.html


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


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


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜