Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 622
  • Last Modified:

Convert c# code to vb.net

I asked a question at the following URL that has more specific information.  


In short, it wraps a function from an unmanaged c++ dll and gives me access in C#.  I can currently build the c# project as a dll and reference it in a vb.net project but I would rather be able to have the new wrapped api itself in vb.net since I know a lot more about it and can make changes later.

This one is worth 500 points if you can convert this to vb.net 2005 code in a context that works for  my problem.
private struct APCDeviceDescription {
    public IntPtr next;
    public IntPtr name;
    public IntPtr description;
public class APCDevice {
    public string name;
    public string description;
private extern static unsafe int AirpcapGetDeviceList(APCDeviceDescription** ppDevices, char* pBuf);
private extern static unsafe int AirpcapFreeDeviceList(APCDeviceDescription* pDevices);
public static unsafe int AirpcapGetDeviceListWrap(out APCDevice[] outDevices, ref string outErrors) {
    List<APCDevice> devicesToRet = new List<APCDevice>();
    APCDeviceDescription* pDevices = (APCDeviceDescription*)0;
    APCDeviceDescription** ppDevices = &pDevices;
    char*pErrorBuffer = stackalloc char[513];
    int result = 0;
    try {
        result = AirpcapGetDeviceList(ppDevices, pErrorBuffer);
    } finally {
        if (result != 0) {
            APCDeviceDescription* pDevice = pDevices;
            while (pDevice != (APCDeviceDescription*)0) {
                APCDevice newDev = new APCDevice();
                newDev.name = Marshal.PtrToStringAnsi(pDevice->name);
                newDev.description = Marshal.PtrToStringAnsi(pDevice->description);
                pDevice = (APCDeviceDescription*)pDevice->next;
            outDevices = devicesToRet.ToArray();
            outErrors = null;
        } else {
            List<char> listChars = new List<char>();
            char charFromBuf = pErrorBuffer[0];
            while (charFromBuf != 0) {
            char[] errorsCharArray = listChars.ToArray();
            outErrors = new String(errorsCharArray);
            outDevices = null;
    return ( result );

Open in new window

  • 3
  • 2
  • 2
1 Solution
Wayne Taylor (webtubbs)Commented:
How's this?
<StructLayout(LayoutKind.Sequential)> _
Private Structure APCDeviceDescription
    Public [next] As IntPtr
    Public name As IntPtr
    Public description As IntPtr
End Structure
Public Class APCDevice
    Public name As String
    Public description As String
End Class 
<DllImport("airpcap.dll")> _
Private Shared Function AirpcapGetDeviceList(ByVal ppDevices As APCDeviceDescription**, ByVal pBuf As Char*) As Integer
End Function 
<DllImport("airpcap.dll")> _
Private Shared Function AirpcapFreeDeviceList(ByVal pDevices As APCDeviceDescription*) As Integer
End Function 
Public Shared Function AirpcapGetDeviceListWrap(ByRef outDevices As APCDevice(), ByRef outErrors As String) As Integer
    Dim devicesToRet As New List(Of APCDevice)()
    Dim pDevices As APCDeviceDescription* = DirectCast(0, APCDeviceDescription*)
    Dim ppDevices As APCDeviceDescription** = AddressOf
    Dim pErrorBuffer As Char* = 
    Dim result As Integer = 0
        result = AirpcapGetDeviceList(ppDevices, pErrorBuffer)
        If result <> 0 Then
            Dim pDevice As APCDeviceDescription* = pDevices
            While pDevice <> DirectCast(0, APCDeviceDescription*)
                Dim newDev As New APCDevice()
                newDev.name = Marshal.PtrToStringAnsi()
                newDev.description = Marshal.PtrToStringAnsi()
                pDevice = DirectCast(, APCDeviceDescription*)
            End While
            outDevices = devicesToRet.ToArray()
            outErrors = Nothing
            Dim listChars As New List(Of Char)()
            Dim charFromBuf As Char = pErrorBuffer(0)
            While charFromBuf <> 0
            End While
            Dim errorsCharArray As Char() = listChars.ToArray()
            outErrors = New String(errorsCharArray)
            outDevices = Nothing
        End If
    End Try
    Return (result)
End Function

Open in new window

Wayne Taylor (webtubbs)Commented:
mileyjaAuthor Commented:
  <DllImport("airpcap.dll")> _
Private Shared Function AirpcapGetDeviceList(ByVal ppDevices As APCDeviceDescription**, ByVal pBuf As Char*) As Integer
    End Function

<DllImport("airpcap.dll")> _
Private Shared Function AirpcapFreeDeviceList(ByVal pDevices As APCDeviceDescription*) As Integer
    End Function

I tihnk your close it doesnt like those asterisks that are used in the c#, I don't even know what those stupid things mean, any thoughts??  
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

I don't know VB, there some explanations using C#:
APCDeviceDescription val; <-- this is value type object variable declaration;
APCDeviceDescription * pVal; <-- this is declaration of pointer (it points to value with type APCDeviceDescription). Pointer points to your in-process memory.

fixed(pVal = &val) {// do something with pointer}; <-- taking address of variable val and putting it to pointer pVal
(i think in VB '&' operator == 'AddressOf' operator)

Why 'fixed'? Fixed statement means that garbage collection should not move 'val' in memory, so your pointer will always point to real 'val'.

APCDeviceDescription * * pVal; <-- this is declaration of pointer to pointer =). Using pointer to pointer your AirpcapGetDeviceList can put address of pointer (APCDeviceDescription*) to your variable (pointer to pointer variable). =)
mileyjaAuthor Commented:
so what do the asterisks actually mean??
My english is not very good. But i'll try to explain.
'*' -  it's pointer declaration in C#. When you adding asterisk after type name -- that means variable you decaring is not variable of type with that name, but an pointer to variable/(object instance) of this type.
What is pointer? See:

I think you can't find how to use pointers in VB). I searched how to declare pointer's in VB, but i can't find that. I also can't find 'unsafe' keyword in VB. I think unsafe code in C# cannot be converted to .NET. I have found AddressOf is not method to retrieve address of variable. AddressOf is used only to get reference to delegate. That why VB.NET code generated here http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx is not valid.


Maybe this is mistake? I don't know VB.
mileyjaAuthor Commented:
please tell me where Im wrong now that your on in this other questions


I think maybe there is a problem with the struct conversion, the intptr it returns seems like it comes back with a valid reference but something is just not quite there and im sure you can tell me what it is.

if there isn't much of a performance hit ill just not bother converting to vb.net and just reference the c# project in vb.net

The fact you don't think the unsafe code can be converted really makes the other question mega priority!!

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now