问题描述
我有一个DLL,我需要在C ++中处理。我使用WxWidgets(标准编译,但我也尝试Unicode开/关)和NetBeans。我也尝试处理这个没有WxWidgets( windows.h
),并有相同的问题。
I have a DLL that I need to handle in C++. I'm using WxWidgets (standard compilation, but I also tried Unicode on/off) and NetBeans. I also tried dealing with this without WxWidgets (windows.h
) and had same problems.
使用WxWidgets访问DLL函数:
Here is how I access the DLL functions using WxWidgets:
// -------------------- 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内容,我尝试保存到文件。
And here is the function I run. It should return and XML content, that I try to save to the file.
//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.
问题是在文件里面,而不是在文件中好的内容我得到这样的垃圾:
The problem is that inside the file instead of good content I get rubbish like this:
注意,这仅发生在使用如下缓冲区的函数中:
NOTE that this only happens in functions that use buffers like:
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,但我不认为这里的问题。
Documentation say that the DLL operates on Windows-1250. File ping.xml
I have set to windows ANSI, but I don't think problem lies here.
strong> EDIT :我写了问题没有WxWidgets(我加载DLL使用 windows.h
) - 同样的问题。以下是代码:。请帮助:(
EDIT: I have written problem without WxWidgets (I load DLL using windows.h
) - same problems. Here is the code: Getting trash data in char* while using it as buffer in function . Please help :(
推荐答案
这里是answear:。
Here is answear: Getting trash data in char* while using it as buffer in function.
感谢大家 - 特别是耐心。
Thanks everyone - expecially for patience.
这篇关于在c ++中使用缓冲区时,废弃字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!