Solved

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

Posted on 2016-07-25
8
79 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

746 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

13 Experts available now in Live!

Get 1:1 Help Now