tagged [marshalling]

Marshal.PtrToStructure throwing System.ArgumentException error

Marshal.PtrToStructure throwing System.ArgumentException error I'm attempting to get a KBDLLHOOKSTRUCT from a keyboard-hook's lParam. Unfortunately the PtrToS

23 Jan at 19:26

Passing a Structure to C++ API using Marshal.StructureToPtr in C#

Passing a Structure to C++ API using Marshal.StructureToPtr in C# I am using API written in C++ in my code (writting in C#). API requires a parameter as Pointer to Structure. The Structure consists of...

26 Jun at 19:6

P/Invoke, c#: unsigned char losing a byte

P/Invoke, c#: unsigned char losing a byte Im working towards a dll file for a software's SDK and i'm trying to call a function to get information about the host of the software. there are two unsigned...

7 Jan at 09:54

Do I need to delete structures marshaled via Marshal.PtrToStructure in unmanaged code?

Do I need to delete structures marshaled via Marshal.PtrToStructure in unmanaged code? I have this C++ code: Then in C# I call the function thus: ``` [DllImport("MyDll.dll")] static extern void Alloca...

30 Jan at 22:8

Convert array of structs to IntPtr

Convert array of structs to IntPtr I am trying to convert an array of the RECT structure (given below) into an IntPtr, so I can send the pointer using PostMessage to another application. ``` [StructLa...

6 Jul at 11:33

Can I use SafeHandle instead of IntPtr?

Can I use SafeHandle instead of IntPtr? I've searched the internet far and wide but didn't find a good explanation. My question is pretty simple. I have a DLL which has a function called Initialize an...

15 Aug at 16:22

The fastest way to check if a type is blittable?

The fastest way to check if a type is blittable? In my serialiser/deserialiser, I have the following snippet: ``` if (element_type.IsValueType && collection_type.IsArray) { try { GCHan...

Correct way to marshal SIZE_T*?

Correct way to marshal SIZE_T*? I have the following C++ function definition, which I am trying to call through PInvoke from managed code: My managed declaration looked as follows: Some of you may not...

21 Aug at 00:13

How can I marshall a vector<int> from a C++ dll to a C# application?

How can I marshall a vector from a C++ dll to a C# application? I have a C++ function that produces a list of rectangles that are interesting. I want to be able to get that list out of the C++ library...

30 Apr at 20:47

Marshal C++ struct array into C#

Marshal C++ struct array into C# I have the following struct in C++: And a function that I'm p/invoking into to get an array of 3 of these structures: In C++ I would just do something like this: ``` L...

9 Oct at 17:27

Marshal.SizeOf throws ArgumentException on enums

Marshal.SizeOf throws ArgumentException on enums Consider this code: it throws the exception: > An unhandled exception of type 'System.ArgumentException' occurred in TestConsole.exeAdditional informa...

27 Dec at 10:37

Reading a C/C++ data structure in C# from a byte array

Reading a C/C++ data structure in C# from a byte array What would be the best way to fill a C# struct from a byte[] array where the data was from a C/C++ struct? The C struct would look something like...

Detecting cross-thread marshaling by COM RCW objects in C#

Detecting cross-thread marshaling by COM RCW objects in C# I'm working in a large multithreaded C# application handling bunches of COM interop. The other developers and I have ample opportunity to acc...

How do I marshall a pointer to a pointer of an array of structures?

How do I marshall a pointer to a pointer of an array of structures? My C declarations are as follows: My C# declarations are as follows: ``` [DllImport("myData.dll", EntryPoint = "myData")] public

30 May at 19:35

Runtime callable wrapper (RCW) scope - process or application domain?

Runtime callable wrapper (RCW) scope - process or application domain? What is the scope of Runtime Callable Wrapper (RCW), when referencing unmanaged COM objects? According to the docs: > The runtime ...

C# Marshalling double* from C++ DLL?

C# Marshalling double* from C++ DLL? I have a C++ DLL with an exported function: The function calculates the FFT of the two double arrays (real and imaginary) an returns a single double array with the...

21 Feb at 23:46

Non-blittable error on a blittable type

Non-blittable error on a blittable type I have this struct and this code: ``` [StructLayout(LayoutKind.Sequential, Pack = 8)] private class xvid_image_t { [MarshalAs(UnmanagedType.ByValArray, SizeCo...

Conversion in .net: Native Utf-8 <-> Managed String

Conversion in .net: Native Utf-8 Managed String I created those two methods to convert Native utf-8 strings (char*) into managed string and vice versa. The following code does the job: ``` public IntP...

27 May at 12:19

How to return a vector of structs from Rust to C#?

How to return a vector of structs from Rust to C#? How is it possible to write Rust code like the C code below? This is my Rust code so far, without the option to marshal it: ``` pub struct PackChar {...

How to pass C# array to C++ and return it back to C# with additional items?

How to pass C# array to C++ and return it back to C# with additional items? I have a C# project which is using a C++ dll. (in visual studio 2010) I have to pass a array of int from C# code to C++ func...

17 May at 15:31

C# to C++ process with WM_COPYDATA passing struct with strings

C# to C++ process with WM_COPYDATA passing struct with strings From a c# program I want to use WM_COPYDATA with SendMessage to communicate with a legacy c++/cli MFC application. I want to pass a manag...

Marshalling .NET generic types

Marshalling .NET generic types Here is a C# program that tries `Marshal.SizeOf` on a few different types: ``` using System; using System.Runtime.InteropServices; [StructLayout(LayoutKind.Sequential)] ...

17 Oct at 17:24

WinApi - GetLastError vs. Marshal.GetLastWin32Error

WinApi - GetLastError vs. Marshal.GetLastWin32Error But I found no disadvantages of those 2! But see the accepted answer. --- I read [here](http://blogs.msdn.com/b/adam_nathan/archive/2003/04/25/56643...

How can I prevent CompileAssemblyFromSource from leaking memory?

How can I prevent CompileAssemblyFromSource from leaking memory? I have some C# code which is using CSharpCodeProvider.CompileAssemblyFromSource to create an assembly in memory. After the assembly has...

Marshal.PtrToStructure (and back again) and generic solution for endianness swapping

Marshal.PtrToStructure (and back again) and generic solution for endianness swapping I have a system where a remote agent sends serialized structures (from an embedded C system) for me to read and sto...