【问题标题】:converting a char* to BSTR* which contains special characters将 char* 转换为包含特殊字符的 BSTR*
【发布时间】:2011-08-09 19:54:25
【问题描述】:

我正在尝试将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);

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

谢谢!!!!

【问题讨论】:

  • 你到底想做什么?您是否正在尝试将 VB 与 C 代码接口?
  • 是的...完全正确....VB 代码正在调用 C 代码...然后回到 VB 代码。
  • 你找到解决这个问题的方法了吗?

标签: char character bstr


【解决方案1】:

如果您正在操作缓冲区,您可能不想直接操作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

【讨论】:

    猜你喜欢
    • 2019-05-02
    • 2011-04-08
    • 2010-10-11
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2011-05-17
    • 1970-01-01
    相关资源
    最近更新 更多