Solved

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

Posted on 2016-07-25
8
100 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 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