Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB to C# rewriting question

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#.

like image 256
nan Avatar asked Sep 28 '10 13:09

nan


People also ask

Can you convert VB to 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#.

Should I migrate from VB.NET 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#.

Can we convert C# to Java?

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.


2 Answers

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

like image 51
Andrey Avatar answered Oct 02 '22 06:10

Andrey


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).

like image 27
Konrad Rudolph Avatar answered Oct 02 '22 07:10

Konrad Rudolph