Openfilename playing sound win32

I have the following code which gets the name of a .mp3 file and passes it to a function to play it. It works if i set szFile to the name of a file manually in the source, but when i have it pass szFile to the function, it won't play
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
OPENFILENAME ofn;       // common dialog box structure
                   // buffer for file name
                          // owner window
            HANDLE hf;              // file handle

            // Initialize OPENFILENAME
            ZeroMemory(&ofn, sizeof(ofn));
            ofn.lStructSize = sizeof(ofn);
            ofn.hwndOwner = hWnd;
            ofn.lpstrFile = szFile;
            // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
            // use the contents of szFile to initialize itself.
            ofn.lpstrFile[0] = '\0';
            ofn.nMaxFile = sizeof(szFile);
            ofn.lpstrFilter = "Sound\0*.MP3\0All\0*.*\0";
            ofn.nFilterIndex = 1;
            ofn.lpstrFileTitle = NULL;
            ofn.nMaxFileTitle = 0;
            ofn.lpstrInitialDir = NULL;
            ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;// | OFN_NOCHANGEDIR;

            // Display the Open dialog box.

            if (GetOpenFileName(&ofn)==TRUE)
                hf = CreateFile(ofn.lpstrFile,
                                GENERIC_READ,
                                0,
                                (LPSECURITY_ATTRIBUTES) NULL,
                                OPEN_EXISTING,
                                FILE_ATTRIBUTE_NORMAL,
                                (HANDLE) NULL);

            for(int i = 0; i < 500;i++)
            {
                if(szFile[i] == 92)
                {
                    szFile[i] = 47;
                   // std::cout << (int)szFile[i];
                }
                if(szFile[i] == 0)
                {
                    break;
                }
            }
PlaytheSound(szFile);


1
2
3
4
5
6
7
8
9
10
int PlaytheSound(char soundfile[500])
{


    int b = Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 4096);
    std::cout << b;
    Mix_Music *music = Mix_LoadMUS(soundfile);
    int a = Mix_PlayMusic(music, 0);
    std::cout << a;
}


First
1
2
3
                if(szFile[i] == 92)
                {
                    szFile[i] = 47;
What are you doing here?

And second, you could cout << szFile before PlaytheSound, to see what the string is

Edit:
1
2
3
4
            ofn.lpstrFile = szFile;
            // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
            // use the contents of szFile to initialize itself.
            ofn.lpstrFile[0] = '\0';

Wtf? i think youre supposed to do szFile[0] = 0 not ofn.lpstrFile[0], that messes up / removes the pointer to szFile
Last edited on
That first bit replaces \ with /. I have cout'd szFile and it gives me the file name, eg. C:/Users/beep.mp3. And if i use PlaytheSound("C:/Users/beep.mp3"); it works, just not when i get it using the openfile window.
The code i got from MSDN so im pretty sure its correct.
Topic archived. No new replies allowed.