代码如下:VOID CALLBACK TimeFunc(HWND hwnd, UINT uMsg,UINT idEvent,DWORD dwTime)
{
::Beep(200,200);
nCount++;
return ;
} BOOL APIENTRY DllMain( HANDLE hModule, 
  DWORD  ul_reason_for_call, 
  LPVOID lpReserved)
{
switch ( ul_reason_for_call )
{
case DLL_PROCESS_ATTACH:

nTimeID=::SetTimer(NULL,0,1000,TimeFunc);
if(nCount>=10)
{
KillTimer(NULL,nTimeID);
exit(0);
}
}
break;

default: break; 
}return TRUE;
}
//连一声不响就抛出了异常:(

解决方案 »

  1.   

    //nCount已经声明了;
    //nTimeID=::SetTimer(NULL,0,1000,TimeFunc);
    改成nTimeID=::SetTimer(NULL,1,1000,TimeFunc);
    也没有用.
      

  2.   

    http://www.cpp.atfreeweb.com/ConsoleTimer.html
    The following console program works. It sets a timer using SetTimer then loops in a message loop. The message loop receives and processes WM_TIMER messages and the timer callback also is called for each time interval.#define STRICT 1 
    #include <windows.h>
    #include <iostream.h>VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime) {
    cout << "Time: " << dwTime << '\n';
    cout.flush();
    }int main(int argc, char *argv[], char *envp[]) {
    int Counter=0;
    MSG Msg;
    UINT TimerId = SetTimer(NULL, 0, 500, &TimerProc);
    cout << "TimerId: " << TimerId << '\n';
    if (!TimerId)
    return 16;
    while (GetMessage(&Msg, NULL, 0, 0)) {
    ++Counter;
    if (Msg.message == WM_TIMER)
    cout << "Counter: " << Counter << "; timer message\n";
    else
    cout << "Counter: " << Counter << "; message: " << Msg.message << '\n';
    DispatchMessage(&Msg);
    }
    KillTimer(NULL, TimerId);
    return 0;
    }
    I wrote this program after reading MS KB article Q102482 - INFO: SetTimer() Should Not Be Used in Console Applications which says that SetTimer() in a console application requires a separate thread. The Under The Hood article in the March 1997 issue of MSJ has a couple of sample console programs very similar to mine above. Like mine, they use SetTimer() without a separate thread.