Solved

Conversion from LPTSTR* to STL string

Posted on 2004-04-14
24
1,176 Views
Last Modified: 2007-12-19
Hi,
I would like to know how to convert the string from LPTSTR* to STL string object.

Sample code would be of great help.
Thanks in advance.

nt WINAPI WinMain(HINSTANCE hInstance,
                           HINSTANCE hPrevInstance,
                           LPSTR lpCmdLine,
                           int nCmdShow)
{
      LPTSTR *szArglist;
      int nArgs;
      szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
//I would like to convert the szArglist to STL string object

}

Regards,
Pampa
0
Comment
Question by:pampa_analytica
  • 6
  • 5
  • 5
  • +3
24 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10822031
Well  LPTSTR is infact a LPSTR if u are not using unicode characters
And LPSTR is actually a pointer to a char string
Hence this should work (I haven't tried it)

string stlStr ( szArglist ) ;

Amit
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10822090
That's the way to do it. It does work. "string" does have a constructor that takes a char *.
0
 

Author Comment

by:pampa_analytica
ID: 10822315
I tried in the way what Amit has posted, but it is not working. It throws me error:
f:\Viz\Viz_src\DataVizGUI\DataVizGUI.cpp(36): error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::basic_string<_Elem,_Traits,_Ax>::_Alloc &)' : cannot convert parameter 1 from 'LPTSTR * ' to 'const std::basic_string<_Elem,_Traits,_Ax>::_Alloc &'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]
        and
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]

Please provide me some sample code to convert the char*/LPTSTR* to STL string.

Thanks in advance,
Pampa
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10822441
This works for me:

            LPTSTR lptstr = "this is the lptstr";
            string the_string(lptstr);

            cout << the_string << endl;
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10822449
If this still does not help, show your source code.
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10822576
Try this:

for(int i=0;i<nArgs;i++)
{
  if(!i)
    the_string +=' ';
  the_string += szArglist[i];
}

if you want all arguments to be palced in a string.
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10822577
...my mistake, if(!i) should be if(i)
0
 
LVL 14

Expert Comment

by:wayside
ID: 10822627
> string stlStr ( szArglist ) ;

LPTSTR * is not the same as LPTSTR. The first is an array of pointers, the second is one pointer.

Since szArglist is an array of pointers, you would need to do

string stlStr1(szArglist[0]);
string stlStr2(szArglist[1]);

etc.
0
 
LVL 11

Expert Comment

by:cup
ID: 10822810
If you are using unicode, use wstring instead of string
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10822861
wayside is absolutely correct
All of us didn't notice that ur szArglist is a pointer of type LPTSTR

Amit

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:nonubik
ID: 10822889
I did ;)
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10822934
Sorry nonubik,

I should have had a meticulous look at your code

Amit
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10822964
np, maybe it was too obscure.
0
 

Author Comment

by:pampa_analytica
ID: 10830040
Hi guys,

int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine,
                       int nCmdShow)
{
     LPTSTR *szArglist;
     int nArgs;
     szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
     string the_string;
//I would like to convert the szArglist to STL string object. I tried this:
          for( i=1; i<=nArgs-1; i++)
          {
      the_string = the_string + szArglist[i];
      MessageBox(NULL,szArglist[i],0,0);
         }
}

But i got this error:
f:\Viz\Viz_src\DataVizGUI\DataVizGUI.cpp(63): error C2678: binary '+' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion).

I am unable to solve this coz I am pretty new to VC++. PLease help me out in solving this problem.



Thanks in advance,
Pampa
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10830058
pampa_analytica,

std::string does have a binary + operator
and the above code works fine for me

Amit
0
 

Author Comment

by:pampa_analytica
ID: 10830078
I got this error while I was trying to do this:
      for( i=1; i<=nArgs-1; i++)
      {
                  the_string += szArglist[i];
              MessageBox(NULL,szArglist[i],0,0);
      }


f:\Viz\Viz_src\DataVizGUI\DataVizGUI.cpp(64): error C2679: binary '+=' : no operator found which takes a right-hand operand of type 'LPTSTR' (or there is no acceptable conversion)
0
 

Author Comment

by:pampa_analytica
ID: 10830080
#include <string>
using namespace std;

These are my includes in the project
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10830127
I think u are using VC++6.0
But I tried on it & it works fine for me

Amit
0
 

Author Comment

by:pampa_analytica
ID: 10830157
No Amit I am using VC++7.0. I tried but still Iam getting the same error.
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 50 total points
ID: 10830177
I am not sure but I have heard that VC++7.0 is less compliant compiler as compared to VC++7.1
I am not sure since I haven't used both
However, the code should work
The std::string does have a += as well as a + operator defined
Here is the quick reference

http://www.cppreference.com/cppstring_details.html#Operators

Amit

0
 
LVL 16

Expert Comment

by:nonubik
ID: 10830883
try a cast:
the_string += (char *)szArglist[i];
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 66
add elements to existing standard structure 2 96
Need some help with Microsoft Visual Studio C++ 2003 5 51
Problem with SqlConnection 4 159
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

919 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now