[英]Passing LPTSTR Parameter to DLL gives Access Violation in C++ project
I'm calling a function in an undocumented DLL which unpacks a file. 我在未解压缩的DLL中调用函数,该DLL解压缩文件。
There must be a mistake in the way the header is declared / allocated but can't figure out what is going wrong. 标头的声明/分配方式一定有误,但无法弄清楚出了什么问题。
Project character set in VS 2010 is Unicode. VS 2010中的项目字符集是Unicode。
Can call the DLL function succesfully from C# with the snippet below (but I need to make it work in c++): 可以使用下面的代码片段从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);
If I uncomment of move one the headers an Acccess Violation pops up. 如果我不建议移动标头,则会弹出“访问冲突”。 The function also returns 0 which it doesn't in C#. 该函数还返回0,而C#中没有。
Any thoughts? 有什么想法吗?
Code in the VC++ 2010 project: 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);
That does not match the CallingConvention property of your C# declaration. 这与您的C#声明的CallingConvention属性不匹配。 The default for C# is StdCall, the default for native C++ projects is __cdecl. C#的默认值为StdCall,本机C ++项目的默认值为__cdecl。 Fix: 固定:
typedef void* (__stdcall * UnpackFile)(unsigned char*, int, LPTSTR, int);
And keep in mind that error checking is never optional in C++, you really do need to check if LoadLibrary() and GetProcAddress() succeeded. 而且请记住,错误检查是从来没有在C ++中可选的,你真的需要检查是否调用LoadLibrary(成功)和GetProcAddress()。 Automatic in C#, not in C++. 在C#中是自动的,在C ++中不是自动的。 Both functions return NULL when they fail. 这两个函数均在失败时返回NULL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.