unsafe

Map C struct with union field to Go struct

倖福魔咒の 提交于 2019-12-08 00:31:27
问题 I'm getting results from syscall to some WinApi in Go. I map simple structs from C code easily, but how to deal with C structs like the following? typedef struct SPC_LINK_ { DWORD dwLinkChoice; # define SPC_URL_LINK_CHOICE 1 # define SPC_MONIKER_LINK_CHOICE 2 # define SPC_FILE_LINK_CHOICE 3 union { LPWSTR pwszUrl; SPC_SERIALIZED_OBJECT Moniker; LPWSTR pwszFile; }; } SPC_LINK, *PSPC_LINK; If all possible types are defined in Go type SPC_LINK struct { dwLinkChoice DWORD Moniker SPC_SERIALIZED

Is GCHandleType.Pinned similar to using “fixed” keyword?

半世苍凉 提交于 2019-12-07 10:53:56
问题 I'm experimenting with IntPtr in "safe" code, comparing it to how things are done in the "unsafe" mode. Is GCHandleType.Pinned similar to using "fixed" in unsafe mode? GCHandle pinnedArray = GCHandle.Alloc(byteArray, GCHandleType.Pinned); IntPtr pointer = pinnedArray.AddrOfPinnedObject(); //do your stuff pinnedArray.Free(); vs byte[] buffer = new byte[255]; fixed (byte* p = buffer) { IntPtr ptr = (IntPtr)p; // do you stuff here } 回答1: Yes, the result is the same. The difference is in the

How to create an array or a slice from an array unsafe.Pointer in golang?

心不动则不痛 提交于 2019-12-07 05:04:22
问题 A pointer to an array, let's say: p := uintptr(unsafe.Pointer(&array)) size := 5 I can't access to the variable array , the code above is used to make it more clear. Also, I know the size of the array, but the size is not constant, it changes according to the runtime. Now, I want to initialize a slice or an array with the known pointer, size and of course the data type. I come up with the following code: data := make([]byte, size) stepSize := unsafe.Sizeof(data[0]) for i := 0; i < size; i++ {

Unsafe code to change length (by mutation!) of a String object?

此生再无相见时 提交于 2019-12-07 03:54:12
问题 As .NET doesn't use C style null s to end a string how can I keep the allocated string but change the length of it by using unsafe code? As I understand .NET using a 20 bytes header for every string, presumably this is where the length of the string is stored, is there anyway to directly modify this length? So .NET will keep the string in memory but when I call .Length it'll return the .Length I want. if this is possible, also it would be interesting to hear all crazy possible side-effects of

Unity3D Unsafe code requires the `unsafe' command line option to be specified

我与影子孤独终老i 提交于 2019-12-07 02:27:22
问题 i am using Unity3D 4.3 and calling a DLL that i created. when trying to call the only function it have which is this: void GetModelReferences(int &nVertices, float * vertices, int &nTriangles, int * triangles, float * normals, float * uvCoordinates); unity gives me an error: Unsafe code requires the `unsafe' command line option to be specified so in my MonoDevelop i opened : Project->Assembly-Csharp options and turned on the unsafe mode. it reduces part of the errors but this last one wont go

Should I mingle my safe code with my unsafe code?

二次信任 提交于 2019-12-07 01:26:12
问题 I'm working on a project that uses a bunch of WIN32 API calls and requires some unsafe code. From a best practices standpoint should I isolate this code in its own DLL compiled with /unsafe switch while keeping my main application safe? To put it another way. Is there any reason not to compile a project with the /unsafe switch? Are there any potential risks? 回答1: An assembly is by definition the smallest unit of independently versionable, redistributable code in .NET. Therefore the first

Fixed Size Array of Structure type

痴心易碎 提交于 2019-12-06 21:36:10
问题 how do I declare fixed-size array of a structure type in C# : [StructLayout(LayoutKind.Sequential,Pack=1), Serializable] public unsafe struct MyStruct{ ... } public class MyClass { ... public fixed MyStruct myStruct[256]; } this will result to CS1663 : fixed size buffers of struct type is not allowed, how do I workaround this ?, I prefer not to use C# or "Managed Collection data structure" type, as I need to frequently marshall this to native C++ 回答1: If your C# struct uses only primitive

How can I display a pointer address in C#?

有些话、适合烂在心里 提交于 2019-12-06 17:27:49
问题 I've not done any pointers since I've been programming in C# - and my C++ days were long ago. I thought I should refresh my knowledge and was just playing around with them because of another question on here. I understand them all okay, but I can't figure out how to write the pointer's address to the console... char c = 'c'; char d = 'd'; char e = 'e'; unsafe { char* cp = &d; //How do I write the pointer address to the console? *cp = 'f'; cp = &e; //How do I write the pointer address to the

How to test handling of AccessViolationException

青春壹個敷衍的年華 提交于 2019-12-06 17:14:01
问题 I need to write a test which verifies that my code can handle an AccessViolationException (or any other WIN32 Corrupted State Exception - CSE), which occours in an unsafe context, typically by calling a 3rd party lib. This should all be done using C# on .net 4.0. I found this related question How to handle AccessViolationException and this related article http://dotnetslackers.com/articles/net/All-about-Corrupted-State-Exceptions-in-NET4.aspx, which explains how to catch these CSE's and their

Map C struct with union field to Go struct

我的未来我决定 提交于 2019-12-06 10:10:17
I'm getting results from syscall to some WinApi in Go. I map simple structs from C code easily, but how to deal with C structs like the following? typedef struct SPC_LINK_ { DWORD dwLinkChoice; # define SPC_URL_LINK_CHOICE 1 # define SPC_MONIKER_LINK_CHOICE 2 # define SPC_FILE_LINK_CHOICE 3 union { LPWSTR pwszUrl; SPC_SERIALIZED_OBJECT Moniker; LPWSTR pwszFile; }; } SPC_LINK, *PSPC_LINK; If all possible types are defined in Go type SPC_LINK struct { dwLinkChoice DWORD Moniker SPC_SERIALIZED_OBJECT pwszFile LPWSTR pwszUrl LPWSTR } after the syscall with unsafe.Pointer to this Go struct as a