月影流殇 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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置