473,378 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Managed String To LPCSTR

I want to read from an ini file :
System::String* windowsFolder = System::Environment::SystemDirectory;

System::String* iniFile = System::IO::Path::Combine(windowsFolder,
"My.ini");

char Class[200];

LPCTSTR lstr = 0;

lstr = static_cast<LPCTSTR>(const_cast<void*>(static_cast <const
void*>(Marshal::StringToHGlobalAuto(iniFile))));

// I took this cast from MSDN->Managed Extensions for C++ Migration
Guide->Appendix: Sample Code

// iniFile is good = "C:\winnt\system32\My.ini", BUT lstr is only "C" ?!!!!

GetPrivateProfileString("Section", "Key", "", Class, 199, lstr);

Please help.

Thanks
Nov 16 '05 #1
2 3851
Bragadiru,
I want to read from an ini file :
System::String* windowsFolder = System::Environment::SystemDirectory;

System::String* iniFile = System::IO::Path::Combine(windowsFolder,
"My.ini");

char Class[200];

LPCTSTR lstr = 0;

lstr = static_cast<LPCTSTR>(const_cast<void*>(static_cast <const
void*>(Marshal::StringToHGlobalAuto(iniFile))));

// I took this cast from MSDN->Managed Extensions for C++ Migration
Guide->Appendix: Sample Code

// iniFile is good = "C:\winnt\system32\My.ini", BUT lstr is only "C"

?!!!!

That's because in your use of StringToHGlobalAuto(), you're converting to a
wide char string, and storing the result in an ansi one (I bet you're not
compiling with UNICODE/_UNICODE defined).

Instead, use explicitly Marshal::StringToHGlobalAnsi() or STHGUni() as
necessary.
--
Tomas Restrepo
to****@mvps.org
Nov 16 '05 #2
It works. Thank you.

"Tomas Restrepo (MVP)" <to****@mvps.org> wrote in message
news:um****************@TK2MSFTNGP09.phx.gbl...
Bragadiru,
I want to read from an ini file :
System::String* windowsFolder = System::Environment::SystemDirectory;

System::String* iniFile = System::IO::Path::Combine(windowsFolder,
"My.ini");

char Class[200];

LPCTSTR lstr = 0;

lstr = static_cast<LPCTSTR>(const_cast<void*>(static_cast <const
void*>(Marshal::StringToHGlobalAuto(iniFile))));

// I took this cast from MSDN->Managed Extensions for C++ Migration
Guide->Appendix: Sample Code

// iniFile is good = "C:\winnt\system32\My.ini", BUT lstr is only "C" ?!!!!

That's because in your use of StringToHGlobalAuto(), you're converting to

a wide char string, and storing the result in an ansi one (I bet you're not
compiling with UNICODE/_UNICODE defined).

Instead, use explicitly Marshal::StringToHGlobalAnsi() or STHGUni() as
necessary.
--
Tomas Restrepo
to****@mvps.org

Nov 16 '05 #3

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

Similar topics

3
by: Norm Cook | last post by:
Hope there are some mixed language programmers here. Trying to write a c++ dll for my VB6 program. To help me with my understanding, I just want to pass it a VB long (c++ int) & have it return...
8
by: johnsto | last post by:
I'm really stuck - can someone help me! I've got a basic setup consisting of two things: 1. A C# web service 2. An unmanaged C++ DLL The WS is intended to call some functions in the DLL. The...
0
by: nygiantswin2005 | last post by:
I would like to know how do I pass a pointer to a struct from managed code to unmanaged code. For example if I create structure like this in managed code. StructLayout( LayoutKind.Sequential,...
4
by: SJR3t2 | last post by:
I am new to C#, and I have an issue that I can't find the answer thru googling or checking msdn. I want to run some c++ code from a dll in C#. Here are snipits of the C++ header file. struct...
13
by: Joe Thompson | last post by:
Hi, I am using VS 2003 writing a managed C++ windows app. I am calling PlaySound. The first parameter is a LPCSTR. I have a String *str representing a filename. I like using the String class...
7
by: nicolas.hilaire | last post by:
hi all, i'm using this code to convert a String ^ in char * String ^str = "string .net"; IntPtr p = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str); LPCSTR str2 =...
14
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
I have seen the following function to convert from a System::String^ to a const wchar_t*. I would like to get a LPCTSTR and AFAIK LPCTSTR is equal to const wchar_t*. Then it should all work right?...
2
by: Sean F. Aitken | last post by:
Good afternoon, We have an app that uses a CMap with CString types for values and accepts LPCSTR as the parameter. The object being created is on the heap (created dynamically). A leak detector...
4
by: Olivier Matrot | last post by:
Hello, I'm in the process of writing a managed wrapper on legacy C++ code. I have to deal with methods that are using the ellipsis. For instance,the legacy method is : void CLog::Print (LPCSTR...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.