2013-07-30 84 views
-1

我有一個需要在C++中處理的DLL。我正在使用WxWidgets(標準編譯,但我也嘗試過Unicode開/關)和NetBeans。我也試過在沒有WxWidgets的情況下處理這個問題(windows.h),並且遇到了同樣的問題。在C++中使用緩衝區時的垃圾回收字符

這裏是我如何訪問使用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. 

的問題是,該文件,而不是良好的內容裏面我得到垃圾這樣的:

enter image description here

請注意,這只是發生在使用緩衝區像功能:

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) - 同樣的問題。這是代碼:Getting trash data in char* while using it as buffer in function。請幫助:(

回答

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 *wchar_t *wxChar *給文件不同的名稱。
    例如file_ping_xml.Open(WXT( 「C:\ DLL \ ping_w_t_FromUTF8.xml」),...
    爲wchar_t的*結合
    wxString mystring = wxString::FromUTF8(buffor_ping_xml);
  • 也結合
  • wxString mystring(buffor_ping_xml);

  • 然後在瀏覽器中檢查出的樣子,這些文件的。

要測試你可以去你的wxWidgets示例文件夾。編譯到文件夾C:\wxWidgets\samples\docview\docview.cpp。用docview.exe打開一個unicode文件。它看起來如何。

Unicode download file

Unicode相關編譯設置

應該定義wxUSE_UNICODE 1編譯您以Unicode模式程序。目前適用於wxMSW,wxGTK,wxMac和wxX11。如果以ANSI模式編譯程序,則仍然可以定義wxUSE_WCHAR_T以獲得對wchar_t類型的有限支持。

+0

嗨。函數名稱是一個錯字(現在已修復) - 我將某些東西的代碼分條以使其閱讀更加容易。然而,關於變量'buffor_ping_xml [2000]',如果我這樣給bufor,這是唯一的方法:'char * buffor_ping_xml'程序將會崩潰。在文檔中我使用了一個數據類型'PChar'。有任何想法嗎? –

+0

另外:從typdef中取走*將不會編譯。 –

+0

@baron_bartek:你試過了:'const char * buffor_ping_xml = new char [2000];' –

0

DLL_PingConnection=(TYPE_DLL_PingConnection) 

應該不會是

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

似乎不然,你不會得到一個有效的指針在DLL中的函數。

作爲一般規則,你應該檢查返回值,特別是從 你動態地加載DLL,因爲它發生,你有時會得到另一個DLL的 版本,它可能有相同的名字,但其他簽名或 的功能在哪裏完全失蹤。

+0

我的不好。我編輯了我的問題。 PS:我會檢查dll函數的返回值。我得到了succec(bool - > true),但在緩衝區中我只找到垃圾,而返回字符串而不是bool(不使用緩衝區)的函數工作得很好。示例是DLL_ERR_DESCRIPTION –