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#.
The VB to C# code converter from the SharpDevelop team is now a standalone extension to Visual Studio. Once installed, you can convert an entire VB.NET project to C# by opening the solution, right clicking the solution node in the Solution Explorer and selecting Convert to C#.
NET Core, you need to convert VB.NET to C#. If desktop apps are enough for you and your users, VB.NET works with NET 5, but only to target Windows Forms--not WPF. But if you want to write web apps running on ASP.NET Core, you'll need C#.
To convert a C# snippet, we paste the code into the left editor and click on the Convert button. After clicking the Convert button, the tool converts the C# code to Java and shows it on the right editor. We can save this in a file using the Save button or copy it.
check signature here: http://pinvoke.net/default.aspx/winspool/OpenPrinter.html
Particularly I am not sure if it the
ByRef
argument specifier is equivalent toref
is C#. Also I don't know ifShared
==static
and whether it must beextern
.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With