Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB到C#重写问题_C#_.net_Vb.net_Translation - Fatal编程技术网

VB到C#重写问题

VB到C#重写问题,c#,.net,vb.net,translation,C#,.net,Vb.net,Translation,我在VB中有以下方法声明,需要将其转换为C#: _ 作为布尔值的公共共享函数OpenPrinter(ByVal src作为字符串,ByRef hPrinter作为IntPtr,ByVal pd作为Int16) 端函数 特别是我不确定ByRef参数说明符是否等同于ref是C# 另外,我不知道是否Shared==static以及它是否必须是extern。 可能你们中的很多人都精通VB和C,所以我非常感谢你们用C提供正确的声明。请在这里检查签名: 使用此选项: 我希望这有帮助 谢谢, 达米安 特别是

我在VB中有以下方法声明,需要将其转换为C#:

_
作为布尔值的公共共享函数OpenPrinter(ByVal src作为字符串,ByRef hPrinter作为IntPtr,ByVal pd作为Int16)
端函数
特别是我不确定
ByRef
参数说明符是否等同于
ref
是C# 另外,我不知道是否
Shared==static
以及它是否必须是
extern
。 可能你们中的很多人都精通VB和C,所以我非常感谢你们用C提供正确的声明。

请在这里检查签名:

使用此选项:

我希望这有帮助

谢谢, 达米安

特别是我不确定
ByRef
参数说明符是否等同于
ref
是C。 另外,我不知道是否
共享
=
静态
,以及它是否必须是
外部

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

[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
可以对应于
ref
out
,但由于我不知道这里需要哪一个,所以我将使用更通用的
ref
——这保证会起作用)。

一个很好的翻译工具是.NET reflector。使用它将EXE或DLL反向工程为各种语言:

VB

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


这是不对的。至少,它缺少了
extern
,并且有一个方法体太多了。@Konrad:你说得对。如果没有更多的上下文,译者就不知道实现是外部的,因为它没有像应该的那样“认真地”对待DllImport。编辑以更正。
[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) {
}
[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);
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
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);