Solved

TCHAR to String^ and String^ to TCHAR

Posted on 2009-05-14
13
1,696 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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 500 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

785 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