月影流殇 2014-05-30 09:36 采纳率: 0%
浏览 3698

用vb.net(或C#)调用C++编写的DLL时遇到的问题

这部分是在c++中可以正常工作的部分,其声明如下:

extern "C" int LHD_Main(unsigned char *pImage, RECT *rcRoi,int w, int h, char *PlateCharacters, RECT *rectPlateLocation, int *PlateReliability, int nMod);

调用如下

//加载图片
IplImage *image = cvLoadImage(file_name,-1);
if (image == NULL)
{
    printf("image is not exist!\n");
    return -1;
}


//BGR数据
unsigned char *pRawData = (unsigned char *)malloc(image->width*image->height*image->nChannels);

if (NULL == pRawData)
{
    printf("memalloc error\n");
    return -1;
}
int pitch = image->nChannels*image->width;
int width = image->width;
int height = image->height;

//转换成3通道图像
for (int y = 0; y < image->height; y++)
{
    for (int x = 0; x <image->width; x++)
    {
        *(pRawData + y*pitch + 3*x)     = ((unsigned char *)(image->imageData + image->widthStep*y))[3*x];
        *(pRawData + y*pitch + 3*x + 1) = ((unsigned char *)(image->imageData + image->widthStep*y))[3*x+1];
        *(pRawData + y*pitch + 3*x + 2) = ((unsigned char *)(image->imageData + image->widthStep*y))[3*x+2];
    }
}

char cFileName[128];
sprintf_s(cFileName,"%s","京");
cProvince[0] = cFileName[0];
cProvince[1] = cFileName[1];
LHD_Init(LPR_WIDTH, LPR_HEIGHT, cProvince);

//start license plate recognition module,宽高减少1是图像坐标从0开始
RECT recttROI = {0,0,image->width -1, image->height- 1};//设置区域
RECT rectPlate = {0,0,0,0};//输出定位结果
RECT rectTmp  = {0,0,0,0};
int nblePlate = 0;//
char HPHM[128] = {'0'};//
char mHPHM[128] = {'0'};
int  mnblePlate = 0;
bool m_bLPR  = 0;

if(LHD_Main(pRawData,&recttROI,width,height, HPHM, &rectTmp,&nblePlate, i))

这些代码是可以正常工作的。
下面我在VB.NET中如此声明

    <DllImport("LPR.dll", EntryPoint:="LHD_Main", CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Function LHD_Main(ByRef pImage As Byte, ByRef rcRoi As RECT, ByVal w As Int32, ByVal h As Int32, ByRef PlateCharacters As Byte, ByRef rectPlateLocation As RECT, ByRef PlateReliability As Int32, ByVal nMod As Integer) As Int32

End Function


Public Structure RECT
    Dim left As Int32
    Dim top As Int32
    Dim right As Int32
    Dim bottom As Int32
End Structure

调用如下

 Dim cProvince(1) As byte
        cProvince(0) = &HBE
        cProvince(1) = &HA9

        LHD_Init(768, 288, cProvince(0))
        Dim path As String = "d:\pic\rtemp.jpg"
        Dim rImage As IntPtr = Emgu.CV.CvInvoke.cvLoadImage(path, Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_COLOR)
        Dim mImage As Emgu.CV.Structure.MIplImage = CType(System.Runtime.InteropServices.Marshal.PtrToStructure(rImage, GetType(Emgu.CV.Structure.MIplImage)), Emgu.CV.Structure.MIplImage)
        Dim RawData(mImage.width * mImage.height * mImage.nChannels)
        Dim TempData(mImage.imageSize) As Byte

        Marshal.Copy(mImage.imageData, TempData, 0, mImage.imageSize)
        Dim p = mImage.nChannels * mImage.width
        Dim w = mImage.width
        Dim h = mImage.height

        '转换成3通道图像
        For y = 0 To h - 1
            For x = 0 To w - 1
                RawData(y * p + 3 * x) = TempData(3 * x + mImage.widthStep * y)
                RawData(y * p + 3 * x + 1) = TempData(3 * x + mImage.widthStep * y + 1)
                RawData(y * p + 3 * x + 2) = TempData(3 * x + mImage.widthStep * y + 2)
            Next
        Next
        Dim recttROI As New RECT
        recttROI.left = 0
        recttROI.top = 0
        recttROI.right = mImage.width - 1
        recttROI.bottom = mImage.height - 1

        Dim rectPlate As New RECT
        Dim rectTmp As New RECT
        Dim mnblePlate As Integer = 0
        Dim HPHM(127) As Byte
        Dim rw As Integer = mImage.width
        Dim rh As Integer = mImage.height

        For i = 0 To 8
            Dim result As Integer = LHD_Main(RawData(0), recttROI, rw, rh, HPHM(0), rectTmp, mnblePlate, i)

运行后在LHD_Main处报错: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

求问怎么解决!

  • 写回答

3条回答 默认 最新

  • oyljerry 2015-01-24 09:29
    关注

    注意参数是否正确,尤其类型。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog