I am trying to get an unmanaged C++ DLL to work with my C# project, when running the program through debugger in VS2019 the code works. When running without debugger the app crashes when ran and event viewer gives me Exception Info: System.AccessViolationException when calling C++ Dll
I have tried changing the way I have used p/invoke with variations on what I have though might make it work. C++ isnt my forte but the DLL isnt anything complex and is just an if statement returning a string.
Is anyone able to see where I have gone wrong here? Or offer any tips on how to troubleshoot this issue?
C#
[DllImport("unmanaged.dll", EntryPoint = "checkCPU", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr checkCPU();
// to call it
string check = Marshal.PtrToStringAnsi(checkCPU());
C++
extern "C" __declspec(dllexport) std::string checkCPU()
{
if (InstructionSet::SSE41() || InstructionSet::SSE42()) {
return "true";
}
else {
return "false";
}
}
(I know this has been asked a few times already, after rummaging round the internet I havent found anything that could help me.)
Edit
Both the DLL and project are x86, with the same netframework