Hallo!
Ich möchte eine dll in c# einbinden(driver.dll). Die Spezifikation für den Dll-Zugriff(verwendete Datentypen) habe ich aus einem Header-Files(c++) herausgelesen. Möchte nun mit meiner C# Applikation die dll-Befehle aufrufen können. Meistens funktioniert die Aufrufe problemlos.
Aber es treten auch Exception bzw. Errormeldungen auf:
z.Bsp.:
Error1:
FatalExecutionEngineError was detected
Message: The runtime has encountered a fatal error. The address of the error was at 0x79eea33a, on thread 0x484. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack
Error2:ExecutionEngine Exception was thrown
Error3:
Fehler in Anwendung:
Die Anweisung in “0x01217c48” verweist auf Speicher in “0x3f0000b0“ Der Vorgang Written konnte nicht auf dem Speicher durchgeführt werden.
Solche Fehlermeldungen können durch den dll-import von unmanaged code.
Problem:Bei welchen Imports liegen ich mit meine Deklarationen falsch.
Ich habe leider keine Ahnung wo sich das Problem versteckt.
Danke schon mal für eure Hilfe
Markus
verwende Framework 2.0
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:
| [StructLayout (LayoutKind.Sequential, Pack=1)] public struct _cfg { public Int16 version; [MarshalAs(UnmanagedType.ByValArray ,SizeConst=14)] public char[] serial; public _channel Channel0; public _channel Channel1; public _channel Channel2; public _channel Channel3; public _channel Channel4; public _channel Channel5; public _channel Channel6; public _channel Channel7; }
[StructLayout(LayoutKind.Sequential, Pack = 1)] public struct _channel { public float highpass; public float lowpass; public float sensitivity; public float samplerate; public char polarity; }
[StructLayout (LayoutKind.Sequential,Pack=1)] unsafe public struct Buffer { public short* pBuffer; public UInt32 size; public UInt32 validPoints; }
[DllImport("driver.dll")] static extern IntPtr GT_OpenDevice([MarshalAs(UnmanagedType.LPStr)] string lpPort); [DllImport("driver.dll")] static extern bool GT_GetData(IntPtr hDevice, ref Buffer buffer, out System.Threading.NativeOverlapped lpovl);
[DllImport("driver.dll")] private static extern bool GT_GetConfig(IntPtr hdevice, IntPtr p_cfg);
public bool GetConfig(IntPtr hdevice, out _cfg ConfigMobilLab) { bool ret; ret = false; IntPtr p_cfg1 = Marshal.AllocHGlobal(4); ret = GT_GetConfig(hdevice, p_cfg1); ConfigMobilLab = (_cfg)Marshal.PtrToStructure(p_cfg1, typeof(_cfg)); Marshal.FreeHGlobal(p_cfg1); return ret; } |
Quelltext
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:
| c++ Code(Header-File)
typedef struct // structure used to handle buffers { SHORT *pBuffer; // Pointer to buffer UINT size; // Buffersize in bytes: max. 1024 bytes UINT validPoints; // number of data points returned from driver }_BUFFER_ST; // (data point size is 2 bytes)
typedef struct { float highpass; float lowpass; float sensitivity; float samplerate; char polarity; }__channel;
typedef struct { __int16 version; char serial[14]; __channel channels[8]; }__cfg;
GMOBILABPLUS_API HANDLE __stdcall GT_OpenDevice(LPSTR lpPort);
GMOBILABPLUS_API BOOL __stdcall GT_GetData(HANDLE hDevice,_BUFFER_ST *buffer,LPOVERLAPPED lpOvl);
GMOBILABPLUS_API BOOL __stdcall GT_GetConfig(HANDLE hdevice, __cfg * cfg); |