I have a nuget formed from a C++ project - inside that nuget I have an extern method GetBytes inside of a static class FileAccess.cs that I call in another project's (C#) REST service call to retrieve some data. Whenever I call this GetBytes extern method from my C# REST service I get an AccessViolationException for some reason. The method takes in a two strings and several int values and returns an IntPtr.
I'm confused because I have a similar call that does not result in a violation. The key difference is that this version does not call the extern method directly. Instead, it calls a C# DataGroup class (which is part of the nuget) which contains a function that calls the extern method. The DataGroup class method returns a byte[,], but only after it calls the extern method and marshals the data.
// Version resulting in AccessViolation:
- REST call to extern method as
IntPtr(IntPtr ptr = FileAccess.getBytes(...); - extern method returns an
IntPtr AccessViolationExceptionoccurs
// Working version with extra class:
- REST call to data group method that returns a
byte[,]. (e.g.dataGroupInstance.getDataGroupBytes(); - extern method is called inside method
getDataGroupBytes - extern method returns an
IntPtr - method
getDataGroupBytesmarshals the data fromIntPtrand returns abyte[,] - no error
Basically, why is calling the extern method directly resulting in an AccessViolationException?
Figured it out. The problem was a result of my parameters - my C++ functions used
std::stringwhen I should have been usingconst char*. I replaced all instances ofstd::string(where they were used as parameters) withconst char*and I no longer received the exception.