Solved

TCHAR to String^ and String^ to TCHAR

Posted on 2009-05-14
13
1,676 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
Comment Utility
and also, I am using "Use Multi-Byte Character Set" for Character Set
0
 
LVL 19

Expert Comment

by:alb66
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:funcoding
Comment Utility
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
Comment Utility
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
Comment Utility
thanks much!
0
 
LVL 1

Author Comment

by:funcoding
Comment Utility
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
Comment Utility
You need a cast:

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

Author Comment

by:funcoding
Comment Utility
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
Comment Utility
I tink that your solution works only in a UNICODE project
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
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…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

772 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