月影流殇 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 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划