c++ - 将 LPTSTR 参数传递给 DLL 会在 C++ 项目中产生访问冲突

标签 c++ dll

我在一个未记录的 DLL 中调用一个函数来解压一个文件。

header 声明/分配的方式肯定有错误,但无法弄清楚出了什么问题。

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, native C++ 项目的默认值为 __cdecl。修复:

 typedef void* (__stdcall * UnpackFile)(unsigned char*, int, LPTSTR, int);

请记住,错误检查在 C++ 中绝不是可选的,您确实确实需要检查 LoadLibrary() 和 GetProcAddress() 是否成功。在 C# 中是自动的,而不是在 C++ 中。两个函数在失败时都返回 NULL。

关于c++ - 将 LPTSTR 参数传递给 DLL 会在 C++ 项目中产生访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27592013/

相关文章:

c++ - DLL 中全局变量的使用限制(适用于 Windows)

c++ - 多个 CRT 出现段错误

c++ - 将参数发送到 DLL 以进行 boost::bind'ed

c++ - 如何从 C++ DLL 导出函数并在 Delphi 中使用?

c++ - 变量值在没有调用 scanf 的情况下发生变化

c++ - 模板继承层次结构中的循环依赖

c++ - 我可以轻松地覆盖 (STL) 迭代器的类别吗?

c - 如何为 jvm.dll 设置系统范围的环境变量

C++从 double 组中减去均值

c++ - 为什么初始化程序列表顺序必须与成员声明顺序匹配?