Solved

Convert c# code to vb.net

Posted on 2007-11-25
7
606 Views
Last Modified: 2013-12-16
I asked a question at the following URL that has more specific information.  

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22976921.html

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.
[StructLayout(LayoutKind.Sequential)]
private struct APCDeviceDescription {
    public IntPtr next;
    public IntPtr name;
    public IntPtr description;
}
 
public class APCDevice {
    public string name;
    public string description;
}
 
[DllImport("airpcap.dll")]
private extern static unsafe int AirpcapGetDeviceList(APCDeviceDescription** ppDevices, char* pBuf);
 
[DllImport("airpcap.dll")]
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);
                devicesToRet.Add(newDev);
                pDevice = (APCDeviceDescription*)pDevice->next;
            }
            AirpcapFreeDeviceList(pDevices);
            //
            outDevices = devicesToRet.ToArray();
            outErrors = null;
        } else {
            List<char> listChars = new List<char>();
            char charFromBuf = pErrorBuffer[0];
            while (charFromBuf != 0) {
                listChars.Add(charFromBuf);
            }
            char[] errorsCharArray = listChars.ToArray();
            //
            outErrors = new String(errorsCharArray);
            outDevices = null;
        }
    }
    return ( result );
}

Open in new window

0
Comment
Question by:mileyja
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 20346964
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
    Try
        result = AirpcapGetDeviceList(ppDevices, pErrorBuffer)
    Finally
        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()
                devicesToRet.Add(newDev)
                pDevice = DirectCast(, APCDeviceDescription*)
            End While
            AirpcapFreeDeviceList(pDevices)
            '
            outDevices = devicesToRet.ToArray()
            outErrors = Nothing
        Else
            Dim listChars As New List(Of Char)()
            Dim charFromBuf As Char = pErrorBuffer(0)
            While charFromBuf <> 0
                listChars.Add(charFromBuf)
            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

0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 20346969
0
 

Author Comment

by:mileyja
ID: 20346990
  <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??  
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 3

Expert Comment

by:wizrr
ID: 20347773
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). =)
0
 

Author Comment

by:mileyja
ID: 20347895
so what do the asterisks actually mean??
0
 
LVL 3

Accepted Solution

by:
wizrr earned 500 total points
ID: 20348002
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:
http://msdn2.microsoft.com/en-us/library/cxx6f46y(VS.71).aspx
http://msdn2.microsoft.com/en-US/library/t2yzs44b(vs.80).aspx
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=122782&SiteID=1
http://msdn2.microsoft.com/en-us/library/28k1s2k6(VS.80).aspx

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.

http://msdn2.microsoft.com/en-us/library/y72ewk2b(VS.80).aspx

Maybe this is mistake? I don't know VB.
0
 

Author Comment

by:mileyja
ID: 20348047
please tell me where Im wrong now that your on in this other questions

http://www.experts-exchange.com/Programming/Languages/C/Q_22981334.html

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!!
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help with a query 3 39
visual studio vb.net windows onkeyup 2 30
VB.Net  Exporting Excel Woorkbook to PDF 7 60
Telerik RadEditor Control Save 8 20
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question