473,698 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String object to unmanged char *?

I have a string object that I need to convert into an unmanged char * to be
used by several unmnaged 3rd party functions.
I have tried to use: (Marshal::Strin gToHGlobalAuto (Profname))
This would get me an IntPtr value, but I could not find the correct way to
cast an IntPtr to an unmanged char *.
One example I tried was:
char *inifile;
inifile = static_cast <char*> (Marshal::Strin gToHGlobalAuto (Profname));

It gave following compile error:
c:\T02010_NET_o ra9\cgi-bin\programs\Ti meReader\TimeRe aderWinService. cpp(236): error C2440: 'static_cast' : cannot convert from 'System::IntPtr ' to 'char *'
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called

There is another wrinkle on top of this, which I am hoping wont be a problem
once I figure the above example out. That is the parameter to function is
defined as a COMSTR which is a typedef to char far *.

The only reason profname is a string is that it is created and passed to
this function through a managed class and I could not get the managed class
to correctly keep a unmanaged char array.

Thanks,
Brian

Dec 21 '05 #1
7 2559
>This would get me an IntPtr value, but I could not find the correct way to
cast an IntPtr to an unmanged char *.


IntPtr::ToPoint er()
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 21 '05 #2
I have tried that but it does not seem to return whole string, but only first
character in unmanaged string below is code and output from those lines

fip->WriteLine ("(profname Full Path to ini file: ]{0}[ " , Profname);

char *inifile;
char abc[200];
inifile = (char *)(Marshal::Str ingToHGlobalAut o (Profname)).ToP ointer();
strcpy (abc, inifile);
fip->WriteLine ("inifile Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (inifile)));
fip->WriteLine ("abc Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (&abc[0])));

(profname Full Path to ini file: ]C:\WINDOWS\syst em32\TimeReader .ini[
inifile Full Path to ini file: ]C[
abc Full Path to ini file: ]C[
"Mattias Sjögren" wrote:
This would get me an IntPtr value, but I could not find the correct way to
cast an IntPtr to an unmanged char *.


IntPtr::ToPoint er()
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Dec 22 '05 #3
Hyd
Hello,

Personally, I use this :

String^ pmanagedString = gcnew String( "HelloWorld " );
IntPtr pstr = Marshal::String ToHGlobalAnsi( pmanagedString );
if( pstr != IntPtr::Zero )
{
char* punmanagedStrin g =
reinterpret_cas t<char*>(static _cast<void*>(ps tr));
...
...
Marshal::FreeHG lobal( pstr ); // Do not forget to free the memory.
}

Hervé.

brian_harris a écrit :
I have tried that but it does not seem to return whole string, but only first
character in unmanaged string below is code and output from those lines

fip->WriteLine ("(profname Full Path to ini file: ]{0}[ " , Profname);

char *inifile;
char abc[200];
inifile = (char *)(Marshal::Str ingToHGlobalAut o (Profname)).ToP ointer();
strcpy (abc, inifile);
fip->WriteLine ("inifile Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (inifile)));
fip->WriteLine ("abc Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (&abc[0])));

(profname Full Path to ini file: ]C:\WINDOWS\syst em32\TimeReader .ini[
inifile Full Path to ini file: ]C[
abc Full Path to ini file: ]C[
"Mattias Sjögren" wrote:
This would get me an IntPtr value, but I could not find the correct way to
cast an IntPtr to an unmanged char *.


IntPtr::ToPoint er()
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Dec 22 '05 #4
Hello,
I got the same problem using method you specified, but I did figure out what
seems to be happening, I just don't know how to fix it. It seems that the
StringToHGlobal Auto method is puting a null terminator after each character
in the String it is converting So that following code produces following
output:

fip->WriteLine ("(profname Full Path to ini file: ]{0}[ " , Profname);

char *inifile;
IntPtr pstr = Marshal::String ToHGlobalAuto (Profname);
if (pstr != IntPtr::Zero) {
inifile = reinterpret_cas t<char*>(static _cast<void*>(ps tr));

fip->WriteLine ("inifile Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (inifile)));
fip->WriteLine ("inifile Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (inifile+1)));
fip->WriteLine ("inifile Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (inifile+2)));
fip->WriteLine ("inifile Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (inifile+3)));
fip->WriteLine ("inifile Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (inifile+4)));
fip->WriteLine ("inifile Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (inifile+5)));
fip->WriteLine ("inifile Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (inifile+6)));
fip->WriteLine ("inifile Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (inifile+7)));
}

(profname Full Path to ini file: ]C:\WINDOWS\syst em32\TimeReader .ini[
inifile Full Path to ini file: ]C[
inifile Full Path to ini file: ][
inifile Full Path to ini file: ]:[
inifile Full Path to ini file: ][
inifile Full Path to ini file: ]\[
inifile Full Path to ini file: ][
inifile Full Path to ini file: ]W[
inifile Full Path to ini file: ][

"Hyd" wrote:
Hello,

Personally, I use this :

String^ pmanagedString = gcnew String( "HelloWorld " );
IntPtr pstr = Marshal::String ToHGlobalAnsi( pmanagedString );
if( pstr != IntPtr::Zero )
{
char* punmanagedStrin g =
reinterpret_cas t<char*>(static _cast<void*>(ps tr));
...
...
Marshal::FreeHG lobal( pstr ); // Do not forget to free the memory.
}

Hervé.

brian_harris a écrit :
I have tried that but it does not seem to return whole string, but only first
character in unmanaged string below is code and output from those lines

fip->WriteLine ("(profname Full Path to ini file: ]{0}[ " , Profname);

char *inifile;
char abc[200];
inifile = (char *)(Marshal::Str ingToHGlobalAut o (Profname)).ToP ointer();
strcpy (abc, inifile);
fip->WriteLine ("inifile Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (inifile)));
fip->WriteLine ("abc Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (&abc[0])));

(profname Full Path to ini file: ]C:\WINDOWS\syst em32\TimeReader .ini[
inifile Full Path to ini file: ]C[
abc Full Path to ini file: ]C[
"Mattias Sjögren" wrote:
>This would get me an IntPtr value, but I could not find the correct way to
>cast an IntPtr to an unmanged char *.

IntPtr::ToPoint er()
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Dec 22 '05 #5
Hyd
Don't use StringToHGlobal Auto but StringToHGlobal Ansi : managed string
is a Unicode string and char* is a ASCII (Ansi) string.
StringToHGlobal Ansi allows to convert a string from Unicode to ASCII.

"ABC" is 41 00 42 00 43 00 00 00 (hexadecimal) in Unicode including
null terminator.
"ABC" is 41 42 43 00 (hexadecimal) in ASCII including null terminator.

Hervé.

Dec 22 '05 #6
I finally saw the big difference between what you where doing and what I was
doing. When I use StringToHGlobal Auto I get the bad results That I have
mentioned,
but if I use StringToHGlobal Ansi then it works correctly. I don't
understand what the problem is since documentation indicates that they both
should end up with same results.

"Hyd" wrote:
Hello,

Personally, I use this :

String^ pmanagedString = gcnew String( "HelloWorld " );
IntPtr pstr = Marshal::String ToHGlobalAnsi( pmanagedString );
if( pstr != IntPtr::Zero )
{
char* punmanagedStrin g =
reinterpret_cas t<char*>(static _cast<void*>(ps tr));
...
...
Marshal::FreeHG lobal( pstr ); // Do not forget to free the memory.
}

Hervé.

brian_harris a écrit :
I have tried that but it does not seem to return whole string, but only first
character in unmanaged string below is code and output from those lines

fip->WriteLine ("(profname Full Path to ini file: ]{0}[ " , Profname);

char *inifile;
char abc[200];
inifile = (char *)(Marshal::Str ingToHGlobalAut o (Profname)).ToP ointer();
strcpy (abc, inifile);
fip->WriteLine ("inifile Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (inifile)));
fip->WriteLine ("abc Full Path to ini file: ]{0}[ " ,
Marshal::PtrToS tringAnsi(stati c_cast<IntPtr> (&abc[0])));

(profname Full Path to ini file: ]C:\WINDOWS\syst em32\TimeReader .ini[
inifile Full Path to ini file: ]C[
abc Full Path to ini file: ]C[
"Mattias Sjögren" wrote:
>This would get me an IntPtr value, but I could not find the correct way to
>cast an IntPtr to an unmanged char *.

IntPtr::ToPoint er()
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Dec 22 '05 #7
Hyd
For IntPtr Marshal::String ToHGlobalAuto(s tring s), MSDN Library says :
"Copies the contents of a managed String into unmanaged memory,
converting into ANSI format if required".
The problem is "if required", I don't know how it can choose if it must
convert or not into ANSI. In your case, it doesn't convert into ANSI
format and char* uses ANSI format.
You can work with UNICODE format in unmanaged code, but you must use
wchar_t* string instead of char* string.

Dec 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4777
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
16
17426
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char * is 4 bytes, should the following yield 4, 5, of something else? (And, if something else, what determines the result?) char x = "abcd"; printf( "%d\n", sizeof( x ) ); -Don
7
4358
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
2
1660
by: chaor | last post by:
hi, how to convert the following strut into safe struct in c#? struct in C++: typedef struct { char m_username; char m_password; char m_userIP;
0
242
by: P Reddy | last post by:
Hi All, Greetings!!! I have a question. Please respond.... I am trying to write a C# component that need to inherit some of the the interfaces from unmanged code(VC). I think one way to resolve this is to port unmanged interfaces and header files in to manage C++ as Visual Studio allows porting and compiling all this
3
1102
by: kilroytrout | last post by:
When trying to use some unmanged C++ libs in a VS 2005 Release Candidate Windows Forms (/clr) project, I found that any dynamic initialization in the lib's global STL objects (perhaps other C++ objects as well) create an exception at startup. Has anyone seen this or know a workaround? For example create a simple /clr C++ console app: #include "unmanaged.h"
5
3412
by: Maxwell | last post by:
Hello, Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project where I have a managed class reference in a unmanaged class...simple enough. To keep things short I am for the most part attempting to do what is this article by Nish: http://www.voidnish.com/articles/ShowArticle.aspx?code=cbwijw I have to hook up a unmanaged callback to a managed method using IJW NOT P\Invoke. So I am employing this "Thunk" or "Bridge" class...
2
1882
by: brian_harris | last post by:
I have a 3rd party unmanged .dll. This has functions that will look up information and fill a C string passed to it with data. I want to use this data in .NET classes after it has been filled and I am programming in C++. I believe that I should use pinvoke, but I see a problem that I can't figure how to get around. I don't think I can declare the string I want as a String class since when I pass it to unmanged function it is not yet...
1
1330
by: hkasten | last post by:
Hello, I have a unmanged C++ dll which has a function defined like: Modify(char* str) The char* is an in/out with a max length of 1024 I'd like to have a C++.Net Wrapper function which can be called by CSharp like that:
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.