- Home /
unity creashes when using a pinvoke with input string
hi,
I'm using unity 2019.2.13f1
I have created a c++ dll that I'm using in my unity project.
I've created a method that has const char* (string) as input in the c++:
extern "C"
{
__declspec(dllexport) double __stdcall get_au(const char* au) {
for (auto reg : *au_reg1) {
if (reg.first.compare(au) == 0)
return reg.second;
}
return -1;
}
}
when I'm sending it from my test c# project it is working fine.
but when I'm trying to send from my unity project, the process crashes.
this is my c# declaration:
[DllImport("au_FeatureExtraction.dll", EntryPoint = "get_au", CallingConvention = CallingConvention.Cdecl)]
static extern double get_au(string au);
I have other functions that uses the same DLL with\without in parameters and it works fine for that. (example for other method that works):
c++:
extern "C"
{
__declspec(dllexport) int __stdcall getdetection_success() {
return detection_success;
}
}
c#:
[DllImport("FeatureExtraction.dll", EntryPoint = "getdetection_success", CallingConvention = CallingConvention.Cdecl)]
static extern int getdetection_success();
Answer by Bunny83 · Jun 24, 2020 at 10:42 AM
Well, the issues is most likely in your C++ code. You haven't told us what "au_reg1" actually points to. Since you dereference it it should be a pointer to any supported collection for an auto for loop. Are you actually sure that this pointer is not null? Where and when is it initialized? We're completely blind here. We know nothing about the context.
Also not totally irrelevant would be to know the build target, scripting backend and .NET compatibility level.
Your answer
![](https://koobas.hobune.stream/wayback/20220613001646im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
c# Using an IEnumerator yield WaitForSeconds to temporarily pause a While loop 3 Answers
Unity crash when deleting list 0 Answers