How do I substring from a BSTR or -bstr_t string or from a VARIANT variable?

Hi, I need to get a subsring from the following varialbe var (VARIANT vaiable) or the _bstr_ VARIABLE WINlOGINnAME.  The substring should start from the 1st char and stops right before the '@' character.  Please help with how to code this.  Thank you.  I'm using C++ in a COM component.

      winLoginName = var.bstrVal;
      winLoginName.
      VariantClear(&var);
lapuccaAsked:
Who is Participating?
 
jkrCommented:
Sorry, that should be

_variant_t vt(var);
_bstr_t bstr(vt);

string str = (char*) bstr;

int pos = str.find('@');

string sub = str.substr(0, pos -1);
0
 
jkrCommented:
Using a '_bstr_t', that is fairly easy:

#include <comdef.h>

VARIANT v:
_variant_t vt(v)
_bstr_t bstr(vt);

const char* pc = (char*) bstr;

// now look for the string in question
0
 
jkrCommented:
Oh, the above works because there is an 'operator char*( )' defined for '_bstr_t', that is the only secret behind that (FYI: There's also 'operator wchar_t*( ) ', so you are safe on the UNICODE side).
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
lapuccaAuthor Commented:
My question is how to look for a substring.  I'm not sure you're answering to that.  I apologize if you are but I still don't know how your code finds the substring that I'm looking for.
0
 
jkrCommented:
Sorry, I thought that was clear:

#include <comdef.h>

VARIANT v:
_variant_t vt(v)
_bstr_t bstr(vt);

const char* pc = (char*) bstr;

char* p = NULL;

p = strstr(pc,"substring");

if (p != NULL ) { // found substring


}

or

#include <string>
#include <comdef.h>
using namespace std;

VARIANT v:
_variant_t vt(v)
_bstr_t bstr(vt);

string str = (char*) bstr;

int pos = str.find("substring");
0
 
lapuccaAuthor Commented:
Hi, I still don't know how do I specify to find the substring of all characters before an '@' sign with the following code:

const char* pc = (char*) bstr;

char* p = NULL;

p = strstr(pc,"substring");//How do I specify to find all chars before an '@' ?

if (p != NULL ) { // found substring
0
 
jkrCommented:
Use

p = strstr(pc,"@");

to locate the '@' sign. Then Create a new buffer using

size_t sz = (size_t)(p - pc) + 1; // calc. size + trailing zero

char* buf = new char[sz];

memset(buf,0,sz);

memcpy(buf,pc,sz - 1); // copy everything before '@'
0
 
jkrCommented:
Or, alternatively:

#include <string>
#include <comdef.h>
using namespace std;

VARIANT v:
_variant_t vt(v)
_bstr_t bstr(vt);

string str = (char*) bstr;

int pos = str.find('@');

string sub = str.substr(0, pos -1);
0
 
lapuccaAuthor Commented:
Strange, but I keep getting a -1 return for pos.  Why is that so?  Below is my code:  
      str = (char*)(var.bstrVal);
      pos = str.find('@');
      if(pos > 1)
      {
            sub = str.substr(0, pos -1);
            winLoginName = sub.c_str();
      }
      else
            winLoginName = (_bstr_t)(var);      
0
 
jkrCommented:
>>str = (char*)(var.bstrVal);

That does not work, it has to be

_bstr_t bstr(var);
str = (char*) bstr;

      pos = str.find('@');
      if(pos > 1)
      {
            sub = str.substr(0, pos -1);
            winLoginName = sub.c_str();
      }
      else
            winLoginName = str.c_str();  

Without the '_bstr_t' helper, there is no 'operator char*()' and the cast 'str = (char*)(var.bstrVal);' simply produces incorrect results.
0
 
lapuccaAuthor Commented:
I'm getting error with  _bstr_t bstr(var); It doesn' like me decaring this in the middle of the fuction.  Any way around this?  Thank you.
Error      3      error C2362: initialization of 'bstr' is skipped by 'goto CleanUp'      
0
 
lapuccaAuthor Commented:
Sorry about all these questions. I'm a C# programmer and a beginner level for C++ in COM.  Thanks for all your help.  It is now working (pos shouldn't -1).  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.