2010-09-28 7 views
6

j'ai une déclaration de méthode suivante en VB et ont besoin de le traduire en C#:VB à C# réécriture question

<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 

En particulier, je ne suis pas sûr si elle le spécificateur argument ByRef équivaut à ref est C#.
Aussi, je ne sais pas si Shared == static et si ce doit être extern. Probablement beaucoup d'entre vous maîtrisent à la fois VB et C#, donc je vous serais reconnaissant de fournir une déclaration correcte en C#.

Répondre

1

En utilisant cette "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) { 
} 

J'espère que cette aide.

Merci, Damian

+2

Ceci n'est pas correct. À tout le moins, il manque le 'extern' et a un corps de méthode trop. –

+0

@Konrad: Vous avez raison. Sans plus de contexte, le traducteur ne sait pas que l'implémentation est externe, car DllImport ne prend pas "sérieusement" comme il se doit. Modification pour corriger. –

1

En particulier, je ne suis pas sûr si le spécificateur argument ByRef équivaut à ref est C#. Aussi, je ne sais pas si Shared == static et si elle doit être extern.

Oui, tous ces assumtions sont corrects:

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

(En fait, ByRef peut correspondre soit ref ou out mais comme je ne sais pas ce qui est nécessaire ici, je vais avec le plus général ref - c'est garanti pour fonctionner).

+0

C'est * out * [15 caractères] –

0

Un excellent outil de traduction est le réflecteur .NET. Utilisez-le pour reverse engineering d'un fichier EXE ou DLL en plusieurs langues: 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); 

Il est un bon outil de conversion ici, il ne gère pas tout, mais c'est plutôt bien.

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