?
Solved

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

Posted on 2007-10-19
12
Medium Priority
?
4,681 Views
Last Modified: 2008-01-09
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);
0
Comment
Question by:lapucca
  • 7
  • 5
12 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 20113647
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
 
LVL 86

Expert Comment

by:jkr
ID: 20113655
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
 

Author Comment

by:lapucca
ID: 20113733
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:jkr
ID: 20113752
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
 

Author Comment

by:lapucca
ID: 20115615
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
 
LVL 86

Expert Comment

by:jkr
ID: 20115661
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
 
LVL 86

Expert Comment

by:jkr
ID: 20115678
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
 

Author Comment

by:lapucca
ID: 20116251
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
 
LVL 86

Expert Comment

by:jkr
ID: 20116275
>>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
 

Author Comment

by:lapucca
ID: 20116316
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
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 20116663
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
 

Author Comment

by:lapucca
ID: 20119228
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question