Solved

TCHAR to String^ and String^ to TCHAR

Posted on 2009-05-14
13
1,689 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
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…

911 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

18 Experts available now in Live!

Get 1:1 Help Now