marshalling

PInvoke error when marshalling struct with a string in it

匆匆过客 提交于 2019-12-18 09:00:36
问题 I have a C++ struct struct UnmanagedStruct { char* s; // Other members }; and a C# struct struct ManagedStruct { [MarshalAs(UnmanagedType.LPStr)] string s; // Other members } the C++ library exposes extern "C" UnmanagedStruct __declspec(dllexport) foo( char* input ); And it is imported like [DllImport("SomeDLL.dll", CharSet = CharSet.Ansi)] static extern ManagedStruct foo( string input ); However when I call this function I get MarshalDirectiveException was unhandled Method's type signature

Fast copy of Color32[] array to byte[] array

浪子不回头ぞ 提交于 2019-12-18 06:57:00
问题 What would be a fast method to copy/convert an array of Color32[] values to a byte[] buffer? Color32 is a struct from Unity 3D containing 4 bytes, R, G, B and A respectively . What I'm trying to accomplish is to send the rendered image from unity through a pipe to another application ( Windows Forms ). Currently I'm using this code: private static byte[] Color32ArrayToByteArray(Color32[] colors) { int length = 4 * colors.Length; byte[] bytes = new byte[length]; IntPtr ptr = Marshal

Improper marshaling: C# array to a C++ unmanaged array

自作多情 提交于 2019-12-18 05:59:13
问题 I have the following C# code with a structure definition (CInput), obj definition and init, and a call to a C++ (native) DLL function (that has also been written by me). //C# code public struct CInput { [MarshalAsAttribute(UnmanagedType.R8)] public double Time; [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_R8)] public double[] Database; /* other similar fields*/ } CInput Inputs = new CInput(); /* init of Inputs fields*/ int bfr = Example(ref Inputs); //'Example' being the

EclipseLink MOXy JSON Serialization

☆樱花仙子☆ 提交于 2019-12-18 05:51:36
问题 I have got a sample class: class Zoo { public Collection<? extends Animal> animals; } When serialized with MOXy, I am getting: { "bird": [ { "name": "bird-1", "wingSpan": "6 feets", "preferredFood": "food-1" } ], "cat": [ { "name": "cat-1", "favoriteToy": "toy-1" } ], "dog": [ { "name": "dog-1", "breed": "bread-1", "leashColor": "black" } ] } Why is it using array indicators "[]", while bird, cat, and dog are not arrays? Second, is there a way to get rid of "bird", "cat", and "dog"? In other

How to write to file when using Marshal::dump in Ruby for object serialization

自闭症网瘾萝莉.ら 提交于 2019-12-18 04:30:11
问题 Lets say I have the object line from class Line : class Line def initialize point1, point2 @p1 = point1 @p2 = point2 end end line = Line.new... How can I binary serialize the line object? I tried with: data = Marshal::dump(line, "path/to/still/unexisting/file") but it created file and didn't add anything. I read the Class: IO documentation but I couldn't really get it. 回答1: Like this: class Line attr_reader :p1, :p2 def initialize point1, point2 @p1 = point1 @p2 = point2 end end line = Line

Copy array to struct array as fast as possible in C#

我的未来我决定 提交于 2019-12-18 04:16:25
问题 I am working with Unity 4.5, grabbing images as bytes arrays (each byte represent a channel, taking 4 bytes per pixel (rgba) and displaying them on a texture converting the array to a Color32 array, using this loop: img = new Color32[byteArray.Length / nChannels]; //nChannels being 4 for (int i=0; i< img.Length; i++) { img[i].r = byteArray[i*nChannels]; img[i].g = byteArray[i*nChannels+1]; img[i].b = byteArray[i*nChannels+2]; img[i].a = byteArray[i*nChannels+3]; } Then, it is applied to the

Marshal.PtrToStructure throwing System.ArgumentException error

冷暖自知 提交于 2019-12-18 04:04:34
问题 I'm attempting to get a KBDLLHOOKSTRUCT from a keyboard-hook's lParam. private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { KBDLLHOOKSTRUCT kbd = new KBDLLHOOKSTRUCT(); Marshal.PtrToStructure(lParam, kbd); // Throws System.ArguementException ... Unfortunately the PtrToStructure is throwing two A first chance exception of type 'System.ArgumentException' occurred in myprogram.exe errors every time a key is pressed. It also stops that method in its tracks. MSNDA says:

How do I marshal a pointer to an array of pointers to structures?

本小妞迷上赌 提交于 2019-12-18 04:04:00
问题 I have a C function with the following signature: int my_function(int n, struct player **players) players is a pointer to an array of pointers to struct player objects. n is the number of pointers in the array. The function does not modify the array nor the contents of the structures, and it does not retain any pointers after returning. I tried the following: [DllImport("mylibary.dll")] static extern int my_function(int n, [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] player_in [

C# Marshalling double* from C++ DLL?

ⅰ亾dé卋堺 提交于 2019-12-18 02:41:29
问题 I have a C++ DLL with an exported function: extern "C" __declspec(dllexport) double* fft(double* dataReal, double* dataImag) { [...] } The function calculates the FFT of the two double arrays (real and imaginary) an returns a single double array with the real an imaginary components interleaved: { Re, Im, Re, Im, ... } I'm not sure how to call this function in C#. What I'm doing is: [DllImport("fft.dll")] static extern double[] fft(double[] dataReal, double[] dataImag); and when I test it

How do I marshal wchar_t* from C++ to C# as an out parameter or return value?

耗尽温柔 提交于 2019-12-17 19:29:07
问题 I have tried to do this in many ways, but none is working. Does anyone have a correct example for this? I just want to move the wchar_t* value from a function to the C# level. 回答1: This isn't as difficult as you think it is... What is wchar_t* ? What value does that type typically represent? A string. It's the equivalent to the LPWSTR type defined in windows.h . So, you marshal it as a string type. However, since it's an out parameter (or a return value), you'll need to use the StringBuilder