简体   繁体   English

VB转C#重写问题

[英]VB to C# rewriting question

I have a following method declaration in VB and need to translate it into C#: 我在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

Particularly I am not sure if it the ByRef argument specifier is equivalent to ref is C#. 特别是我不确定ByRef参数说明符是否等同于ref是C#。
Also I don't know if Shared == static and whether it must be extern . 另外我不知道Shared == static是否必须是extern Probably lot of you are proficient in both VB and C#, so I'd be grateful for providing correct declaration in C#. 可能很多人都精通VB和C#,所以我很感激在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#. 特别是我不确定ByRef参数说明符是否等同于ref是C#。 Also I don't know if Shared == static and whether it must be extern . 另外我不知道Shared == static是否必须是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). (实际上, ByRef可以对应refout但因为我不知道这里需要哪个,所以我会选择更通用的ref - 这可以保证工作)。

A great translation tool is the .NET reflector. 一个很棒的翻译工具是.NET反射器。 Use it to reverse engineer an EXE or DLL into various languages: http://www.red-gate.com/products/reflector/ 使用它将EXE或DLL反向工程为各种语言: http//www.red-gate.com/products/reflector/

VB 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# 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/ http://www.developerfusion.com/tools/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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