pinvoke

C# Marshalling a C++ struct with wchar_t* member occasionally leaves the heap corrupted

一世执手 提交于 2019-12-26 06:04:22
问题 I have declared a struct as follows: // C++ struct TestStruct { wchar_t* TestString; }; and the corresponding managed representation // C# [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct TestStruct { [MarshalAs(UnmanagedType.LPWStr)] public string TestString; } As well as this function: // C++ __declspec(dllexport) void __stdcall FillMultipleStructs(TestStruct* testStructures, const short arrayLength) { for(int i = 0; i < arrayLength; i++) { const wchar_t

C# Marshalling a C++ struct with wchar_t* member occasionally leaves the heap corrupted

六月ゝ 毕业季﹏ 提交于 2019-12-26 06:04:01
问题 I have declared a struct as follows: // C++ struct TestStruct { wchar_t* TestString; }; and the corresponding managed representation // C# [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct TestStruct { [MarshalAs(UnmanagedType.LPWStr)] public string TestString; } As well as this function: // C++ __declspec(dllexport) void __stdcall FillMultipleStructs(TestStruct* testStructures, const short arrayLength) { for(int i = 0; i < arrayLength; i++) { const wchar_t

C# Marshalling a C++ struct with wchar_t* member occasionally leaves the heap corrupted

落爺英雄遲暮 提交于 2019-12-26 06:02:07
问题 I have declared a struct as follows: // C++ struct TestStruct { wchar_t* TestString; }; and the corresponding managed representation // C# [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct TestStruct { [MarshalAs(UnmanagedType.LPWStr)] public string TestString; } As well as this function: // C++ __declspec(dllexport) void __stdcall FillMultipleStructs(TestStruct* testStructures, const short arrayLength) { for(int i = 0; i < arrayLength; i++) { const wchar_t

delphi record to C#

删除回忆录丶 提交于 2019-12-26 03:31:17
问题 need help converting delphi record to C# struct and passing this struct to C dll: type Transaction = record pos_id: array[0..9] of char; ev_type: array[0..4] of char; amount: array[0..14] of char; chip_reader_used: boolean; internal_magstripereader_used: boolean; track2data: array[0..99] of char; card_usage_mode: array[0..4] of char; receipt_directory: array[0..254] of char; additional_info: array[0..999] of char; event_number: array[0..9] of char; original_filingcode: array[0..19] of char;

Getting AccessViolation Exception when returning a bool from C++ to C#

て烟熏妆下的殇ゞ 提交于 2019-12-25 18:34:38
问题 I am using a third-party, proprietary DLL for which the source code is not available to me. Wrapper code that appears to have been auto-generated using SWIG 1.3.39 is, however, available to me. The wrapper code consists of a C++ file that compiles (using some headers that describe the DLL) to a DLL and of a C# project that makes PInvoke calls to the C++ wrapper DLL. After inspecting the StackTrace I got the following information: at org.doubango.tinyWRAP.tinyWRAPPINVOKE.MediaSessionMgr

Why does accessing unmanaged memory cause a System.AccessViolationException? [duplicate]

左心房为你撑大大i 提交于 2019-12-25 18:33:55
问题 This question already has an answer here : Use XGBoost DLL from c# via p/invoke (1 answer) Closed 3 years ago . I'm trying to use XGBoost's dll (libxgboost.dll) to create a DMatrix (which is like a 2D array) and get how many columns it has. It runs fine until it throws a System.AccessViolationException at the int cols = ... line in the code below: using System; using System.Runtime.InteropServices; namespace basicXgboost { class Program { [DllImport("../../libs/libxgboost.dll", CharSet =

PInvoke, function call with pointer-to-pointer-to-pointer parameter

℡╲_俬逩灬. 提交于 2019-12-25 18:32:53
问题 I am creating a new question here as I now know how to ask this question, but I'm still a newb in PInvoke. I have a C API, with the following structures in it: typedef union pu { struct dpos d; struct epo e; struct bpos b; struct spos c; } PosT ; typedef struct dpos { int id; char id2[6]; int num; char code[10]; char type[3]; } DPosT ; and the following API function: int addPos(..., PosT ***posArray,...) the way I call this in C like this: int main(int argc, const char *argv[]) { ... PosT *

PInvoke Pointer to struct including float array

孤者浪人 提交于 2019-12-25 18:14:32
问题 I'm attempting to P Invoke a C library for use on a Xamarin android app. Consider the following C structure: typedef struct { bool myBool; myOtherStruct sOtherStruct; int myInt; float myFloat; float myFloatArray[1024]; float myFloatArray2[1024]; float myFloatArray3[20]; float myFloatArray4[30]; } sMyStruct; This is called using the following function: unsigned int initialise(sMyStruct* a_pMyStruct) I've put this into a C# structure: [StructLayout(LayoutKind.Sequential)] public unsafe struct

Need help with RegisterWindowMessage and SendMessage from c++ to c#

只谈情不闲聊 提交于 2019-12-25 16:43:23
问题 I have this code in C#: [DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern uint RegisterWindowMessage(string lpProcName); [DllImport("user32.dll")] private static extern IntPtr SendMessage( IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); and I need to convert this from C++ to C#: UINT UWM_UART_CTRL_TRS = ::RegisterWindowMessage(_T("BT_UARTCTRL_TRANSFER")); ::SendMessage(HWND_BROADCAST, UWM_UART_CTRL_TRS, 0, 0); and this: UINT UWM_UART_CTRL_TRS = ::RegisterWindowMessage(_T

Passing a C# callback function through Interop/pinvoke

佐手、 提交于 2019-12-25 09:03:37
问题 I am writing a C# application which uses Interop services to access functions in a native C++ DLL. I am already using about 10 different functions which are working. Now I am not sure how to handle passing a callback as a parameter so that the DLL can call my code. Here is the function prototype of the DLL: typedef void (WINAPI * lpfnFunc)(const char *arg1, const char *arg2) And the function that allows me to pass the above type: int WINAPI SetFunc(lpfnFunc f) Here is my C# code for the