-1

我有一个需要用 C++ 处理的 DLL。我正在使用 WxWidgets(标准编译,但我也尝试过 Unicode 开/关)和 NetBeans。我也尝试在没有 WxWidgets ( ) 的情况下处理这个windows.h问题并且遇到了同样的问题。

以下是我使用 WxWidgets 访问 DLL 函数的方法:

// -------------------- POINTERS TO FUNCTIONS
typedef bool(*TYPE_DLL_SetLicense)(char*, char*);
typedef bool(*TYPE_DLL_PingConnection)(char*); 
typedef char*(*TYPE_DLL_ERR_DESCRIPTION)(void);

class DLL_Library
{
public:
    // pointers to functions inside dll
    TYPE_DLL_SetLicense DLL_SetLicense;        //initialize - will wor fine as it returns only true/false (buffer only provide data)
    TYPE_DLL_PingConnection DLL_PingConnection;      //ping to serwer. Will return trahs, becouse it uses buffer to provide data ang get answear back
    TYPE_DLL_ERR_DESCRIPTION DLL_ERR_DESCRIPTION;      //error description. No buffer, no trouble. Returns correct string.
    wxDynamicLibrary dynLib2;

    int initialize(void)
    {
        //patch to dll
        wxString path = wxStandardPaths::Get().GetExecutablePath().BeforeLast('\\') + _("\\DLL_dll\\DLLMOK.dll");
        if(!wxFile::Exists(path)) return -1;

        //load dll
        if(!dynLib2.Load(path)) return -2;

        //Assign functions in dll to variable
        DLL_SetLicense=(TYPE_DLL_SetLicense) dynLib2.GetSymbol(wxT("DLL_SetLicense"));
        DLL_PingConnection=(TYPE_DLL_PingConnection)  dynLib2.GetSymbol(wxT("DLL_PingConnection"));
        DLL_ERR_DESCRIPTION=(TYPE_DLL_ERR_DESCRIPTION) dynLib2.GetSymbol(wxT("DLL_ERROR_DESCRIPTION"));


    return 0;
   }
};

这是我运行的功能。它应该返回和 XML 内容,我尝试将其保存到文件中。

//DLL_PingConnection            
//result ping to be save in file
wxFile file_ping_xml;
plik_ping_xml.Open(wxT("C:\\dll\\ping.xml"),wxFile::write);
char buffor_ping_xml[2000];

//I run the function here
bool is_ping = DLL_PingConnection(buffor_ping_xml);

if(is_ping)
{

    tex_box->AppendText(wxT("DLL_PingConnection True\n"));

    //we save result to file
    bool is_write_ping_ok = file_ping_xml.Write(buffor_ping_xml,2000);
    if (is_write_ping_ok){tex_box->AppendText(wxT("Save to file is ok ok\n"));}
    else {tex_box->AppendText(wxT("Save to file failed :( \n"));}                
}
else
{
    tex_box->AppendText(wxT("DLL_PingConnection False\n"));
} 


std::cout << "Error description: " << DLL_ERR_DESCRIPTION() << "\n"; //will work fine both in saving to file, and in streaming to screen.

问题是在文件内部而不是好的内容中,我得到了这样的垃圾:

在此处输入图像描述

请注意,这只发生在使用缓冲区的函数中,例如:

char buffer[2000] //buffer will contain for example file xml
function do_sth_with_xml(buffer) //buffer containing xml will (should) be overwriten with xml results of the function - in our case DLL_PingCONNECTION should save in buffer xml with connection data

文档说 DLL 在 Windows-1250 上运行。我已将文件ping.xml设置为 windows ANSI,但我认为问题不在于这里。

编辑:我写了没有 WxWidgets 的问题(我使用加载 DLL windows.h)-同样的问题。这是代码:在 char* 中获取垃圾数据,同时在函数中将其用作缓冲区。请帮忙 :(

4

3 回答 3

0

你命名了一个函数

DLL_PingConnection=(TYPE_DLL_PingConnection) dynLib2.GetSymbol(....

并调用它

OSOZ.OSOZ_PingConnection(buffor_ping_xml);

你 typedef 一个函数

typedef bool(*TYPE_DLL_PingConnection)(char*); 

你创建一个变量

char buffor_ping_xml[2000];

在您的 typedef 中char*,您的 buffor_ping_xml 是char

那怎么行?

尝试

char *buffor_ping_xml = new char[2000]; 
/* or */
wchar_t *buffor_ping_xml = new wchar_t[2000];
/* or */
wxChar *buffor_ping_xml = new wxchar[2000];

bool is_ping = DLL_PingConnection(buffor_ping_xml);
wxString mystring = wxString::FromUTF8(buffor_ping_xml);

写入mystring文件。

去做:

  • 在您的wxwidgets\libs文件夹中查找名称中libs 是否libwxmsw29ud_*带有“u”的文件夹(此处为版本号 29)?如果不是你不能使用 unicode

如果是,下一步

  • 对于所有不同的测试char *,给文件不同的名称。 例如 file_ping_xml.Open(wxT("C:\dll\ping_w_t_FromUTF8.xml"), ... for wchar_t * 与wchar_t *wxChar *


    wxString mystring = wxString::FromUTF8(buffor_ping_xml);
  • 也结合
  • wxString mystring(buffor_ping_xml);

  • 然后查看浏览器中文件的外观。

要进行测试,您可以转到 wxWidgets 示例文件夹。在文件夹中编译C:\wxWidgets\samples\docview\docview.cpp。用 docview.exe 打开一个 unicode 文件。它看起来如何。

Unicode 下载文件

Unicode 相关的编译设置

您应该定义wxUSE_UNICODE为 1 以在 Unicode 模式下编译您的程序。这目前适用于 wxMSW、wxGTK、wxMac 和 wxX11。如果您在 ANSI 模式下编译程序,您仍然可以定义wxUSE_WCHAR_T以获得对 wchar_t 类型的一些有限支持。

于 2013-08-01T01:49:49.733 回答
0

DLL_PingConnection=(TYPE_DLL_PingConnection)

不应该

DLL_PingConnection=(TYPE_DLL_PingConnection) dynLib2.GetSymbol(wxT("DLL_PingConnection"));

?

否则您将无法获得指向 DLL 中函数的有效指针。

作为一般规则,您应该检查返回值,特别是从动态加载的 DLL 中检查返回值,因为有时您会得到另一个版本的 DLL,该版本可能具有相同名称但其他签名的函数,或者完全缺失的地方。

于 2013-08-02T18:16:40.287 回答
0

这是答案:在 char* 中获取垃圾数据,同时将其用作函数中的缓冲区

谢谢大家 - 特别是耐心。

于 2013-09-21T22:53:03.903 回答