Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-07-25
8
Medium Priority
?
133 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

877 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