A problem about char and wchar_t

HeW 1 Reputation point
2021-12-10T17:11:29.19+00:00

I wanna add some sounds in my programme,so I used the "PlaySound".
But the problem is this.

156762-image.png

(I include the"windows.h".
I use MinGW64)

And then it says "cannot convert 'const wchar_t*' to 'LPCTSTR {aka const char*}' in initialization".

I used many ways to try working out the problem,but they didn't work at all!

I just wanna hear the Error sound.

(Hope you can understand my words,thank you!!!)

ADD:

OK.
I have tried all way.
And if there's no any Error,it won't a EXE FILE there(???
So I use cmd to compile the CPP FILE,it says"undefined reference to `PlaySoundA@12"'.
WHAT IS THIS????I HAVEN'T SEEN!!!

Supplement:
I use vscode and I don't know how to check my Character Set.
I use the GNU gdb (GDB) 7.6.1 from MinGW64 to run.
When I install MinGW,I just click this button:
158618-test.png

and I do nothing to it.

Developer technologies | C++
{count} votes

7 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-12-10T18:33:17.56+00:00

    If you want to use LPCTSTR, then:

    LPCTSTR t = _T( "Error.wav");

    If you want to use L, then:

    LPCWSTR t = L"Error.wav";
    PlaySoundW(t, . . .);

    1 person found this answer helpful.

  2. Castorix31 90,686 Reputation points
    2021-12-11T07:04:51.173+00:00

    You should use Unicode in your project :

    156759-unicode.jpg

    1 person found this answer helpful.

  3. WayneAKing 4,931 Reputation points
    2021-12-10T17:48:56.217+00:00

    Did you try it without the "L" prefix?

    If so, what happened?

    • Wayne

  4. WayneAKing 4,931 Reputation points
    2021-12-11T07:26:11.393+00:00

    In VS (2017) the following compiles and links
    just fine when UNICODE is not defined.

    #include <windows.h>
    
    int main()
    {
        LPCTSTR t = "error.wav";
        PlaySound(t, NULL, SND_FILENAME | SND_ASYNC);        
    }
    

    With Winmm.lib added to the linker Input as an
    Additional Dependency in the project properties.

    If UNICODE is defined, this compiles and links
    just fine.

    #include <windows.h>
    
    int main()
    {
        LPCTSTR t = L"error.wav";
        PlaySound(t, NULL, SND_FILENAME | SND_ASYNC);    
    }    
    
    • Wayne

  5. Petrus 【KIM】 546 Reputation points
    2021-12-13T06:37:30.663+00:00

    I think your project's Character Set is "Use Multi-Byte Character Set" or "Not Set".
    Just use Preprocessor directives

    #ifdef UNICODE  
        LPCTSTR sz = L"AAA";  
    #else  
        LPCTSTR sz = "AAA";  
    #endif  
      
        PlaySound(sz, NULL, SND_FILENAME);  
    

    157062-image.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.