marshalling

How to marshall array of structs in C#?

北慕城南 提交于 2019-12-17 19:27:40
问题 I've the following structure in C#: [StructLayoutAttribute(LayoutKind.Sequential)] public struct RECORD { public uint m1; public uint m2; public uint m3; } I need too pass an array (fixed length) of these structs over to native Code, which writes some data to these structures. The array is allocated in C# and passed over to the C dll. I declared the imported function as: [DllImport("marshall.dll", CallingConvention = CallingConvention.Cdecl)] private static extern void doIt(RECORD[]

IntPtr to Byte Array and Back

一世执手 提交于 2019-12-17 19:24:08
问题 Referencing How to get IntPtr from byte[] in C# I am attempting to read the data that an IntPtr is referencing into a byte [] and then back into another IntPtr. The pointer is referencing an image captured from a scanner device so I have also made the assumption that capturing this information should be placed into a byte array. I am also not sure if the Marshal.SizeOf() method will return the size of the data the IntPtr is referencing or the size of the pointer itself. My issue is I am

Getting Array of struct from IntPtr

故事扮演 提交于 2019-12-17 19:19:44
问题 I have some struct like this struct MyStruct { public int field1; public int field2; public int field3; } and I have pointer to array of this struct. So, I need to get array from this pointer. I'm tried to using Marshal.PtrToStructure, but i had memory reading error. This is my methode: public MyStruct[] GetArrayOfStruct(IntPtr pointerToStruct, int length) { var sizeInBytes = Marshal.SizeOf(typeof(TCnt)); MyStruct[] output = new MyStruct[length]; for (int i = 0; i < length; i++) { IntPtr p =

JAXB Marshalling with null fields

梦想的初衷 提交于 2019-12-17 15:36:23
问题 This is a pretty simple request, but I just didn't find a way to do it. I'm basically trying to set up a role in JAXB which says that whenever an null field is encountered, instead of ignoring it in the output, set it to an empty value. So for the class : @XMLRootElement Class Foo { Integer num; Date date; …. } When this has been marshalled into the XML file if the date field is null, my output does not have that element in it. What I want to do is include all the fields in the output; and if

Remove namespace prefix while JAXB marshalling

两盒软妹~` 提交于 2019-12-17 06:41:49
问题 I have JAXB objects created from a schema. While marshalling, the xml elements are getting annotated with ns2. I have tried all the options that exist over the net for this problem, but none of them works. I cannot modify my schema or change package-info.java. Please help 回答1: After much research and tinkering I have finally managed to achieve a solution to this problem. Please accept my apologies for not posting links to the original references - there are many and I wasn't taking notes -

Lowercase JSON key names with JSON Marshal in Go

浪子不回头ぞ 提交于 2019-12-17 04:12:29
问题 I wish to use the "encoding/json" package to marshal a struct declared in one of the imported packages of my application. Eg.: type T struct { Foo int } Because it is imported, all available (exported) fields in the struct begins with an upper case letter. But I wish to have lower case key names: out, err := json.Marshal(&T{Foo: 42}) will result in {"Foo":42} but I wish to get {"foo":42} Is it possible to get around the problem in some easy way? 回答1: Have a look at the docs for encoding/json

Is it possible to get all arguments of a function as single object inside that function?

佐手、 提交于 2019-12-17 03:25:47
问题 In PHP there is func_num_args and func_get_args, is there something similar for JavaScript? 回答1: Use arguments. You can access it like an array. Use arguments.length for the number of arguments. 回答2: The arguments is an array-like object (not an actual array). Example function... function testArguments () // <-- notice no arguments specified { console.log(arguments); // outputs the arguments to the console var htmlOutput = ""; for (var i=0; i < arguments.length; i++) { htmlOutput += '<li>' +

Error: The specified structure must be blittable or have layout information

两盒软妹~` 提交于 2019-12-14 03:56:14
问题 public class TestSerializer { public static byte[] StructureToByteArray(Test[] array) { int size = Marshal.SizeOf(array.Length); byte[] arr = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(array, ptr, true);//error Marshal.Copy(ptr, arr, 0, size); Marshal.FreeHGlobal(ptr); return arr; } When I write the above code I got this error. But my structure is like this : public struct Test { [FieldOffset(0)] // public string name; public Byte icode; [FieldOffset(1)]

Using unmanaged C lib with C#

非 Y 不嫁゛ 提交于 2019-12-14 03:24:52
问题 I'm using a C lib with C#. This is the prototype of the function that I want to use: heatmap_render_default_to(const heatmap_t* h, unsigned char* colorbuf) This function is allocating memory for colorbuf like this: colorbuf = (unsigned char*)malloc(h->w*h->h * 4); to call my function from c# I tried at first to create an unmanaged memory like this: string image = ""; //allocate from COM heap Marshal.StringToCoTaskMemAnsi(image); GCHandle gch = GCHandle.Alloc(image, GCHandleType.Pinned);

How to call a .NET COM method with an array from delphi using PSafeArray?

烈酒焚心 提交于 2019-12-14 03:23:04
问题 I have an .NET (4.0) interface which is implemented with a ServicedComponent COM+ class: interface DotNetIface { void MethodRef(var System.Guid guid); void MethodArray(System.Guid[] guids, params object[] parameters); void MethodCStyle([MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.Struct, SizeConst=5)]System.Guid[] guids); } Now I used the Delphi 2007 import wizard to import the type library, and as expected I get the following signatures: procedure MethodRef(var guid : TGuid);