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.