回 帖 发 新 帖 刷新版面

主题:初用API,就出现这些错误码???????

原程序如下:
这是有关想要声卡来简单录一小段语言的程序,虽然有不少错误,但好像都是由HWAVEIN m_hWaveIn,WAVEHDR m_pWaveHdr,int res,int resPrepare这些定义语句引起的,但这样只是定义而已,怎么会用法错误呢?????[em10][em10],请各位指点,谢谢!!!!!


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

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)

{

    /*定义内存缓存大小*/
    unsigned long datasize=4800;

    /*定义波形音频格式*/
    WAVEFORMATEX waveformat;
    waveformat.wFormatTag=WAVE_FORMAT_PCM;
    waveformat.nChannels=1;
    waveformat.nSamplesPerSec=8000;
    waveformat.nAvgBytesPerSec=16000;
    waveformat.nBlockAlign=2;
    waveformat.wBitsPerSample=16;
    waveformat.cbSize=0;

    printf("WAVEFORMATEX size=%d\n",sizeof(WAVEFORMATEX));

    HWAVEIN m_hWaveIn;

    if(waveInGetNumDevs())
        fprintf(stderr,"有可以使用的WaveIn通道\n");
    else
    {
        fprintf(stderr,"没有可以使用的WaveIn通道\n");
        exit(1);
    }

    /*打开音频设备*/
    int res=waveInOpen(&m_hWaveIn,WAVE_MAPPER,&waveformat,(DWORD)NULL,0l,CALLBACK_WINDOW);
    if(res==MMSYSERR_NOERROR)
        fprintf(stderr,"打开waveIn成功\n");
    else
    {
        fprintf(stderr,"打开waveIn通道失败\n");
        exit(2);
    }

    WAVEHDR m_pWaveHdr;

    m_pWaveHdr.lpData=(char *)GlobalLock(GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE,datasize));
    memset(m_pWaveHdr.lpData,0,datasize);
    m_pWaveHdr.dwBufferLength=datasize;
    m_pWaveHdr.dwBytesRecorded=0;
    m_pWaveHdr.dwUser=0;
    m_pWaveHdr.dwFlags=0;
    m_pWaveHdr.dwLoops=0;
    
    /*准备内存录音*/
    int resPrepare=waveInPrepareHeader(m_hWaveIn,&m_pWaveHdr,sizeof(WAVEHDR));

    if(resPrepare==MMSYSERR_NOERROR)
        printf("准备录音用头文件成功\n");
    else
    {
        printf("不能开辟录音头文件,Error_Code=ox%03X\n",resPrepare);
        exit(3);
    }
    resPrepare=waveInAddBuffer(m_hWaveIn,&m_pWaveHdr,sizeof(WAVEHDR));

    if(resPrepare==MMSYSERR_NOERROR)
        printf("准备录音用内存成功\n");
    else
    {
        printf("不能开辟录音用缓存Error_Code=%d\n",resPrepare);
        exit(4);
    }
    /*开始录制声音*/
    if(!(waveInStart(m_hWaveIn))
        printf("开始录制声音\n");
    else
    {
        printf("开始录制声音失败\n");
        exit(5);
    }
    /*将程式序挂起180秒*/
    sleep(180);

    if(!waveInStope(m_hWaveIn))
        printf("停止录音");
    else
        printf("停止录音失败");   

    /*生成文件*/
    HANDLE FileHandle=CreateFile("myspeech.bit",GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FKLE_ATTRIBUTE_NORMAL,NULL);

    if(waveInUnprepareHeader(m_pWaveHdr,&m_pWaveHdr,sizeof(WAVEHDR)))
        printf("Un_PrepareHeader 失败");
    else
        printf("Un_PrepareHeader 成功");

    if(GlobalFree(GlobalHandle(n_pWaveHdr.lpData)))
        printf("Global Free 失败");
    else
        printf("Global Free 成功");

    /*关闭录音设备*/
    if(res==MMSYSERR_NOERROR)
        if(waveInClose(m_hWaveIn)==MMSYSERR_NOERROR)
            printf("正常关闭录音设备");
        else
            printf("非正常关闭录音设备");
    return 0;
}
错误提示如下:
G:\record.c(25) : error C2275: 'HWAVEIN' : illegal use of this type as an expression
        f:\microsoft visual studio\vc98\include\mmsystem.h(530) : see declaration of 'HWAVEIN'
G:\record.c(25) : error C2146: syntax error : missing ';' before identifier 'm_hWaveIn'
G:\record.c(25) : error C2065: 'm_hWaveIn' : undeclared identifier
G:\record.c(36) : error C2143: syntax error : missing ';' before 'type'
G:\record.c(37) : error C2065: 'res' : undeclared identifier
G:\record.c(45) : error C2275: 'WAVEHDR' : illegal use of this type as an expression
        f:\microsoft visual studio\vc98\include\mmsystem.h(569) : see declaration of 'WAVEHDR'
G:\record.c(45) : error C2146: syntax error : missing ';' before identifier 'm_pWaveHdr'
G:\record.c(45) : error C2065: 'm_pWaveHdr' : undeclared identifier
G:\record.c(58) : error C2143: syntax error : missing ';' before 'type'
G:\record.c(60) : error C2065: 'resPrepare' : undeclared identifier
G:\record.c(80) : error C2146: syntax error : missing ')' before identifier 'printf'

G:\record.c(96) : error C2275: 'HANDLE' : illegal use of this type as an expression
        f:\microsoft visual studio\vc98\include\winnt.h(207) : see declaration of 'HANDLE'
G:\record.c(96) : error C2146: syntax error : missing ';' before identifier 'FileHandle'
G:\record.c(96) : error C2065: 'FileHandle' : undeclared identifier
G:\record.c(96) : error C2065: 'FKLE_ATTRIBUTE_NORMAL' : undeclared identifier
G:\record.c(103) : error C2065: 'n_pWaveHdr' : undeclared identifier
G:\record.c(103) : error C2198: 'GlobalHandle' : too few actual parameters

回复列表 (共8个回复)

沙发

HWAVEIN
MSDN里没有你拿里弄出来的~没有的类型当然是错误的拉!

板凳

在MSDN里有啊,怎么没有呢?
Waveform-Audio Input Data Types
The following data types are defined for waveform-audio input functions:

Type Description 
HWAVEIN Handle of an open waveform-audio input device. 
WAVEFORMATEX Structure that specifies the data formats supported by a particular waveform-audio input device. This structure is also used for waveform-audio output devices. 
WAVEHDR Structure used as a header for a block of waveform-audio input data. This structure is also used for waveform-audio output devices. 
WAVEINCAPS Structure used to inquire about the capabilities of a particular waveform-audio input device. 

3 楼

HWAVEIN m_hWaveIn
这里不是定义了输入设备的句柄么???如果不是的或,怎么取得声卡的句柄呢????

4 楼

弄好了,编译通过了,但连接又有错误了,真的头晕了~~~~~
编译错还可以认真检查语法找出来,连接错真不知如何下手弄?????????
--------------------Configuration: record - Win32 Debug--------------------
Linking...
record.obj : error LNK2001: unresolved external symbol __imp__waveInClose@4
record.obj : error LNK2001: unresolved external symbol __imp__waveInUnprepareHeader@12
record.obj : error LNK2001: unresolved external symbol __imp__waveInStop@4
record.obj : error LNK2001: unresolved external symbol __imp__waveInStart@4
record.obj : error LNK2001: unresolved external symbol __imp__waveInAddBuffer@12
record.obj : error LNK2001: unresolved external symbol __imp__waveInPrepareHeader@12
record.obj : error LNK2001: unresolved external symbol __imp__waveInOpen@24
record.obj : error LNK2001: unresolved external symbol __imp__waveInGetNumDevs@0
Debug/record.exe : fatal error LNK1120: 8 unresolved externals
Error executing link.exe.

record.exe - 9 error(s), 0 warning(s)

5 楼

#pragma comment(lib, "winmm.lib") 
这是基本常识

6 楼

我在Link的选项里添加了winmm.lib也弄好了,程序运行都正常,但录不进声音,
我调用waveInStart(m_hWaveIn),是不是就打开了卡声来录音呢??????
还是保存文件不对呢??????怎么生成的文件是0kb呢????????

7 楼

你看这代码哪有保存文件的动作? 建立文件完了就完了,还有仔细看看 MSDN 里
关于 CreateFile 各参数的说明,尤其是标志。 你是不把 sleep(180); 改成
Sleep(180); 了?再仔细看看 MSDN 关于这个函数的说明。这么时间,你一个字还
没说完就已经关闭了。网上这些代码不是拿来就能用的,

8 楼

Windows程序设计22章里有个录音机程序的例子代码.
看看就行了.

我来回复

您尚未登录,请登录后再回复。点此登录或注册