473,836 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Managed String To LPCSTR

I want to read from an ini file :
System::String* windowsFolder = System::Environ ment::SystemDir ectory;

System::String* iniFile = System::IO::Pat h::Combine(wind owsFolder,
"My.ini");

char Class[200];

LPCTSTR lstr = 0;

lstr = static_cast<LPC TSTR>(const_cas t<void*>(static _cast<const
void*>(Marshal: :StringToHGloba lAuto(iniFile)) ));

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

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

GetPrivateProfi leString("Secti on", "Key", "", Class, 199, lstr);

Please help.

Thanks
Nov 16 '05 #1
2 3867
Bragadiru,
I want to read from an ini file :
System::String* windowsFolder = System::Environ ment::SystemDir ectory;

System::String* iniFile = System::IO::Pat h::Combine(wind owsFolder,
"My.ini");

char Class[200];

LPCTSTR lstr = 0;

lstr = static_cast<LPC TSTR>(const_cas t<void*>(static _cast<const
void*>(Marshal: :StringToHGloba lAuto(iniFile)) ));

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

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

?!!!!

That's because in your use of StringToHGlobal Auto(), 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::String ToHGlobalAnsi() or STHGUni() as
necessary.
--
Tomas Restrepo
to****@mvps.org
Nov 16 '05 #2
It works. Thank you.

"Tomas Restrepo (MVP)" <to****@mvps.or g> wrote in message
news:um******** ********@TK2MSF TNGP09.phx.gbl. ..
Bragadiru,
I want to read from an ini file :
System::String* windowsFolder = System::Environ ment::SystemDir ectory;

System::String* iniFile = System::IO::Pat h::Combine(wind owsFolder,
"My.ini");

char Class[200];

LPCTSTR lstr = 0;

lstr = static_cast<LPC TSTR>(const_cas t<void*>(static _cast<const
void*>(Marshal: :StringToHGloba lAuto(iniFile)) ));

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

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

That's because in your use of StringToHGlobal Auto(), 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::String ToHGlobalAnsi() 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
2837
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 that number of characters as a string, any character. I know about the def file, using stdcall etc. I envision it as something like LPCSTR _stdcall ReturnAString(int NumChars) but not sure about LPCSTR
8
37891
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 DLL has some legacy code which requires char* in its method parameters. I also have a new file in the DLL which is basically an intermediary -
0
1703
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, CharSet=CharSet.Ansi ) ] public struct MYSTRUCT { public String Text;
4
5005
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 ChartInfo { char symbol; // symbol int period; // period (PERIOD_*) time_t start; // start of chart block time_t end; // end of chart block
13
5871
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 because the file name needs to be built on the fly. How do I get from String * I have to the LPCSTR it wants? I have searched around for this and it seems to be a common question but I can not find a simple answer.
7
3251
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 = reinterpret_cast<LPCSTR>(static_cast<void *>(p)); System::Runtime::InteropServices::Marshal::FreeHGlobal(p);
14
10646
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? But I only get the first character. And when I try to do std::wstring l_s(convert(somestring)) I get really strange characters into l_s string representation, but when I check l_s individual characters they look ok. const wchar_t* convert(...
2
3905
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 we have is picking up the CStrings as not being freed. Based on the CMap documentation, the values are deleted when the CMap is destroyed. (In this case, the value IS the CString itself, not a pointer to a CString) All the examples I see...
4
3654
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 szMask, ...) { CHAR szText;
0
9813
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
10834
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10584
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10248
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
9367
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
5645
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5817
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4446
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.