Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...