Solved

c++ substatte a varabe for a string in a LPCTSTR statment

Posted on 2016-07-25
8
84 Views
Last Modified: 2016-07-27
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include "stdafx.h"
#include <windows.h>
#include <wininet.h>
#include <stdlib.h>  

using namespace System;
using namespace std;

int main(array<System::String ^> ^args)
{
// this part works it gives a pointer to the "my text"
      
        LPCTSTR myStr = L"My Text";

// now in here i have a string data = "My Text";

      string data = "My Text";
i have tried sunbstating the data for the text as in
LPCTSTR myStr = L ( data );
// throws up error C2065: 'Ldata' : undeclared identifier
any idea on how to substatute the 'text"  for the varable

      
      Console::WriteLine(L"Hello World");
    return 0;
}
0
Comment
Question by:sydneyguy
  • 4
  • 4
8 Comments
 
LVL 19

Expert Comment

by:mrwad99
ID: 41729232
Have you tried

wstring data = L"My Text";
LPCTSTR myStr = data;

?
0
 

Author Comment

by:sydneyguy
ID: 41730368
this will work the problem is that
functioncall (string abc)
{

          wstring data = L"My Text";    the "My Text" needs to be the varable abc coming in the function call
           LPCTSTR myStr = data;
}

this is what i cannot get the syntax correct on
thanks for you help so far
0
 
LVL 19

Accepted Solution

by:
mrwad99 earned 500 total points
ID: 41730783
OK, so you either need to pass a wstring argument as 'abc' into functioncall(), or if you cannot do this you need to change myStr to be a LPCSTR (note the missing T), or if you cannot do either of these you need to convert 'abc' into a wide string:

void functioncall(string abc)
{
	size_t size = abc.length() + 1;
	wchar_t* wide = new wchar_t[size];

	size_t outSize;
	mbstowcs_s(&outSize, wide, size, abc.c_str(), size - 1);

	LPCTSTR myStr = wide;
}

Open in new window

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:sydneyguy
ID: 41730953
works great thanks for that you seem pretty savy on this subject what i really need is some good documentation i can read on this subject do you know of any good sites or ebooks i can spend a few days getting up to speed on this subject
g
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 41730964
I think that the codeproject link you received on your other question is one of the best sources personally.  Generally speaking this isn't something that there is a lot of material on because there doesn't need to be.  I think with this question you knew what you wanted to do but you were looking at it in the wrong way; you needed to assign a std::string to an LPCTSTR, but you should have been searching for converting a std:;string to a std::wstring.

Glad to help, please close this question if I've answered it for you to keep the site tidy :)
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 41730965
You may also find https://msdn.microsoft.com/en-us/library/ms235631.aspx helpful, which talks about string type conversion.
0
 

Author Comment

by:sydneyguy
ID: 41730997
thats brilliant between the codeporject and that page and some other chapters i have in some other books i am on my to guru status , thanks again for you help, its funny isnt it its always easy to do something when you know how to do it
b
0
 

Author Closing Comment

by:sydneyguy
ID: 41730998
thanks again
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

785 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