2010-09-28 15 views
6

tengo un siguiente declaración de método en VB y la necesidad de traducirlo en C#:VB a C# reescribir la pregunta

<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 

Particularmente no estoy seguro de si el especificador ByRef argumento es equivalente a ref es C#.
Además, no sé si Shared == static y si debe ser extern. Probablemente muchos de ustedes son expertos tanto en VB como en C#, por lo que agradecería proporcionar la declaración correcta en C#.

Respuesta

1

El uso de este "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) { 
} 

espero que esto ayude.

Gracias, Damian

+2

Esto no es correcto. Por lo menos, falta el 'extern' y tiene un cuerpo de método demasiado. –

+0

@Konrad: Tienes razón. Sin más contexto, el traductor no sabe que la implementación es externa, ya que no toma DllImport como "seriamente" como debería. Editando para corregir –

1

Particularmente no estoy seguro de si el especificador ByRef argumento es equivalente a ref es C#. Además, no sé si Shared == static y si debe ser extern.

Sí, todos estos assumtions son correctas:

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

(De hecho, ByRef puede corresponder a cualquiera ref o out pero ya no sé lo que se requiere aquí voy con el más general ref - esto está garantizado para funcionar).

+0

Es * fuera * [15 caracteres] –

0

Una gran herramienta de traducción es el reflector .NET. Utilizarlo para realizar ingeniería inversa a un archivo EXE o DLL en varios idiomas: 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); 
} 
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); 

Hay una buena herramienta de conversión de aquí, no maneja todo, pero es bastante bueno.

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