C PlaySound具有奇怪的行为

C PlaySound具有奇怪的行为,c,winapi,C,Winapi,我正在用C编写一个函数,用于播放wav文件。我可以播放一次声音,但我想添加一个循环选项 我有两种工作模式: 从文件名播放 凭记忆演奏 在这两种模式下,我不能播放超过两次的声音,然后函数崩溃 注意:我解决了将此添加到代码中的问题: 布尔WINAPI播放声音(LPCSTR、HMODULE、DWORD) 没有它,我就有问题了 我的代码: #include <windows.h> #include <stdio.h> void play(char * fileName, i

我正在用C编写一个函数,用于播放
wav
文件。我可以播放一次声音,但我想添加一个循环选项

我有两种工作模式:

  • 从文件名播放
  • 凭记忆演奏
在这两种模式下,我不能播放超过两次的声音,然后函数崩溃

注意:我解决了将此添加到代码中的问题:

布尔WINAPI播放声音(LPCSTR、HMODULE、DWORD)

没有它,我就有问题了

我的代码:

#include <windows.h>
#include <stdio.h>

void play(char * fileName, int repeat);
char* file2vector(char* fileName, long int* size);

int main(int argc, char ** argv)
{
    if (argc > 1) {
        play(argv[1], 5);
    }

}


void play(char * fileName, int repeat)
{
#define SND_SYNC 0
#define SND_ASYNC 1
#define SND_FILENAME 0x20000
#define SND_NODEFAULT 2
#define SND_MEMORY 4
#define SND_NOSTOP 16

    int mode = SND_SYNC | SND_NODEFAULT | SND_NOSTOP;
    char * sound;
    int play = 1;
    int i;

    long int size;
    unsigned char* wavFile = file2vector(fileName, &size);

    if (wavFile == NULL) {
        mode |= SND_FILENAME;
        sound = fileName;
        printf("filename\n");
    }
    else {
        mode |= SND_MEMORY;
        sound = wavFile;
        printf("memory\n");

    }


    if (repeat) {
        play += repeat;
    }

    printf("play %d times\n", play);

    int res;
    for (i = 1; i <= play; ++i) {
        printf("played %i\n", i);
        res = PlaySound(sound, NULL, mode);
        printf("res:%d\n", res);
    }

    PlaySound(NULL, 0, 0);
    free(wavFile);

    printf("ready");


}

char* file2vector(char* fileName, long int* size)
{
    char* vector = NULL;
    FILE* file = fopen(fileName, "rb");

    if (NULL == file) {
        *size = 0L;
    }
    else
    {
        fseek(file, 0L, SEEK_END);
        *size = ftell(file);
        fseek(file, 0L, SEEK_SET);

        /* ftell can return -1 on failure */
        if (*size <= 0) {
            *size = 0L;

        }
        else
        {
            vector = (char*)malloc(*size);
            if (NULL != vector) {
                fread(vector, sizeof(char), *size, file);
            }
        }

        fclose(file);
    }

    return vector;     
}
它打印:

memory
play 6 times
played 1
res:1
played 2
res:1
played 4198705

在我的电脑里,代码工作正常。即使我播放文件更多次。 输出为:

C:\Users\avesudra\*****\***\*****\example\bin\Debug>example.exe c:\windows\media\chimes.wav memory play 8 times played 1 res:1 played 2 res:1 played 3 res:1 played 4 res:1 played 5 res:1 played 6 res:1 played 7 res:1 played 8 res:1 ready C:\Users\avesudra\******\*********\example\bin\Debug>example.exe C:\windows\media\chimes.wav 记忆 玩8次 第1场 决议:1 第二场 决议:1 第三场 决议:1 第四场 决议:1 打5 决议:1 打6 决议:1 打7 决议:1 打8 决议:1 准备好的 这很奇怪。如果您愿意,您可以从这里下载该可执行文件以试用,并查看是否有效:

当您单步执行循环时,调试器向您显示了什么?谢谢。我使用的是tiny c,我添加了这个头,现在我编译并运行ok,没有它,运行时执行失败。布尔WINAPI播放声音(LPCSTR、HMODULE、DWORD);奇怪的是,我的代码是在gcc上编译的,可能是由编译器编译的,但我不知道。可能是以前用错误的调用约定调用的?
WINAPI
位起作用。 C:\Users\avesudra\*****\***\*****\example\bin\Debug>example.exe c:\windows\media\chimes.wav memory play 8 times played 1 res:1 played 2 res:1 played 3 res:1 played 4 res:1 played 5 res:1 played 6 res:1 played 7 res:1 played 8 res:1 ready