开发者

Odd Errors from PInvoke struct/function

开发者 https://www.devze.com 2023-03-24 00:23 出处:网络
Im currently writing a C# wrapper for a C++ API, but a specific struct and a function that relies on this struct have been giving very strange errors when debugging.

Im currently writing a C# wrapper for a C++ API, but a specific struct and a function that relies on this struct have been giving very strange errors when debugging.

C++ Struct:

typedef struct  
{  
    unsigned __int handle;  
    char name[80];  
    unsigned int unique_ID;  
} DeviceInfo;

Followed by this function:

int __stdcall get_device_info(DeviceInfo di[], const int length_of_di_array, int* p_numValidDevices);

The struct and function is imported as such:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]  
public struct DeviceInfo  
{
    public UInt32 handle;  
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 80)]  
    public String name;  
    public UInt32 unique_ID;  
}

[DllImportAttribute("MyC++API.dll", EntryPoint = "get_device_info", CallingConvention = CallingConvention.StdCall)]  
public static extern int get_device_info(ref DeviceInfo di, int length_of_di_array, ref numValidDevices);

The intended use of this struct and function is just to obtain some device info from the board Im accessing. Currently I do not have access to the function body in C++, so I can only assume it's working 100% (works fine in C++).

The issue is that when I use the function to run through an array of structs, it outputs the data I'm looking for, but also will begin to fail at runtime, giving me various error windows.

C# code:

static void Main()  
{    
    int numValidDevices = 0; //initialize variable  
    DeviceInfo[] di = new DeviceInfo[16]; //max 开发者_开发技巧of 16 devices  

    for (int i = 0; i < numValidDevices; ++i) //sorts through all validated devices  
    {  
        rc = get_device_info(ref di[i], 16, ref numValidDevices); //accesses each device element and returns the data  
        Console.WriteLine("Handle: {0}\nName: {1}\nUnique ID: {2}", di[i].handle, di[i].name, di[i].unique_ID);  
    }  
    Console.ReadLine(); //stops console from closing prematurely  
    API_close();  //custom close function from the C++ API
}

Errors while debugging (information is still shown): "An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.dll

Additional information: Thread has not been started."

"An unhandled exception of type 'System.ExecutionEngineException' occurred in mscorlib.dll"

Error while debugging (information is not shown, program fails to execute): "An unhandled exception of type 'System.AccessViolationException' occurred in mscorlib.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

When closing the console window: "The instruction at '0x7c9113c0' referenced memory at '0x00000000'. The memory could not be 'written'." (sometimes says 'read' instead of 'written').

I've done a lot of research on PInvoke and came across the Microsoft InteropAssistant application, various stack overflow articles such as this one, and this post seems even closer to what Im doing, but I'm still digging into how to use the Marshal.CoTaskMemAlloc/Free, and see if it even will do anyhting...

Thus far what I have for my struct and function are correct, I've tried changing the struct to use an IntPtr but that does not return a di.name value and the di.unique_ID becomes jibberish (oddly enough the di.handle stays valid)

C# code:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]  
public struct DeviceInfo  
{
    public UInt32 handle;  
    IntPtr p_name;
    public String name { get { return Marshal.PtrToStringAnsi(p_name); } }
    public UInt32 unique_ID;  
}

Intended output:

Handle: 3126770193  
Name: DEVICE_A  
Unique ID: 12345678  

IntPtr output:

Handle: 3126770193  
Name:  
Unique ID: 1145128264  

Oddly enough, using an IntPtr results in none of the errors above, and runs fine. This leads me to believe the issue lies with marshaling over the C++ char to a string, but I'm not sure if the issue lies with the marshaling, memory management (there is none?), or something I'm not catching entirely.

Any and all feedback would be really appreciated, I've been stumped on this for a number of weeks now...


The exceptions you get indicate that the unmanaged code you are pinvoking is destroying the garbage collected heap. It isn't crystal why, but you don't give the pinvoke marshaller much of a chance to do the Right Thing. It cannot properly pin the array. Start by declaring the function properly, it takes an array so declare one:

[DllImportAttribute("MyC++API.dll", CallingConvention = CallingConvention.StdCall)]  
public static extern int get_device_info(
    DeviceInfo[] di, 
    int length_of_di_array, 
    out int p_numValidDevices
);

Your first declaration of DeviceInfo is correct, the 2nd isn't since the string isn't a pointer.


Something's not adding up, here. It's not clear to me how the function is supposed to be called.

In particular, this declaration:

int __stdcall get_device_info(DeviceInfo di[], const int length_of_di_array, int* p_numValidDevices);

doesn't match how you're using it:

[DllImportAttribute("MyC++API.dll", EntryPoint = "get_device_info", CallingConvention = CallingConvention.StdCall)]  
public static extern int get_device_info(ref DeviceInfo di, const int length_of_di_array, int* p_numValidDevices);

...

DeviceInfo[] di = new DeviceInfo[16]; //max of 16 devices  
for (int i = 0; i < numValidDevices; ++i) //sorts through all validated devices  
{
    rc = get_device_info(ref di[i], 16, ref numValidDevices); //accesses each device element and returns the data
}

You're telling it that the array length is 16, starting at index i, which is wrong. Did you mean to only pass one element of the array at a time?

DeviceInfo[] di = new DeviceInfo[16]; //max of 16 devices  
for (int i = 0; i < numValidDevices; ++i) //sorts through all validated devices  
{
    rc = get_device_info(ref di[i], 1, ref numValidDevices); //accesses each device element and returns the data
}

Or did you mean to pass the entire array once?

DeviceInfo[] di = new DeviceInfo[16]; //max of 16 devices
rc = get_device_info(ref di[0], 16, ref numValidDevices); //accesses each device element and returns the data
for (int i = 0; i < numValidDevices; ++i) //sorts through all validated devices  
{
  Console.WriteLine(...);
}

P.S. I would consider changing your p/invoke declaration to be:

[DllImportAttribute("MyC++API.dll", EntryPoint = "get_device_info", CallingConvention = CallingConvention.StdCall)]  
public static extern int get_device_info(
    [In, Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] DeviceInfo[] di,
    int length_of_di_array,
    ref int p_numValidDevices);


So, as pointed out in the replies below, I had two problems:
1. I wasn't calling my DllImport correctly, the way I had it was in an attempt to hack together an output, in doing so I screwed up the memory allocation to the array of structs.
2. I tried to hack together an output and screwed up the code even more (tried to pass in the DeviceInfo array di as a single element di[number] instead of as a whole).

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]  
public struct DeviceInfo  
{
    public UInt32 handle;  
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 80)]  
    public String name;  
    public UInt32 unique_ID;  
}

[DllImportAttribute("MyC++API.dll", EntryPoint = "get_device_info", CallingConvention = CallingConvention.StdCall)]  
public static extern int get_device_info(
    [In, Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] DeviceInfo[] di,
    int length_of_di_array,
    ref int p_numValidDevices);

static void Main()
{

    int numValidDevices = 0;

    DeviceInfo[] di = new DeviceInfo[16];

    get_device_info(di, 16, ref numValidDevices);

    for (int i = 0; i < numValidDevices; ++i)
    {
        Console.WriteLine("Handle: {0}\nName: {1}\nUnique ID: {2}", di[i].handle, di[i].name, di[i].unique_ID);
    }

    Console.ReadLine();
    API_close();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消