C#调用C++ dll的问题,
尝试读取或写入受保护的内存,这通常指示其他内存已损坏
用C++封装的DLL,其中函数用到结构,C++中的函数如下:
extern "C " bool __stdcall SaveBMPFile(LPCSTR lpFileName, BITMAPINFO* pBmpInfo, BYTE *pImageBuffer)
{
BOOL bRVal = FALSE;
DWORD dwBytesRead = 0;
DWORD dwSize = 0;
BITMAPFILEHEADER bfh = {0};
int nTable = 0;
DWORD dwImageSize = 0;
if (pBmpInfo-> bmiHeader.biBitCount > 8) {
nTable = 0;
}
else{
nTable = 256;
}
dwImageSize =(((pBmpInfo-> bmiHeader.biWidth * pBmpInfo-> bmiHeader.biBitCount) + 31) / 32 * 4 )* pBmpInfo-> bmiHeader.biHeight;
if (dwImageSize <= 0) {
bRVal = FALSE;
MessageBox(NULL, "dwImageSize <= 0 ",NULL,MB_OK);
}
else{
bfh.bfType = (WORD) 'M ' < < 8 | 'B ';
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nTable * sizeof(RGBQUAD);
bfh.bfSize = bfh.bfOffBits + dwImageSize;
HANDLE hFile = ::CreateFile(lpFileName,
GENERIC_WRITE ,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hFile == INVALID_HANDLE_VALUE) {
bRVal = FALSE;
MessageBox(NULL, "hFile= 0 ",NULL,MB_OK);
}
else{
dwSize = sizeof(BITMAPFILEHEADER);
::WriteFile(hFile, &bfh, dwSize, &dwBytesRead, NULL );
dwSize = sizeof(BITMAPINFOHEADER) + nTable * sizeof(RGBQUAD);
::WriteFile(hFile, pBmpInfo, dwSize, &dwBytesRead, NULL );
dwSize = dwImageSize;
WriteFile(hFile, pImageBuffer, dwSize, &dwBytesRead, NULL );
CloseHandle(hFile);
MessageBox(NULL, "Writefile= 0 ",NULL,MB_OK);
}
}
return bRVal;
}
----------------------------------
C#中声明的相关结构:
[StructLayout(LayoutKind.Sequential)]
public struct BITMAPFILEHEADER
{
public ushort bfType;
public int bfSize;
public