Hallo,
ich habe folgende Funktion und möchte diese in csharp mittels DLL Import importieren:
C#-Quelltext
1:
| int getDeviceList(struct Sensor* list, unsigned listLen); |
mit zugehörigem Struct:
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
| struct Sensor { char dev[20]; char ID[8];
enum SensorType { MYSENSOR } type; uint16_t FWver; uint16_t FWsubver; }; |
Ich habe nun folgendes versucht:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63:
| [StructLayout(LayoutKind.Sequential)] public struct Sensor { public char [] dev; public char [] ID;
public enum SensorType { MYSENSOR };
SensorType type; public UInt16 FWver; public UInt16 FWsubver;
public void init() { dev = new char[20]; ID = new char[8]; } };
class Program { [DllImport("MyDLL.dll")] static extern int gGetDeviceList(ref Sensor [] list, uint listLen);
static void Main(string[] args) { Sensor [] list = new Sensor [10]; for (int j = 0; j < 10; j++) { list[j].init(); }
int i = -1; try { i = getDeviceList(ref list, 10); } catch (DllNotFoundException e) { Console.WriteLine("DllNotFoundException: {0}", e.ToString()); } catch (EntryPointNotFoundException e) { Console.WriteLine("EntryPointNotFoundException: {0}", e.ToString()); } catch (ArgumentException e) { Console.WriteLine("ArgumentException: {0}", e.ToString()); } catch(Exception e){ Console.WriteLine("Exception: {0}", e.ToString()); }
Console.WriteLine("test: "+i);
Console.ReadLine(); } } |
Daraufhin bekomme ich die folgende Fehlermeldung:
Exception: System.Runtime.InteropServices.SafeArrayTypeMismatchException: Das an
gegebene Array hat nicht den erwarteten Typ.
Ich habe auch schon folgendes versucht:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56:
| [StructLayout(LayoutKind.Sequential)] public struct Sensor { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)] public char[] dev;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)] public char [] ID;
public enum SensorType { MYSENSOR };
SensorType type; public UInt16 FWver; public UInt16 FWsubver; };
class Program { [DllImport("MyDLL.dll")] static extern int gGetDeviceList(ref Sensor [] list, uint listLen);
static void Main(string[] args) { Sensor [] list = new Sensor [10];
int i = -1; try { i = getDeviceList(ref list, 10); } catch (DllNotFoundException e) { Console.WriteLine("DllNotFoundException: {0}", e.ToString()); } catch (EntryPointNotFoundException e) { Console.WriteLine("EntryPointNotFoundException: {0}", e.ToString()); } catch (ArgumentException e) { Console.WriteLine("ArgumentException: {0}", e.ToString()); } catch(Exception e){ Console.WriteLine("Exception: {0}", e.ToString()); }
Console.WriteLine("test: "+i);
Console.ReadLine(); } } |
Woraufhin ich folgende Fehlermeldung bekam:
Die Laufzeit hat einen schwerwiegenden Fehler entdeckt. Fehleradresse: "0x79e77ce3" in Thread "0xf50". Fehlercode: 0xc0000005. Bei diesem Fehler könnte es sich um ein Problem in der CLR oder in den unsicheren oder nicht verifizierbaren Teilen des Benutzercodes handeln. Übliche Ursachen dieses Bugs sind Marshallerfehler für COM-Interop oder PInvoke, die den Stapel beschädigen können.
Hat jemand einen Rat oder Tipp was ich da falsch mache?
Grüße
Moderiert von
Christian S.: Code- durch C#-Tags ersetzt