来自String的LPCSTR [英] LPCSTR from String
问题描述
我正在使用VS 2003编写托管C ++ Windows应用程序。
我正在调用PlaySound。第一个参数是LPCSTR。我有一个表示文件名的String
* str。我喜欢使用String类,因为文件
名称需要动态构建。
如何从String获取*我需要它想要的LPCSTR?我已经搜索过这个问题,这似乎是一个常见的问题,但我找不到一个简单的答案。
谢谢,
Joe
这似乎是我找到的最简单方法。首先要有一个缓冲区
足够大或根据yourString->长度动态分配一个。
char buffer [512];
sprintf(buffer,"%s",yourString);
mosimu
" Joe Thompson"写道:
我正在使用VS 2003编写托管C ++ Windows应用程序。
我正在调用PlaySound。第一个参数是LPCSTR。我有一个String
* str表示文件名。我喜欢使用String类,因为文件
名称需要动态构建。
我如何从String *获得它需要的LPCSTR?我已经搜索了这个,这似乎是一个常见的问题,但我找不到一个简单的答案。
谢谢你,
Joe
" Joe Thompson" <乔********* @ discussions.microsoft.com>在消息中写道
新闻:D1 ********************************** @ microsof t.com ...我正在使用VS 2003编写托管C ++ Windows应用程序。
我正在调用PlaySound。第一个参数是LPCSTR。我有一个String
* str表示文件名。我喜欢使用String类,因为
文件名称需要动态构建。
我如何从String *获得它想要的LPCSTR?
我不确定你的问题是否与ANSI / UNICODE或
托管/非托管问题有关。
设置阶段:
PlaySound实际上是一个宏,相当于ANSI构建中的PlaySoundA和UNICODE版本中的
PlaySoundW。
LPCSTR是指向ANSI字符串的指针。
..Net字符串由UNICODE字符组成。
那就是说,这可以通过固定字符串(防止它被GC移动到
),获得指向字符串第一个字符的指针,并传递
关闭本机函数的指针:
#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);
问候,
将
你好=?Utf- 8?B?Sm9lIFRob21wc29u?=,
我正在使用VS 2003编写托管C ++ Windows应用程序。
我正在调用PlaySound。第一个参数是LPCSTR。我有一个
String * str表示文件名。我喜欢使用String类
因为文件名需要动态构建。
我如何从String *获得它需要的LPCSTR?我已经四处寻找这个,这似乎是一个常见的问题,但我找不到简单的答案。
参见:如何在Visual C ++ .NET中从System :: String *转换为Char *
http://support.microsoft.com/kb/311259/
对于TCHAR支持,请参阅:从System :: String *转换为TCHAR * / CString
http://blog.kalmbachnet.de/?postid=18
-
问候
Jochen
关于Win32和我的博客.NET
http://blog.kalmbachnet.de/
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
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
"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
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/
这篇关于来自String的LPCSTR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!