Solved

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

Posted on 2016-07-25
8
113 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

623 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