将char *转换为包含特殊字符的BSTR *

时间:2011-04-20 20:38:41

标签: char character bstr

我正在尝试将char*转换为BSTR*,而我的char*中的特殊字符不会被加密。我已经尝试了在网络上找到的几种方法,但是在调用vb代码中,我总是会遇到不同的东西。我很确定这与特殊字符有关,因为如果我没有它们,它似乎没问题......

我的代码就是这些......

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

任何帮助都将非常感谢!!!

感谢!!!!

1 个答案:

答案 0 :(得分: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