Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

arrays - Trash characters when using buffers in c++

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.

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;
   }
};

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
"));

    //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
"));}
    else {tex_box->AppendText(wxT("Save to file failed :( 
"));}                
}
else
{
    tex_box->AppendText(wxT("DLL_PingConnection False
"));
} 


std::cout << "Error description: " << DLL_ERR_DESCRIPTION() << "
"; //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:

enter image description here

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

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.

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 :(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This

DLL_PingConnection=(TYPE_DLL_PingConnection)

shouldn't it be

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

?

seems otherwise you will not get a valid pointer to the function in the DLL.

as a general rule you should check return values, especially from a DLL you load dynamically since it happens that you sometimes get another version of the DLL which may have a function with same name but other signature or where is missing entirely.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...