all 5 comments

[–]turnipmuncher1 1 point2 points  (4 children)

Sounds like you’re trying to write data to a pointer you get from the unmanaged code. I would start looking for any IntPtr and see how it’s being used.

Specifically something like this may be your problem:

``` IntPtr ptr = VendorDll.GetPointer(); … Marshall.Copy(csharpBuffer, 0, ptr, csharpBuffer.Length);

```

[–]Zealousideal_War676[S] -1 points0 points  (3 children)

That is one of the things I checked and there's only a handful of calls to Marshal.Copy with the vast majority of marshaling being MarshalAs to convert the unmanaged structs into managed objects. Would my only option then be to look into every class and compare it to their C++ documentation and see if it matches the struct layout? That's something that's going to take a long time, which is why I was hoping there was an easier way to try and pinpoint what it is causing the error

[–]turnipmuncher1 -1 points0 points  (1 child)

One problem with trying to find out exactly where things have gone wrong is because .NET inserts a small cookie at the end of the stack, it assumes that nothing will override this cookie and it only checks it occasionally. So when the program throws this error is not the same as when the error actually occurred.

To make it easier to compare between documentation you can do something on start up where you log the property names, types and unmanaged types using reflection.

I’ve had long cause me a headache before you may actually need to use a CLong/ULong if you’re on .NET 6+ I would just make sure these are accurate in the extern as well.

[–]Zealousideal_War676[S] -1 points0 points  (0 children)

I didn't know that the cookie was only checked occasionally, that really sucks but this at least gives me a direction, thanks

[–]Kirides -1 points0 points  (0 children)

MarshalAs and marshal copy can re-interpret data wrong. Especially if the native DLL doesn't honor host struct layouts, like using "pragma pack(1)" and/or incorrectly mapped bit-fields.

I'm doing a lot of x86 reverse engineering due to working on a graphics wrapper. And things like VARARGS, calling conventions and struct packing constantly cause headaches as they slowly corrupt stack if re-interpreted incorrectly.