开发者

converting a char* to BSTR* which contains special characters

开发者 https://www.devze.com 2023-02-28 05:36 出处:网络
I\'m trying to convert a char* to a BSTR*, and my char* has special characters in it from being encrypted.I have tried several approaches found on the web, but back in the calling vb code, I always en

I'm trying to convert a char* to a BSTR*, and my char* has special characters in it from being encrypted. I have tried several approaches found on the web, but back in the calling vb code, I always end up with something different. I'm pretty sure this has to do with the special characters, because if I don't have them in, it seems to be ok....

my code is something along these lines...

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

any help would be greatly greatly appreciated!!!

Thanks!!!!


If you're manipulating the buffer, you probably don't want manipulate the char * directly. First make a copy:

_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 that all succeeds, perform your transformation. Make sure to handle failure as well, as appropriate for you.

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

Then make a copy back:

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

Also, have a read of Eric's Complete Guide to BSTR Semantics.

0

精彩评论

暂无评论...
验证码 换一张
取 消