How to pass const char* as parameter of LPCSTR parameter?


Gábor Marschall

I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open.

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 to the LPCSTR pszSound parameter of PlaySoundA() is given as a string (eg "C:/Users/User/Documents/sound.wav"), i.e. the full path to the file is given, the sound will play.

I want to combine incoming data parameter (filename) and filePath parameter, for this I use buffer.

If the file is not found, the default system sound (beep in my case) should play, but this is no longer the case. Even if the SND_NODEFAULT flag is not set, PlaySoundA() will return silently in this case without playing the default system sound.

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

Is it possible that the parameter I am trying to pass is not compatible with the LPCSTR pszSound parameter?

When running the debugger, the buffer variable seems to hold the entire path (C:/Users/User/Documents/sound.wav\r\n), but why is there a \r\n prefix at the end?

definition:

#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;

Call playAudio() here:

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

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

    Sleep(10000);
}

EDIT_1: The real file path I'm using is less than 100 characters long.

EDIT_2: I get this warning: String 'buffer' may not be zero-terminated.

cosmos

Will \r\ncome from the serial port at the end: https://www.arduino.cc/reference/en/language/functions/communication/serial/println

You can skip the following characters when copying:

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

Related


Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass const char instead of std::string as function parameter

Abruzzo Forte and Gentile This is a newbie question, but I don't understand how it works. Suppose I have the following function void foo(const std::string& v) { cout << v << endl; } and the call in my program below. foo("hi!"); Essentially, I'm passing a

How to pass const char* as parameter of LPCSTR parameter?

Gábor Marschall I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open. void AudioClass::playAudio(const char* incomingData, const char* filePath) { char buffer[100]; // <- danger, only storage for 100

How to pass const char* as parameter of LPCSTR parameter?

Gábor Marschall I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open. void AudioClass::playAudio(const char* incomingData, const char* filePath) { char buffer[100]; // <- danger, only storage for 100

Swift pointer to const char parameter

Ben When interacting with a C api in Swift (SQLite 3 C api), I need to pass a pointer to a pointer to a const char as an output parameter: sqlite3_prepare_v2( // snip other parameters const char **pzTail // OUT parameter ); Translate it to Swift: sqlite3_prep

How to pass const char* as parameter of LPCSTR parameter?

Gábor Marschall I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open. void AudioClass::playAudio(const char* incomingData, const char* filePath) { char buffer[100]; // <- danger, only storage for 100

Pass const char in argv main parameter

Ben I have the following: int main(int argc, char *argv[]) { char *INPUTFILE_DATABASE = ""; strcpy(INPUTFILE_DATABASE, argv[1]); if(INPUTFILE_DATABASE[0]=='\0') { cout << "No input file given" << endl; INPUTFILE_DATABASE

cannot pass char *** as parameter

Bisla It's easy to see the tasks to be done here. Read a list of files and pass it to another function. Why doesn't this work. When I try to store the filename in a local char** it works fine, but I can't send it back via a pointer. gives segmentation fault. i

How to pass const char* as parameter of LPCSTR parameter?

Gábor Marschall I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open. void AudioClass::playAudio(const char* incomingData, const char* filePath) { char buffer[100]; // <- danger, only storage for 100

How to pass char pointer as parameter in ctypes python

Naveen kumar Katta rathanaiah Please help me to convert the following line of C++ code to ctypes python: Ret = openFcn(&Handle, "C:\\Config.xml"); Here are the declarations for each: typedef uint16_t (* OpenDLLFcnP)(void **, const char *); OpenDLLFcnP openFcn

'char const *str' as template parameter

Wisviva Lets say I have a template: template<char const *str> class Template { ... }; Why can't the following be written? Template<"literal"> T; or char const *s = "Literal"; Template<s> T; Why does the following approach work? char const s[] = "Literal"; T

How to pass const char* as parameter of LPCSTR parameter?

Gábor Marschall I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open. void AudioClass::playAudio(const char* incomingData, const char* filePath) { char buffer[100]; // <- danger, only storage for 100

How to pass const char* as parameter of LPCSTR parameter?

Gábor Marschall I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open. void AudioClass::playAudio(const char* incomingData, const char* filePath) { char buffer[100]; // <- danger, only storage for 100

How to pass const char* as parameter of LPCSTR parameter?

Gábor Marschall I want to use this function to call the PlaySoundA function, but the sound file I want to play doesn't open. void AudioClass::playAudio(const char* incomingData, const char* filePath) { char buffer[100]; // <- danger, only storage for 100

cannot pass char *** as parameter

Bisla It's easy to see the tasks to be done here. Read a list of files and pass it to another function. Why doesn't this work. When I try to store the filename in a local char** it works fine, but I can't send it back via a pointer. gives segmentation fault. i