0

我正在尝试将 a 转换char*为 a 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);

任何帮助将不胜感激!!!

谢谢!!!!

4

1 回答 1

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 的 BSTR 语义完整指南

于 2011-10-06T16:15:41.967 回答