Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

TCHAR to String^ and String^ to TCHAR

Posted on 2009-05-14
13
Medium Priority
?
1,738 Views
Last Modified: 2013-12-14
I am using VS2008 and windows forms application. I have to define a global string but I get an error if i do that (error: global or static variable may not have managed type 'System::String ^'). It is allowing me to declare a global TCHAR, but I can't seem to get TCHAR converted to String^ or the other way around.
1:
TCHAR t_tchar;
String^ t_string;
t_string=t_tchar;
error C2440: '=' : cannot convert from 'TCHAR' to 'System::String ^'

2:
TCHAR *t_tchar;
String^ t_string;
t_string=System::String(t_tchar);
 error C2440: '=' : cannot convert from 'System::String' to 'System::String ^'

0
Comment
Question by:funcoding
[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
  • 7
  • 6
13 Comments
 
LVL 1

Author Comment

by:funcoding
ID: 24383809
and also, I am using "Use Multi-Byte Character Set" for Character Set
0
 
LVL 19

Expert Comment

by:alb66
ID: 24383913
1) convert from String^ to char*

string^ str;

TCHAR* pStr = (TCHAR*)Marshal::StringToHGlobalAnsi( str ).ToPointer();

// when you've finished with pStr make a call to:
Marshal::FreeHGlobal( (IntPtr)pStr );



2) convert from char* to String^

str = Marshal::PtrToStringAnsi( pStr );

0
 
LVL 1

Author Comment

by:funcoding
ID: 24384097
Thanks for the suggestion. I am getting a linking error for the global TCHAR now. this header file is an include in the main Form if that helps.

1>projectName.obj : error LNK2005: "char * GUI_IP::t_tchar" (?t_tchar@GUI_IP@@3PADA) already defined in GUI_IP.obj
1>C:\Documents and Settings\My Documents\Projects\GUI_IP\Release\GUI_IP.exe : fatal error LNK1169: one or more multiply defined symbols found

This is how i used them:
TCHAR *t_tchar;
String^ t_string;
t_tchar=(TCHAR*)Marshal::StringToHGlobalAnsi( t_string ).ToPointer();

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.

 
LVL 19

Expert Comment

by:alb66
ID: 24384136
It seems that you have defined TCHAR *t_tchar in two files:
gui_ip.cpp and projectname.cpp
0
 
LVL 1

Author Comment

by:funcoding
ID: 24384223
projectname.cpp only has:

#include "StdAfx.h"
#include "projectName.h"

projectname.h is the header file that has the above code in it. and then GUI_IP.cpp has the following include:

#include "stdafx.h"
#include "Form1.h"

where Form1.h has #include "projectName.h" so is it getting initialized twice? I ran into similar prolelm sometime before when i had 1st header file included in Second and a third header file was including Second. I may be violating the standard here :p  Thanks for bearing with me here
0
 
LVL 19

Expert Comment

by:alb66
ID: 24384265
Yes, in this way you declare t_tchar twice.

You can try the following:

1) In projectname.cpp write:

#define __GLOBALS__

#include "StdAfx.h"
#include "projectName.h"


2) In projectname.h write:

#if ( defined( __GLOBALS__ ))
  #define E  
#else
  #define E extern
#endif

E TCHAR *t_tchar;
E String^ t_string;
0
 
LVL 1

Author Comment

by:funcoding
ID: 24384376
that for some reason was giving me the same error.
ProjectName.cpp was automatically created when i included a new form in my project. so the only two lines in that file were:

#include "StdAfx.h"
#include "projectName.h"

so i commented out the second one since i dont really need the .h in this cpp. So now the only file that has projectname.h in it is the Form1.h and it compiles okay! Suggestions on what i should keep an eye out for for later?

out of curiosity in your above suggestion, what is "E"? and what does it mean when you we type
#define E
else
#define E extern?

Thanks much
0
 
LVL 19

Accepted Solution

by:
alb66 earned 2000 total points
ID: 24384423
When __GLOBALS__ is defined ( in the above examples it happans only when you compile projectname.cpp...) E assumes the value of "" (empty).
So, the line
E TCHAR *t_tchar;
became
TCHAR *t_tchar;
and the variable is allocated.


When __GLOBALS__ is undefined ( when you compile gui_ip.cpp) E assumes the value of "extern".
So, the line
E TCHAR *t_tchar;
became
extern TCHAR *t_tchar;
and you tell the compiler that the variable is allocated in another file.

In this way the variable is allocated only once.


0
 
LVL 1

Author Comment

by:funcoding
ID: 24384728
thanks much!
0
 
LVL 1

Author Comment

by:funcoding
ID: 24384882
ran into another small problem: converting tchar back to String^:
TCHAR *t_tchar;
String^ Proj_Path;
Proj_Path= Marshal::PtrToStringAnsi( t_tchar );

error C2665: 'System::Runtime::InteropServices::Marshal::PtrToStringAnsi' : none of the 2 overloads could convert all the argument types
 c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: could be 'System::String ^System::Runtime::InteropServices::Marshal::PtrToStringAnsi(System::IntPtr)'
0
 
LVL 19

Expert Comment

by:alb66
ID: 24386422
You need a cast:

Proj_Path= Marshal::PtrToStringAnsi( (IntPtr)t_tchar );
0
 
LVL 1

Author Comment

by:funcoding
ID: 24386728
I used the line below and it worked:
Proj_Path = gcnew String (t_tchar);

is there any benefit of doing what you suggested over this way?
0
 
LVL 19

Expert Comment

by:alb66
ID: 24392789
I tink that your solution works only in a UNICODE project
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

597 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