VB转C#重写问题

时间:2010-09-28 13:46:02

标签: c# .net vb.net translation

我在VB中有以下方法声明,需要将其转换为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

特别是我不确定ByRef参数说明符是否等同于ref是C#。
另外,我不知道Shared == static是否必须extern。{{1}}。 很可能很多人都精通VB和C#,所以我很感激在C#中提供正确的声明。

5 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

使用此"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) {
}

我希望这会有所帮助。

谢谢, 达米安

答案 2 :(得分:1)

  

特别是我不确定ByRef参数说明符是否等同于ref是C#。   另外,我不知道Shared == static是否必须extern

是的,所有这些假设都是正确的:

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

(事实上,ByRef可以对应refout,但由于我不知道这里需要哪一个,所以我会选择更为通用ref } - 这保证有效。)

答案 3 :(得分:0)

一个很棒的翻译工具是.NET反射器。使用它将EXE或DLL反向工程为各种语言: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);
}

答案 4 :(得分:0)

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

这里有一个很好的转换工具,它不能处理所有事情,但它非常好。

http://www.developerfusion.com/tools/