2010-09-28 3 views
6

Ich habe eine folgende Methode Erklärung in VB und müssen es in C# übersetzen:VB zu C# Frage Umschreiben

<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 

Vor allem, wenn es ich bin nicht sicher, ob das ByRef Argument Spezifizierer zu ref entspricht ist C#.
Auch ich weiß nicht, ob Shared == static und ob es extern sein muss. Wahrscheinlich viele von Ihnen sind sowohl in VB als auch in C# kompetent, daher wäre ich dankbar für die korrekte Deklaration in C#.

Antwort

1

Mit diesem "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) { 
} 

Ich hoffe, das hilft.

Danke, Damian

+2

Dies ist nicht korrekt. Zumindest fehlt es dem "extern" und hat eine Methode Körper zu viele. –

+0

@Konrad: Du hast Recht. Ohne weiteren Kontext weiß der Übersetzer nicht, dass die Implementierung extern ist, da DllImport nicht so "ernst" ist, wie es sollte. Bearbeiten zum Korrigieren. –

1

Besonders bin ich, wenn es das ByRef Argument Spezifikations entspricht ref ist C# nicht sicher. Auch ich weiß nicht, ob Shared == static und ob es extern sein muss.

Ja, alle diese assumtions korrekt sind:

[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 der Tat kann ByRef entsprechen entweder ref oder out aber da ich weiß nicht, was hier erforderlich ist, werde ich mit die allgemeinere ref - das wird garantiert funktionieren.

+0

Es ist * out * [15 Zeichen] –

0

Ein großartiges Übersetzungstool ist der .NET-Reflektor. Verwenden Sie es umgekehrt eine EXE oder DLL in verschiedene Sprachen Ingenieur: 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); 

Es ist ein gutes Werkzeug zur Umwandlung hier, ist es nicht handhaben alles, aber es ist ziemlich gut.

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