473,804 Members | 3,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LPCSTR from String

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.

Thank you,
Joe
Nov 17 '05 #1
13 5868
This seems to be the easiest way that I have found. First have a buffer
large enough or dynamically allocate one based on yourString->Length.

char buffer[512];
sprintf(buffer, "%s", yourString);

mosimu

"Joe Thompson" wrote:
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.

Thank you,
Joe

Nov 17 '05 #2
"Joe Thompson" <Jo*********@di scussions.micro soft.com> wrote in message
news:D1******** *************** ***********@mic rosoft.com...
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'm not sure if your question has to do with ANSI/UNICODE or
managed/unmanaged issues.

To set the stage:

PlaySound is actually a macro that equates to PlaySoundA in ANSI builds and
PlaySoundW in UNICODE builds.

An LPCSTR is a pointer to an ANSI string.

..Net strings are composed of UNICODE characters.

That said, this works by pinning the string (prevent it from being moved by
the GC), getting a pointer to the first character of the string, and passing
off the pointer to a native function:

#include <vcclr.h>

System::String *str = new System::String( "C:\\windows\\m edia\\windows xp
startup.wav");
wchar_t __pin *p = PtrToStringChar s(str);

PlaySoundW(p, 0, SND_FILENAME | SND_SYNC);

Regards,
Will

Nov 17 '05 #3
Hi =?Utf-8?B?Sm9lIFRob21 wc29u?=,
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.


See: How To Convert from System::String* to Char* in Visual C++ .NET
http://support.microsoft.com/kb/311259/

For TCHAR support see: Convert from System::String* to TCHAR*/CString
http://blog.kalmbachnet.de/?postid=18

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #4
I thinnk this has more importance as a managed/unmanaged issue. I don't know
much about LPCSTR, but belive this can hold pointer to both ANSI and UNICODE
string. Up from NT5 windows uses unicode, can some one explane on these
context

"William DePalo [MVP VC++]" wrote:
"Joe Thompson" <Jo*********@di scussions.micro soft.com> wrote in message
news:D1******** *************** ***********@mic rosoft.com...
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'm not sure if your question has to do with ANSI/UNICODE or
managed/unmanaged issues.

To set the stage:

PlaySound is actually a macro that equates to PlaySoundA in ANSI builds and
PlaySoundW in UNICODE builds.

An LPCSTR is a pointer to an ANSI string.

..Net strings are composed of UNICODE characters.

That said, this works by pinning the string (prevent it from being moved by
the GC), getting a pointer to the first character of the string, and passing
off the pointer to a native function:

#include <vcclr.h>

System::String *str = new System::String( "C:\\windows\\m edia\\windows xp
startup.wav");
wchar_t __pin *p = PtrToStringChar s(str);

PlaySoundW(p, 0, SND_FILENAME | SND_SYNC);

Regards,
Will

Nov 17 '05 #5
Hi =?Utf-8?B?Qmlqb3k=?=,
I don't know much about LPCSTR, but belive this can hold pointer
to both ANSI and UNICODE string.
No, LPCSTR only handles ANSI (or MBCS) strings (char*)

To support bith you should use LPCTSTR.
Up from NT5 windows uses unicode, can some
one explane on these context

???

Starting with NT (3.01) it uses unicode as fundamental string encoding!

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #6
Hi mosimu,

This seems to work fine.

Thanks for the help,
Joe

"mosimu" wrote:
This seems to be the easiest way that I have found. First have a buffer
large enough or dynamically allocate one based on yourString->Length.

char buffer[512];
sprintf(buffer, "%s", yourString);

mosimu

"Joe Thompson" wrote:
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.

Thank you,
Joe

Nov 17 '05 #7
Hi William,

Your approach worked also. I am using ANSI builds though. Unless I
explicitly call PlaySoundW I get a compiler error:

error C2664: 'PlaySoundA': cannot convert parameter 1 from 'wchar_t __pin
*volitile ' to 'LPCSTR'

I think I'll use mosimu's approach for now, it seems simpler.

Thank you for the help,
Joe

"William DePalo [MVP VC++]" wrote:
"Joe Thompson" <Jo*********@di scussions.micro soft.com> wrote in message
news:D1******** *************** ***********@mic rosoft.com...
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'm not sure if your question has to do with ANSI/UNICODE or
managed/unmanaged issues.

To set the stage:

PlaySound is actually a macro that equates to PlaySoundA in ANSI builds and
PlaySoundW in UNICODE builds.

An LPCSTR is a pointer to an ANSI string.

..Net strings are composed of UNICODE characters.

That said, this works by pinning the string (prevent it from being moved by
the GC), getting a pointer to the first character of the string, and passing
off the pointer to a native function:

#include <vcclr.h>

System::String *str = new System::String( "C:\\windows\\m edia\\windows xp
startup.wav");
wchar_t __pin *p = PtrToStringChar s(str);

PlaySoundW(p, 0, SND_FILENAME | SND_SYNC);

Regards,
Will

Nov 17 '05 #8
Joe Thompson wrote:
Hi William,

Your approach worked also. I am using ANSI builds though. Unless I
explicitly call PlaySoundW I get a compiler error:

error C2664: 'PlaySoundA': cannot convert parameter 1 from 'wchar_t
__pin
*volitile ' to 'LPCSTR'
Yes, that's why Wiliam explicitly used PlaySoundW in his example.

I think I'll use mosimu's approach for now, it seems simpler.


It may seem so, but it's not really the best choice:

- It's susceptible to buffer overrun errors (at least use _snprintf).
- It incurs the overhead of converting from Unicode to ANSI (in the implicit
conversion that occurs before sprintf is called).
- It probably incurs the overhead of converting from ANSI to Unicode since
most likely PlaySoundA internally converts it's arguments to Unicode and
then call PlaySoundW.
- It's less efficient.

Personally, I'd go with William's suggestion.

-cd
Nov 17 '05 #9
"Joe Thompson" <Jo*********@di scussions.micro soft.com> wrote in message
news:BD******** *************** ***********@mic rosoft.com...
Your approach worked also.
That's good. I try hard not to give bogus answers to questions posted here.
:-)
I am using ANSI builds though. Unless I
explicitly call PlaySoundW I get a compiler error:


Right.

That's to be expected. Dot Net strings are composed of 16 bit characters.
PlaySoundA() which is what you are calling takes 8 bit characters,
PlaySoundW() 16. There is no actual PlaySound() - merely a macro that is
defined to be one or the other of the A and W variants.

Note that NT operating systems (NT/2K/XP/2K+3) all use 16 bit characters
internally. So, almost any time a string is passed to an o/s exported API,
it is widened to 16 bit characters in the in the A version. And almost all
o/s exported APIs returning strings return them as strings of 16 bit
characters. So the A version of such a function has to "narrow" them on
return.

Regards,
Will
Nov 17 '05 #10

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

Similar topics

16
6757
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site... this number is always changing as I have hundreds of thousand of pages of text on my site. Problem: - in my opinion this just not only look weird, but the variable "n" (number)
5
31183
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it will split myString up using the delimiter of 1 space so that
2
3867
by: Bragadiru | last post by:
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; LPCTSTR lstr = 0;
3
14710
by: George Ter-Saakov | last post by:
How do i call from managed C++ function wich accepts LPCSTR? void AddName( System::String *sName ) { AddName( (LPCSTR) sName )); -- does not compile. } George.
6
2253
by: John Lucas | last post by:
I wish to call an unmanaged function from vb delacred as so: #define DllExport __declspec(dllexport) DllExport int WINAPI DllAlcWrite(HANDLE, LPCSTR, int); I have tried: 1.) Private Declare Ansi Function DllAlcWrite Lib "alcsapi" (ByVal hConn
2
7902
by: Neo | last post by:
how convert LPCSTR to LPCWSTR? regards, Mohammad Omer Nasir.
7
8923
by: Toni | last post by:
Hello Everyone , I'm trying to convert an LPCSTR to LPVOID for use in DeviceIOControl. But when i use LPVOID test = (LPVOID)test_lpcstr then it doesn't work , When i use LPVOID test = "TEST" , then it works , so it must have something to do with the conversion. Any ideas ? Note: i'm very new to C++ Sorry for my poor english Edit: DeviceIoControl(driverHandle, CHANGE_PREFIX, "TEST", sizeof("TEST"), szTemp, sizeof(szTemp), &dwReturn,...
1
1643
by: vb739 | last post by:
Can something like this be done: enum ABC { A = "A", B = "B", C = "C" }; , that is can a const string address be used as an enum value?
2
3904
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...
0
9705
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
10323
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
10074
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
9138
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...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
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
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
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
3
2988
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.