2 neverwind neverwind 于 2014.05.30 17:36 提问

用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处报错: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

求问怎么解决!

2个回答

oyljerry
oyljerry   Ds   Rxr 2015.01.24 17:29

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

baidu_30011359
baidu_30011359   2015.07.21 20:48

多数是编译上的问题,你的DLL是x86的,所以要设置:编译> 高级编译选项> 目标CPU ,设置为 X86 就可以解决问题了。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!