I have a C++/CLI project using OpenCV. I compiled this version of OpenCV in VS 2010 myself and I can use it in unmanaged projects without an issue — the trouble started when I tried to use it in a managed one.
The function of interest is cv::imread(std::string&, int)
. Simply calling it from a managed module did not work at all, producing <invalid pointer> on the receiving end. I was sort of expecting it. After all, managed code has its own std::string
implementation.
Things got a little more interesting when I created a separate C++ file, removed CLI support from its module, and placed my code in it. Now, imread
was getting a valid pointer, but its contents were scrambled. Apparently, the string
I was passing it contained the string pointer offset by 4 bytes, but it expected it to be at the 0 offset.
The unmanaged module is using the same CRT DLL as OpenCV and has all options set to the values appropriate for normal OpenCV use. Why would it have a different string
layout? I am lost.
Sample code:
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <string>
using namespace cv;
using namespace std;
void Run()
{
string path("C:\\Users\\Don Reba\\Pictures\\Merlin 1D.jpg");
Mat image(imread(path, CV_LOAD_IMAGE_GRAYSCALE));
imwrite("image.jpg", image);
}