LPCSTR from String

G

Guest

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
 
G

Guest

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
 
W

William DePalo [MVP VC++]

Joe Thompson said:
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\\media\\windows xp
startup.wav");
wchar_t __pin *p = PtrToStringChars(str);

PlaySoundW(p, 0, SND_FILENAME | SND_SYNC);

Regards,
Will
 
J

Jochen Kalmbach

Hi =?Utf-8?B?Sm9lIFRob21wc29u?=,
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/
 
G

Guest

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
 
J

Jochen Kalmbach

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/
 
G

Guest

Hi mosimu,

This seems to work fine.

Thanks for the help,
Joe

mosimu said:
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 said:
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
 
G

Guest

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
 
C

Carl Daniel [VC++ MVP]

Joe said:
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
 
W

William DePalo [MVP VC++]

Joe Thompson said:
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
 
G

Guest

Thanks for the clarification. After reading this and Carl's response, I
guess this approach is better...

Joe
 
S

Serge Baltic

m> This seems to be the easiest way that I have found. First have a
m> buffer large enough or dynamically allocate one based on
m> yourString->Length.
m>
m> char buffer[512]; sprintf(buffer, "%s", yourString);

First, you should use StringCchPrintf instead of sprtinf to avoid buffer
overflow.

Second, the code above is just equal to writing (LPCTSTR)yourString, so why
use it?!
 
J

Jochen Kalmbach

Hi William DePalo [MVP VC++],
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.

Just a small note: Win9x/Me does not support 16-bit chars by default.
And therefor will not work with PlaySoundW.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
W

William DePalo [MVP VC++]

Jochen Kalmbach said:
Just a small note: Win9x/Me does not support 16-bit chars by default.
And therefor will not work with PlaySoundW.

Absolutely true, which is whay I wrote in the next paragraph after the line
you quoted: "Note that NT operating systems (NT/2K/XP/2K+3) all use 16 bit
characters
internally." For the sake of completeness, 9x doesn't support PlaySoundW()
by default, but it does in the presence of MSLU.

Regards,
Will
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top