C # to C code. P / INvoke. Several std: string declarations damage the stack.

I built a C ++ dll with external export to call it from my C # program. The call works very well for most functions, but there is a problem when I need to pass some lines from C # to C ++.

I pass them as a normal string and get them as const char *. They are all beautiful, and all the data is there, but then I continue to define a couple of lines from these char arrays. The code continues without any problems until I exit the function. Then it throws an exception saying that the stack around the last defined std :: string is corrupted, and I'm really not sure why that is. I tried many ways to define strings: copying them, changing the encoding from the P / Invoke definition.

Additional Information I call this function from the timer thread; I mention this because I found that some problems may occur with std :: string in streams. This is done on a machine with multiple processors built in VS 2012 for x86. Also add the appropriate code below

#define OPENCV2P4 #include "openfabmap.hpp" #include <fstream> #ifdef OPENCV2P4 #include <opencv2/nonfree/nonfree.hpp> #endif #include <stdio.h> extern "C" { __declspec(dllexport) int generateBOWImageDescs( const char* _dataPath, const char* _bowImageDescPath,const char* _vocabPath ,int minWords) { std::string dataPath(_dataPath); std::string bowImageDescPath( _bowImageDescPath); std::string vocabPath(_vocabPath); cv::FileStorage fs; //ensure not overwriting training data std::ifstream checker; checker.open(bowImageDescPath.c_str()); if(checker.is_open()) { checker.close(); return -1; } } 

And a call from C #

  [DllImport(@"FabMap\FabMapCPP.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int generateBOWImageDescs( string _dataPath, string _bowImageDescPath, string _vocabPath, int minWords); 

Any help would be greatly appreciated. Also the first question is here, so any comments about the misrepresentation of the question will be appreciated.

Edit: Well, the problem was related to $ cv :: FileStorage fs; I deleted it essentially and started working, possibly due to improper placement or something else.

Thus, this means that the opencv file vault is causing the problem, but not sure why.

+4
source share
1 answer

Look at this 2 links:

http://support.microsoft.com/kb/311259

http://msdn.microsoft.com/en-us/library/7b620dhe.aspx

Basically char * is an IntPtr in C #, not a string.

-1
source

Source: https://habr.com/ru/post/1443843/


All Articles