简体   繁体   English

如何将 const char* 作为参数传递给 LPCSTR 参数?

[英]How to pass in a const char* as an argument to an LPCSTR parameter?

I'd like to use this function to call the the PlaySoundA function, but the sound file I am trying to play does not open.我想用这个 function 来调用 PlaySoundA function,但是我要播放的声音文件打不开。

void AudioClass::playAudio(const char* incomingData, const char* filePath)
{

    char buffer[100]; // <- danger, only storage for 100 characters.
    strncpy_s(buffer, filePath, sizeof(buffer));
    strncat_s(buffer, incomingData, sizeof(buffer));

    PlaySoundA(buffer, NULL, SND_FILENAME);
}

As I found out, if the argument for the LPCSTR pszSound parameter of PlaySoundA() is given as a string (eg "C:/Users/User/Documents/sound.wav"), ie the full path of the file is given, the sound will play.我发现,如果 PlaySoundA() 的 LPCSTR pszSound 参数的参数以字符串形式给出(例如“C:/Users/User/Documents/sound.wav”),即给出文件的完整路径,声音将播放。

I would like to combine the incomingData parameter (file name) and the filePath parameter, for which I used a buffer.我想将incomingData 参数(文件名)和filePath 参数结合起来,为此我使用了一个缓冲区。

If the file cannot be found, the default system sound should play (it was a beep in my case), but this is no longer the case;如果找不到文件,则应播放默认系统声音(在我的情况下是哔哔声),但不再是这种情况; even though SND_NODEFAULT flag was not set, in which case PlaySoundA() would return silently, without playing the default system sound.即使未设置 SND_NODEFAULT 标志,在这种情况下 PlaySoundA() 也会静默返回,不会播放默认系统声音。

https://docs.microsoft.com/en-us/previous-versions/dd743680(v%3Dvs.85) https://docs.microsoft.com/en-us/previous-versions/dd743680(v%3Dvs.85)

Is it possible that the argument I am trying to pass in not compatible with LPCSTR pszSound parameter?我试图传递的参数是否可能与 LPCSTR pszSound 参数不兼容?

While running the debugger, the buffer variable seems to hold the whole path (C:/Users/User/Documents/sound.wav\r\n), but why is there a \r\n prefix at the end?运行调试器时,buffer 变量似乎保存了整个路径(C:/Users/User/Documents/sound.wav\r\n),但为什么末尾有 \r\n 前缀?

Definitions:定义:

#define MAX_DATA_LENGTH 255

char incomingData[MAX_DATA_LENGTH];

const char* filePath {"C:/Users/User/Desktop/"};

//Arduino SerialPort object
SerialPort *arduino;

//Audio file AudioClass object
AudioClass* audio;

playAudio() is called here: playAudio() 在这里被调用:

void exampleReceiveData(void)
{
    int readResult = arduino->SerialPort::readSerialPort(incomingData, MAX_DATA_LENGTH);

    audio->AudioClass::playAudio(incomingData, filePath);

    Sleep(10000);
}

EDIT_1: the length of the real file path I use is shorter than 100 characters. EDIT_1:我使用的真实文件路径的长度小于 100 个字符。

EDIT_2: I get this warning: String 'buffer' might not be zero-terminated. EDIT_2:我收到此警告:字符串“缓冲区”可能不是以零结尾的。

The \r\n at the end comes from the serial port: https://www.arduino.cc/reference/en/language/functions/communication/serial/println末尾的\r\n来自串口: https://www.arduino.cc/reference/en/language/functions/communication/serial/println

You can skip the characters when copying:复制时可以跳过字符:

    strncat_s(buffer, incomingData, strnlen(incomingData) - 2);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将静态const char作为参数传递给函数? - How to pass a static const char as parameter to a function? “HMODULE LoadLibraryA(LPCSTR)”:无法将参数 1 从“const _Elem *”转换为“LPCSTR” - 'HMODULE LoadLibraryA(LPCSTR)': cannot convert argument 1 from 'const _Elem *' to 'LPCSTR' “ const char **”类型的参数与“ const char *”类型的参数不兼容 - Argument of type “const char **” is incompatible with parameter of type “const char *” 将类方法传递给const char *参数 - Pass class method to const char * parameter 错误:无法在 C++ 中将 'const CHAR**' {aka 'const char**'} 转换为 'LPCSTR' {aka 'const char*'} - error: cannot convert 'const CHAR**' {aka 'const char**'} to 'LPCSTR' {aka 'const char*'} in C++ “LPCWSTR”类型的参数与“LPCSTR”类型的参数不兼容 - Argument of type “LPCWSTR” is incompatible of the parameter of type “LPCSTR” “unsigned char *”类型的参数与“const char *”类型的参数不兼容 - Argument of type "unsigned char *" is incompatible with parameter of type "const char *" 类型“ const char *”的参数与类型“ char *”的参数不兼容。 但为什么? :( - Argument of type “const char*” is incompatible with parameter of type “char*”. But why? :( “const char *”类型的参数与“char *”类型的参数不兼容 - Argument of type “const char *” is incompatible with parameter of type “char *” C++ “char”类型的参数与“const char”类型的参数不兼容 - C++ Argument of type “char” is incompatible with parameter of type “const char”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM