Reputation: 81
I have written a function in C++ which takes a Mat class object and returns a Mat after processing. I want to integrate this to a UI written in VB.NET
I wrote this code segment in C++
extern "C" _declspec(dllexport) Mat cropImage(Mat matA){
Mat matB = processingObject.doSomething(matA);
return matB;
}
processingObject.doSomething(matA) works fine.
How Can I use this dll in VB? I don't mind changing the C++ code to make this work.
Thanks
Update >>>>
Found a workaround for the problem. Not 100% What I was looking for though
C++
extern "C" __declspec(dllexport) int* cropImage(char* path, int& size)
{
Mat matA = pavObj.cropped_liquid_region(path);
int rows = matA.rows;
int cols = matA.cols;
int channels = matA.channels();
int length = rows*cols*channels + 3;
size = length;
int* arr = new int[length];
arr[0] = rows;
arr[1] = cols;
arr[2] = channels;
Mat layer[3];
split(matA, layer); // split into color layers BGR
int count = 3;
for(int i=0;i<3;i++){
for(int j=0;j<layer[i].rows;j++){
for(int k=0;k<layer[i].cols;k++){
arr[count] = layer[i].at<uchar>(j, k);
count++;
}
}
}
return arr;
}
extern "C" __declspec(dllexport) int ReleaseMemory(int* pArray)
{
delete[] pArray;
return 0;
}
VB Signatures
<DllImport("mat.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Function cropImage(ByVal path As String, ByRef sz As Integer) As IntPtr
End Function
<DllImport("mat.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Function ReleaseMemory(ByVal ptr As IntPtr) As Integer
End Function
VB call to dll
Dim size As Integer
Dim ptr As IntPtr = cropImage(path, size)
Dim result(size) As Integer
Marshal.Copy(ptr, result, 0, size)
ReleaseMemory(ptr)
So what basically happens is that the content of the image is taken to a 1D array and passed to the managed side where the image is reconstructed again.
Upvotes: 1
Views: 560
Reputation: 5108
You need to use a DllImport call on the C# side to call your
http://msdn.microsoft.com/en-us/library/aa984739(v=vs.71).aspx
How to use <DllImport> in VB.NET?
http://www.codeproject.com/Articles/552348/C-23-2fCplusinteroppluswithplusDllImport
The gist of it is, you need to import your dll like this:
<System.Runtime.InteropService.DllImport("mat.dll", _
SetLastError:=True, CharSet:=CharSet.Auto)> _
<< VB Signature of your cropImage function >>
You will also probably need to do some parameter marshalling to "translate" your Mat type between VB and C++
Upvotes: 1