views:

92

answers:

5

I have a following method declaration in VB and need to translate it into C#:

<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

Particularly I am not sure if it the ByRef argument specifier is equivalent to ref is C#.
Also I don't know if Shared == static and whether it must be extern. Probably lot of you are proficient in both VB and C#, so I'd be grateful for providing correct declaration in C#.

+3  A: 

check signature here: http://pinvoke.net/default.aspx/winspool/OpenPrinter.html

Andrey
+1  A: 

Hi, Using this "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) {
}

I hope this helps.

Thanks, Damian

Damian Schenkelman
This isn’t correct. At the very least, it’s missing the `extern` and has one method body too many.
Konrad Rudolph
@Konrad: You're right. Without more context, the translator does not know the implementation is external, as it does not take DllImport as "seriously" as it should. Editing to correct.
Damian Schenkelman
+1  A: 

Particularly I am not sure if it the ByRef argument specifier is equivalent to ref is C#. Also I don't know if Shared == static and whether it must be extern.

Yes, all of these assumtions are correct:

[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 fact, ByRef can correspond to either ref or out but since I don’t know which is required here I’m going with the more general ref – this is guaranteed to work).

Konrad Rudolph
It is *out* [15 chars]
Hans Passant
A: 

A great translation tool is the .NET reflector. Use it to reverse engineer an EXE or DLL into various languages: 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);
}
ttt
A: 
[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);

There is a good conversion tool here, it doesn't handle everything, but it is pretty good.

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

David