marshalling

Can I marshal a C-struct with a 2d array without using “unsafe”?

给你一囗甜甜゛ 提交于 2021-02-07 21:10:09
问题 I have a C DLL that I'm writing a C# interop class for. In the C DLL, one of the key methods fills a 2d structure; the structure is allocated and freed by helper methods, like so: // Simple Struct Definition -- Plain Old Data typedef struct MyPodStruct_s { double a; double b; } MyPodStruct; typedef struct My2dArray_s { MyPodStruct** arr; // allocated by Init2d; // array of arrays. // usage: arr[i][j] for i<n,j<m int n; int m; } My2dArray; void Init2d(My2dArray* s, int n, int m); void Free2d

Read and merge two Yaml files

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-07 20:40:25
问题 Assuming we have two yaml files master.yaml someProperty: "someVaue" anotherProperty: "anotherValue" override.yaml someProperty: "overriddenVaue" Is it possible to unmarshall, merge, and then write those changes to a file without having to define a struct for every property in the yaml file? The master file has over 500 properties in it that are not at all important to the service at this point of execution, so ideally I'd be able to just unmarshal into a map, do a merge and write out in yaml

Read and merge two Yaml files

旧城冷巷雨未停 提交于 2021-02-07 20:38:20
问题 Assuming we have two yaml files master.yaml someProperty: "someVaue" anotherProperty: "anotherValue" override.yaml someProperty: "overriddenVaue" Is it possible to unmarshall, merge, and then write those changes to a file without having to define a struct for every property in the yaml file? The master file has over 500 properties in it that are not at all important to the service at this point of execution, so ideally I'd be able to just unmarshal into a map, do a merge and write out in yaml

C# marshal unmanaged pointer return type

馋奶兔 提交于 2021-02-07 11:59:52
问题 I have an unmanaged library which has a function like this: type* foo(); foo basically allocates an instance of the unmanaged type on the managed heap through Marshal.AllocHGlobal . I have a managed version of type . It's not blittable but I have MarshalAs attributes set on members so I can use Marshal.PtrToStructure to get a managed version of it. But having to wrap calls to foo with extra bookkeeping to call Marshal.PtrToStructure is a bit annoying. I'd like to be able to do something like

How to pin memory allocated by Marshal.AllocHGlobal() in C#?

 ̄綄美尐妖づ 提交于 2021-02-05 11:22:10
问题 How do you pin memory allocated by Marshal.AllocHGlobal()? My first attempt was the following: int bytes = 10; IntPtr ip = Marshal.AllocHGlobal(bytes); GCHandle iph = GCHandle.Alloc(ip, GCHandleType.Pinned); Although I think this only pins the IntPtr and not the block of memory referred to by the IntPtr . 回答1: The memory allocated by AllocHGlobal is already pinned. the IntPtr that is returned is the address of the pinned location. UPDATE: To be pedantic you can't actually "pin" the memory

Invalid Managed/Unmanaged Type Combination With Embedded, Dynamically-Allocated Array

随声附和 提交于 2021-02-05 07:09:48
问题 I have a common construct in an unmanaged Win32 C++ DLL: // FirstElemPtrContainer.h #include "stdafx.h" typedef unsigned char elem_type; // a byte typedef struct FirstElemPtrContainer { unsigned char num_elems; void *allocd_ary; } FirstElemPtrContainer; The void* in the struct is meant to contain a pointer to the first element of an allocated byte array. The DLL that uses this definition then exports functions to allocate, populate, and deallocate the struct: // The exported allocator

Invalid Managed/Unmanaged Type Combination With Embedded, Dynamically-Allocated Array

回眸只為那壹抹淺笑 提交于 2021-02-05 07:09:32
问题 I have a common construct in an unmanaged Win32 C++ DLL: // FirstElemPtrContainer.h #include "stdafx.h" typedef unsigned char elem_type; // a byte typedef struct FirstElemPtrContainer { unsigned char num_elems; void *allocd_ary; } FirstElemPtrContainer; The void* in the struct is meant to contain a pointer to the first element of an allocated byte array. The DLL that uses this definition then exports functions to allocate, populate, and deallocate the struct: // The exported allocator

Invalid Managed/Unmanaged Type Combination With Embedded, Dynamically-Allocated Array

冷暖自知 提交于 2021-02-05 07:06:29
问题 I have a common construct in an unmanaged Win32 C++ DLL: // FirstElemPtrContainer.h #include "stdafx.h" typedef unsigned char elem_type; // a byte typedef struct FirstElemPtrContainer { unsigned char num_elems; void *allocd_ary; } FirstElemPtrContainer; The void* in the struct is meant to contain a pointer to the first element of an allocated byte array. The DLL that uses this definition then exports functions to allocate, populate, and deallocate the struct: // The exported allocator

PtrToStringUni doesnt work in windows 10

守給你的承諾、 提交于 2021-01-29 14:12:54
问题 So i have been using urlmon.dll 's help with getting the MIME type of files' data as suggested in This answer, and its been working fine in windows 7. However, on windows 10 the same code generates System.AccessViolationException when trying to create a string from the mime pointer. this is the problematic code: uint mimeType; FindMimeFromData(0, null, data, 256, null, 0, out mimeType, 0); var mimePointer = new IntPtr(mimeType); //Exception is thrown on the next line var mime = Marshal

How can I override json tags in a Go struct?

霸气de小男生 提交于 2021-01-28 06:16:24
问题 I'd like to marshal part of this struct: type ValueSet struct { Id string `json:"id" bson:"_id"` Url string `bson:"url,omitempty" json:"url,omitempty"` Identifier *Identifier `bson:"identifier,omitempty" json:"identifier,omitempty"` Version string `bson:"version,omitempty" json:"version,omitempty"` Name string `bson:"name,omitempty" json:"name,omitempty"` Status string `bson:"status,omitempty" json:"status,omitempty"` Experimental *bool `bson:"experimental,omitempty" json:"experimental