![](/img/trans.png)
[英]Calling C DLL from C++ gives Access Violation but C# project with DllImport working
[英]Passing LPTSTR Parameter to DLL gives Access Violation in C++ project
我在未解壓縮的DLL中調用函數,該DLL解壓縮文件。
標頭的聲明/分配方式一定有誤,但無法弄清楚出了什么問題。
VS 2010中的項目字符集是Unicode。
可以使用下面的代碼片段從C#成功調用DLL函數(但我需要使其在c ++中工作):
[DllImport("unpacker.dll", EntryPoint = "UnpackFile", PreserveSig = false)]
internal static extern IntPtr UnpackFile(byte[] file, int fileSize,
[MarshalAs(UnmanagedType.LPWStr)] StringBuilder header, int headerSize);
如果我不建議移動標頭,則會彈出“訪問沖突”。 該函數還返回0,而C#中沒有。
有什么想法嗎?
VC ++ 2010項目中的代碼:
// unpacker.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <fstream>
using namespace std;
typedef void* (*UnpackFile)(unsigned char*, int, LPTSTR, int);
int _tmain(int argc, _TCHAR* argv[])
{
LPTSTR header;
//Move this and get a access violation on the _UnpackFile(filetounpack... line
static unsigned char *filetounpack; //Buffer to byte array with the file to unpack
int filelen; //variable to store the length of the file
HINSTANCE dllHandle; // Handle to DLL
UnpackFile _UnpackFile; // Function pointer
ifstream filetoread; //Stream class to read from files
static LPTSTR header2; //Buffer for the header 2nd
filetoread.open ("c:/projects/testfile.bin", ios::in | ios::binary|ios::ate);
filelen = filetoread.tellg(); //read the length
filetounpack = new unsigned char [filelen]; //allocate space
filetoread.seekg (0, ios::beg); //set beginning
filetoread.read ((char *)filetounpack, filelen); //read the file into the buffer
filetoread.close(); //close the file
dllHandle = LoadLibrary(_T("unpacker.dll"));
_UnpackFile = (UnpackFile)GetProcAddress(dllHandle, "UnpackFile");
//header = new _TCHAR[filelen]; //Allocate memory for header
header2 = new _TCHAR[filelen]; //Allocate memory for header
//Access violation reading location 0xffffffffffffffff!!!
void* tmp = _UnpackFile(filetounpack ,filelen ,header2 ,filelen);
delete[] filetounpack;
delete[] header;
delete[] header2;
FreeLibrary(dllHandle);
return 0;
}
typedef void* (*UnpackFile)(unsigned char*, int, LPTSTR, int);
這與您的C#聲明的CallingConvention屬性不匹配。 C#的默認值為StdCall,本機C ++項目的默認值為__cdecl。 固定:
typedef void* (__stdcall * UnpackFile)(unsigned char*, int, LPTSTR, int);
而且請記住,錯誤檢查是從來沒有在C ++中可選的,你真的需要檢查是否調用LoadLibrary(成功)和GetProcAddress()。 在C#中是自動的,在C ++中不是自動的。 這兩個函數均在失敗時返回NULL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.