2011-04-20 29 views
0

我試圖將char*轉換爲BSTR*,並且我的char*中的特殊字符被加密。我嘗試了幾種在網上找到的方法,但回到調用vb代碼中,我總是會得到一些不同的結果。我敢肯定,這與特殊字符做,因爲如果我沒有他們,這似乎是確定....將char *轉換爲包含特殊字符的BSTR *

我的代碼是沿着這些路線的東西...

_export myFunction(BSTR *VBtextin, BSTR *VBpassword, BSTR *VBtextout, FPINT encrypt) { 

BSTR password = SysAllocString (*VBpassword); 
char* myChar; 
myChar = (char*) password //is this ok to cast? it seems to remain the same when i print out. 

//then I encrypt the myChar in some function...and want to convert back to BSTR 
//i've tried a few ways like below, and some other ways i've seen online...to no avail. 

_bstr_t temp(myChar); 
SysReAllocString(VBtextout, myChar); 

任何幫助將非常感謝!

謝謝!

+0

你究竟在做什麼?您是否試圖將VB與C代碼連接? – thkala 2011-04-20 20:47:35

+0

是......正好...... VB代碼調用C代碼......然後回到VB代碼。 – George 2011-04-20 20:50:10

+0

你有沒有得到這個問題的解決方案? – 2012-03-13 11:14:11

回答

0

如果你在操縱緩衝區,你可能不想直接操作char *。首先進行復制:

_export myFunction(BSTR *VBtextin, BSTR *VBpassword, BSTR *VBtextout, FPINT encrypt) { 

    UINT length = SysStringLen(*VBpassword) + 1; 
    char* my_char = new char[length]; 
    HRESULT hr = StringCchCopy(my_char, length, *VBpassword); 

如果一切成功,請執行轉換。確保也適合您的情況下處理失敗。

if (SUCCEEDED(hr)) { 
    // Perform transformations... 
    } 

然後進行復制回:

*VBtextout = SysAllocString(my_char); 
    delete [] my_char; 
} 

此外,有Eric's Complete Guide to BSTR Semantics讀。