Convert Unsafe c to C# wrapper to safe code

The parent question this code came from was:http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22976921.html
This question has the C source code and the header:  I just want to get this code to SAFE C# code.
[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

mileyjaAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
wizrrConnect With a Mentor Commented:
Here. That code compiled normally and should be without errors:
[StructLayout(LayoutKind.Sequential)]
private struct APCDeviceDescription {
	public IntPtr next;
	public IntPtr name;
	public IntPtr description;
}
 
[StructLayout(LayoutKind.Sequential)]
private struct APCDeviceDescriptionPointer {
	public IntPtr pointer;
}
 
public class APCDevice {
	public string name;
	public string description;
}
 
[DllImport("airpcap.dll")]
private extern static int AirpcapGetDeviceList(IntPtr ppDevices, StringBuilder pBuf);
 
[DllImport("airpcap.dll")]
private extern static int AirpcapFreeDeviceList(APCDeviceDescriptionPointer pDevices);
 
 
public static int AirpcapGetDeviceListWrap(out APCDevice[] outDevices, ref string outErrors)
{
	List<APCDevice> devicesToRet = new List<APCDevice>();
	APCDeviceDescriptionPointer pDevices;
	pDevices.pointer = IntPtr.Zero;
	//
	outDevices = null;
	outErrors = null;
//
	// now let's do that:
	// APCDeviceDescription** ppDevices = &pDevices;
	IntPtr ppDevices = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(APCDeviceDescriptionPointer)));
	Marshal.StructureToPtr(pDevices, ppDevices, false);
 
	StringBuilder builder = new StringBuilder(513);
	int result = 0;
	try
	{
		result = AirpcapGetDeviceList(ppDevices, builder);
	}
	finally
	{
		if (result != 0)
		{
			// get pointer from pointer-to-pointer
			APCDeviceDescriptionPointer pointer2 = (APCDeviceDescriptionPointer)Marshal.PtrToStructure(ppDevices, typeof(APCDeviceDescriptionPointer));
 
			if (pointer2.pointer != IntPtr.Zero) {
				APCDeviceDescription pDevice = (APCDeviceDescription)Marshal.PtrToStructure(pointer2.pointer, typeof(APCDeviceDescription));
				while (true) {
 
					APCDevice newDev = new APCDevice();
					newDev.name = Marshal.PtrToStringAnsi(pDevice.name);
					newDev.description = Marshal.PtrToStringAnsi(pDevice.description);
					devicesToRet.Add(newDev);
					// get next structure
					if ( pDevice.next == IntPtr.Zero ) {
						break;
					}
					pDevice = (APCDeviceDescription)Marshal.PtrToStructure(pDevice.next, typeof(APCDeviceDescription));
				}
				//
				outDevices = devicesToRet.ToArray();
				outErrors = null;
 
			}
			else
			{
				outErrors = builder.ToString();
				outDevices = null;
			}
		}
		if (ppDevices != IntPtr.Zero)
		{
			Marshal.FreeHGlobal(ppDevices);
			ppDevices = IntPtr.Zero;
		}
	}
	// /////////ENDING GET DEVICE LIST STUFF///////////////////////////////////////////////
	return (result);
}

Open in new window

0
 
wizrrCommented:
Hi again. I think now i know how to do that. Pointer is 32bit value, we can declare structure with same size and use Marshal.PtrToStructure/Marshal.StructureToPtr methods to do that.

I'm not sure. You must test.
[StructLayout(LayoutKind.Sequential)]
private struct APCDeviceDescription {
    public IntPtr next;
    public IntPtr name;
    public IntPtr description;
}
 
[StructLayout(LayoutKind.Sequential)]
private struct APCDeviceDescriptionPointer {
    public IntPtr pointer;
}
 
public class APCDevice {
    public string name;
    public string description;
}
 
[DllImport("airpcap.dll")]
private extern static int AirpcapGetDeviceList(IntPtr ppDevices, StringBuilder pBuf);
 
[DllImport("airpcap.dll")]
private extern static int AirpcapFreeDeviceList(APCDeviceDescriptionPointer pDevices);
 
public static int AirpcapGetDeviceListWrap(out APCDevice[] outDevices, ref string outErrors) {
    List<APCDevice> devicesToRet = new List<APCDevice>();
    APCDeviceDescriptionPointer pDevices;
    pDevices.pointer = IntPtr.Zero;
    //
    // now let's do that:
    // APCDeviceDescription** ppDevices = &pDevices;
    IntPtr ppDevices = Marshal.AllocHGlobal(Marshal.SizeOf(APCDeviceDescriptionPointer));
    Marshal.StructureToPtr(pDevices, ppDevices, false);
    
    StringBuilder builder = new StringBuilder(513);
    int result = 0;
    try {
        result = AirpcapGetDeviceList(ppDevices, builder );
    } finally {
 
        if (result != 0) {
            // get pointer from pointer-to-pointer
            APCDeviceDescriptionPointer pointer2 = Marshal.PtrToStructure(ppDevices, typeof(APCDeviceDescriptionPointer));
            // get structure from pointer
            APCDeviceDescription pointer3 = Marshal.PtrToStructure(pointer2.pointer, typeof(APCDeviceDescription));
            while (pointer3 != IntPtr.Zero) {
                APCDevice newDev = new APCDevice();
                newDev.name = Marshal.PtrToStringAnsi(pDevice->name);
                newDev.description = Marshal.PtrToStringAnsi(pDevice->description);
                devicesToRet.Add(newDev);
                // get next structure
                pointer3 = Marshal.PtrToStructure(pointer3.next, typeof(APCDeviceDescription));
            }
            AirpcapFreeDeviceList(pointer2);
            //
            outDevices = devicesToRet.ToArray();
            outErrors = null;
        } else {
            //
            outErrors = new String(buffer.ToString());
            outDevices = null;
        }
        if ( ppDevices != IntPtr.Zero ) {
            Marshal.FreeHGlobal(ppDevices);
            ppDevices = IntPtr.Zero;
        }
    }
    return ( result );
}

Open in new window

0
 
wizrrCommented:
Damn. Sorry.
There
-------
APCDeviceDescription pointer3 = Marshal.PtrToStructure(pointer2.pointer, typeof(APCDeviceDescription));
            while (pointer3 != IntPtr.Zero) {
                APCDevice newDev = new APCDevice();
                newDev.name = Marshal.PtrToStringAnsi(pDevice->name);
                newDev.description = Marshal.PtrToStringAnsi(pDevice->description);
                devicesToRet.Add(newDev);
                // get next structure
                pointer3 = Marshal.PtrToStructure(pointer3.next, typeof(APCDeviceDescription));
            }

--------
Should be
--------

 if (pointer2.pointer != IntPtr.Zero) {
    APCDeviceDescription pointer3 = Marshal.PtrToStructure(pointer2.pointer, typeof(APCDeviceDescription));
            while (true) {
               
                APCDevice newDev = new APCDevice();
                newDev.name = Marshal.PtrToStringAnsi(pDevice->name);
                newDev.description = Marshal.PtrToStringAnsi(pDevice->description);
                devicesToRet.Add(newDev);
                // get next structure
                if ( pointer3.next == IntPtr.Zero )
                     break;
                pointer3 = Marshal.PtrToStructure(pointer3.next, typeof(APCDeviceDescription));
            }

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mileyjaAuthor Commented:
 IntPtr ppDevices = Marshal.AllocHGlobal(Marshal.SizeOf(APCDeviceDescriptionPointer)); gives
Error      5      'GetDeviceList2.AirpcapInterface.APCDeviceDescriptionPointer' is a 'type' but is used like a 'variable'      
 im guess int this is suppposed to be pDevices

     APCDeviceDescriptionPointer pointer2 = Marshal.PtrToStructure(ppDevices, typeof(APCDeviceDescriptionPointer));
gives:
Error      6      Cannot implicitly convert type 'object' to 'GetDeviceList2.AirpcapInterface.APCDeviceDescriptionPointer'.

while (pointer3 != IntPtr.Zero)
Gives:
Error      8      Operator '!=' cannot be applied to operands of type 'GetDeviceList2.AirpcapInterface.APCDeviceDescription' and 'System.IntPtr'      

    outErrors = new String(buffer.ToString());
I dont think "buffer" is declared, thats basically what its telling me,

im running out of juice and im in class Ill probably test this more after class and get back to you if you can try to reconcile those items.

Thanks
0
 
mileyjaAuthor Commented:
thanks ill dig into her more after class at 10ish cst
0
 
wizrrCommented:
See previous post. Also try
Marshal.SizeOf(typeof(APCDeviceDescriptionPointer))
instead of
Marshal.SizeOf(APCDeviceDescriptionPointer)
//
Also
APCDeviceDescriptionPointer pointer2 = (APCDeviceDescriptionPointer)Marshal.PtrToStructure...
instead of
APCDeviceDescriptionPointer pointer2 = Marshal.PtrToStructure...
//
And last. Ofcourse not buffer. I mean 'builder'.
0
 
mileyjaAuthor Commented:
Ive got my brackets all messed up. somehow

below is what I did
       public static int AirpcapGetDeviceListWrap(out APCDevice[] outDevices, ref string outErrors)
        {
            List<APCDevice> devicesToRet = new List<APCDevice>();
            APCDeviceDescriptionPointer pDevices;
            pDevices.pointer = IntPtr.Zero;
            //
            // now let's do that:
            // APCDeviceDescription** ppDevices = &pDevices;
            IntPtr ppDevices = Marshal.AllocHGlobal(Marshal.SizeOf(APCDeviceDescriptionPointer));
            Marshal.StructureToPtr(pDevices, ppDevices, false);
 
            StringBuilder builder = new StringBuilder(513);
            int result = 0;
            try
            {
                result = AirpcapGetDeviceList(ppDevices, builder);
            }
            finally
            {
 
                if (result != 0)
                {
                    // get pointer from pointer-to-pointer
                    if (pointer2.pointer != IntPtr.Zero) {
                      APCDeviceDescription pointer3 = Marshal.PtrToStructure(pointer2.pointer, typeof(APCDeviceDescription));
            while (true) {
               
                APCDevice newDev = new APCDevice();
                newDev.name = Marshal.PtrToStringAnsi(pDevice->name);
                newDev.description = Marshal.PtrToStringAnsi(pDevice->description);
                devicesToRet.Add(newDev);
                // get next structure
                if ( pointer3.next == IntPtr.Zero ) {
                     break;
                pointer3 = Marshal.PtrToStructure(pointer3.next, typeof(APCDeviceDescription));
            }}
 
                else
                {
                    //
                    outErrors = new String(buffer.ToString());
                    outDevices = null;
                }
                if (ppDevices != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ppDevices);
                    ppDevices = IntPtr.Zero;
                }}
            
            return (result);
        } }
 
        // /////////ENDING GET DEVICE LIST STUFF///////////////////////////////////////////////
 
    }}

Open in new window

0
 
wizrrCommented:
Marshal.SizeOf(typeof(APCDeviceDescriptionPointer))

instead of

Marshal.SizeOf(APCDeviceDescriptionPointer)
public static int AirpcapGetDeviceListWrap(out APCDevice[] outDevices, ref string outErrors)
        {
            List<APCDevice> devicesToRet = new List<APCDevice>();
            APCDeviceDescriptionPointer pDevices;
            pDevices.pointer = IntPtr.Zero;
            //
            // now let's do that:
            // APCDeviceDescription** ppDevices = &pDevices;
            IntPtr ppDevices = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(APCDeviceDescriptionPointer)));
            Marshal.StructureToPtr(pDevices, ppDevices, false);
 
            StringBuilder builder = new StringBuilder(513);
            int result = 0;
            try
            {
                result = AirpcapGetDeviceList(ppDevices, builder);
            }
            finally
            {
 
                if (result != 0)
                {
                    // get pointer from pointer-to-pointer
                    if (pointer2.pointer != IntPtr.Zero) {
                      APCDeviceDescription pointer3 = (APCDeviceDescription)Marshal.PtrToStructure(pointer2.pointer, typeof(APCDeviceDescription));
            while (true) {
               
                APCDevice newDev = new APCDevice();
                newDev.name = Marshal.PtrToStringAnsi(pDevice->name);
                newDev.description = Marshal.PtrToStringAnsi(pDevice->description);
                devicesToRet.Add(newDev);
                // get next structure
                if ( pointer3.next == IntPtr.Zero ) {
                     break;
                pointer3 = (APCDeviceDescription)Marshal.PtrToStructure(pointer3.next, typeof(APCDeviceDescription));
            }}
 
                else
                {
                    //
                    outErrors = new String(builder.ToString());
                    outDevices = null;
                }
                if (ppDevices != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ppDevices);
                    ppDevices = IntPtr.Zero;
                }}
            
            return (result);
        } }
 
        // /////////ENDING GET DEVICE LIST STUFF///////////////////////////////////////////////
 
    }}

Open in new window

0
 
mileyjaAuthor Commented:
also you dont ever actually dim that pointer 2 i read the above post but it was kind of hard to understand.
0
 
mileyjaAuthor Commented:
you did the elipsis (...) thing again and I don't know where to stick that statement either.
0
 
mileyjaAuthor Commented:
good call, works beautifully
0
 
mileyjaAuthor Commented:
I am starting to figure out how this works on my own, I feel silly asking all these questions, I guess that happens when you are working with unmanaged to managed code and you have never used either C or C# before.
0
All Courses

From novice to tech pro — start learning today.