2010-09-28 11 views
6

ho un seguente dichiarazione di metodo in VB e la necessità di tradurlo in C#:VB a C# riscrittura domanda

<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 

In particolare io non sono sicuro se lo specificatore ByRef argomento è equivalente a ref è C#.
Inoltre, non so se Shared == static e se deve essere extern. Probabilmente molti di voi sono competenti sia in VB che in C#, quindi sarei grato di fornire una dichiarazione corretta in C#.

risposta

1

Usando questo "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) { 
} 

Spero che questo aiuta.

Grazie, Damian

+2

Questo non è corretto. Per lo meno, manca il 'extern 'e ha un corpo di metodo troppi. –

+0

@Konrad: hai ragione. Senza più contesto, il traduttore non sa che l'implementazione è esterna, poiché DllImport non richiede "sul serio" come dovrebbe. Modifica per correggere. –

1

In particolare non sono sicuro se lo specificatore ByRef argomento è equivalente a ref è C#. Inoltre, non so se Shared == static e se deve essere extern.

Sì, tutte queste assumtions sono corrette:

[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 realtà, ByRef di corrispondenza a uno o refout ma dal momento che non so quale è richiesta qui sto andando con il più generale ref - questo è garantito per funzionare).

+0

È * fuori * [15 caratteri] –

0

Un ottimo strumento di traduzione è il reflector .NET. Usalo per decodificare un file EXE o DLL in varie lingue: 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); 

C'è un buon strumento di conversione qui, non gestisce tutto, ma è abbastanza buono.

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