473,387 Members | 1,464 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,387 software developers and data experts.

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 5823
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*********@discussions.microsoft.com> wrote in message
news:D1**********************************@microsof t.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\\media\\windows xp
startup.wav");
wchar_t __pin *p = PtrToStringChars(str);

PlaySoundW(p, 0, SND_FILENAME | SND_SYNC);

Regards,
Will

Nov 17 '05 #3
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/
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*********@discussions.microsoft.com> wrote in message
news:D1**********************************@microsof t.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\\media\\windows xp
startup.wav");
wchar_t __pin *p = PtrToStringChars(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*********@discussions.microsoft.com> wrote in message
news:D1**********************************@microsof t.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\\media\\windows xp
startup.wav");
wchar_t __pin *p = PtrToStringChars(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*********@discussions.microsoft.com> wrote in message
news:BD**********************************@microsof t.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
Thanks for the clarification. After reading this and Carl's response, I
guess this approach is better...

Joe

"William DePalo [MVP VC++]" wrote:
"Joe Thompson" <Jo*********@discussions.microsoft.com> wrote in message
news:BD**********************************@microsof t.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 #11
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?!

--
Serge

Nov 17 '05 #12
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/
Nov 17 '05 #13
"Jochen Kalmbach" <no********************@holzma.de> wrote in message
news:Xn*********************************@127.0.0.1 ...
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
Nov 17 '05 #14

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

Similar topics

16
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......
5
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...
2
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...
3
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
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...
2
by: Neo | last post by:
how convert LPCSTR to LPCWSTR? regards, Mohammad Omer Nasir.
7
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" ,...
1
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
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.