2014-02-12 54 views
1

我已經寫了一個函數在C++中,它需要一個Mat類對象並在處理後返回一個Mat。我想這個集成到寫在VB.NET從C++ DLL返回一個墊到VB.Net

一個UI我寫在C該代碼段++

extern "C" _declspec(dllexport) Mat cropImage(Mat matA){ 
    Mat matB = processingObject.doSomething(matA); 
    return matB; 
} 

processingObject.doSomething(MATA)工作正常。

如何在VB中使用此dll?我不介意改變C++代碼來完成這個工作。

感謝

更新 >>>>

找到該問題的解決方法。不是100%我一直在尋找,雖然

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簽名

<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調用對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) 

所以基本上發生的是,圖像的內容被帶到一維數組,並傳遞到管理方,圖像被重新構建。

回答

1

您需要使用的DllImport調用C#的側打電話給你

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

它的要點是,你需要導入您的DLL是這樣的:

<System.Runtime.InteropService.DllImport("mat.dll", _ 
SetLastError:=True, CharSet:=CharSet.Auto)> _ 
<< VB Signature of your cropImage function >> 

您可能還需要做一些參數編組來「翻譯」您的墊子類型在VB和C++之間

+0

感謝Martin,你能否提供編碼和VB簽名的代碼?我是cpp業餘愛好者,vb互操作。 –