Windows下写一个定时中断的程序,用MinGW编译

xiaoyu8995 2015-01-08 10:33:25
找到一个时钟中断的例子,http://blog.csdn.net/sunrier/article/details/6440029用minGW编译老是出现 error: variable or field 'interrupt' declared void的错误,编译不过去,是为什么啊?
我现在需要写一个时钟中断的程序,定时触发回调函数进行一些操作,类似于SetTimer或Linux下的setitimer函数,但必须是用MinGW编译通过,大神们给点启发吧
...全文
717 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoyu8995 2015-01-25
  • 打赏
  • 举报
回复
已解决 示例时钟
#include <Windows.h>
#include <stdio.h>
#include <iostream>
#pragma comment(lib, "Winmm.lib")


using namespace std;


void CALLBACK TimeProc(
    UINT uID,
    UINT uMsg,
    DWORD dwUser,
    DWORD dw1,
    DWORD dw2);




int flag = 0;
int main(int argc, _TCHAR* argv[])
{


//启动计时器
   /* timeSetEvent(1000, 1, TimeProc, 0, TIME_PERIODIC);
    int a;
    scanf("%d", &a);*/
    MMRESULT nIDTimerEvent = timeSetEvent(
        40,//延时0.04秒
        0,
        TimeProc,
        0,
        (UINT)TIME_PERIODIC);


    if( nIDTimerEvent == 0 )
      cout<< "启动计时器失败" << endl;

    int a;
    scanf("%d", &a);
    return 0;
}


//回调过程(时钟到来,回调函数被系统自动调用)
void CALLBACK TimeProc(
    UINT uID,
    UINT uMsg,
    DWORD dwUser,
    DWORD dw1,
    DWORD dw2)
{
    cout<< "时钟到来" << endl;
    flag ++;
    cout << "flag is :" << flag << "   in timeProc" << endl;
}
程序如下
mymtom 2015-01-12
  • 打赏
  • 举报
回复
用SetTimer呀!

#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

#include <time.h>
#include <tchar.h>
#include <windows.h>

#define ID_TIMER_SECOND	1

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("TimerWindowsApp");

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T("Timer Windows App"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

	SetTimer(hwnd, ID_TIMER_SECOND, 1000, NULL);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
        	KillTimer(hwnd, ID_TIMER_SECOND);
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;

		case WM_TIMER:
			{
                time_t tks;
                struct tm tms;
				TCHAR buf[20];

				tks = time(NULL);
				tms = *localtime(&tks);
				_tcsftime(buf, sizeof(buf), "%H:%M:%S", &tms);
				SetWindowText(hwnd, buf);
			}
			break;

        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

xiaoyu8995 2015-01-12
  • 打赏
  • 举报
回复
cygwin是不是比mingw稳定些啊?功能差不多??
mujiok2003 2015-01-09
  • 打赏
  • 举报
回复
mingw在windows下不稳定, 技术支持也不及时, 不建议在生产环境中使用。
lm_whales 2015-01-09
  • 打赏
  • 举报
回复
不支持 interrupt 关键字的编译器,中断例程,用汇编语言实现吧 因为中断函数确实不同于 一般函数, 中断函数 用 iret 指令返回, 一般函数映 ret 指令返回 另外,一般现代操作系统,已经不支持直接的硬件操作了(包括中断),要编写中断服务程序,硬件支持程序, 一般来说,要编写 硬件驱动程序才可以
tangtangtangbaoli 2015-01-09
  • 打赏
  • 举报
回复
可以用cygwin阿 同样提供linux平台下的for win32的gcc,make等等
赵4老师 2015-01-08
  • 打赏
  • 举报
回复
你没赶脚我2楼的代码Windows和Linux通吃吗?
xiaoyu8995 2015-01-08
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
MinGW不是号称在Windows下完全兼容Linux吗? 在Linux下怎么做就在MinGW下怎么做。
首先Linux下的一些库,比如sys/time.h,windows下没有啊?那在linux下用的setitimer函数Windows下怎么用
赵4老师 2015-01-08
  • 打赏
  • 举报
回复
仅供参考
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
    #include <windows.h>
    #include <io.h>
    #include <process.h>
    #define  MYVOID             void
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  _vsnprintf         vsnprintf
    #define  MYVOID             void *
#endif
//Log{
#define MAXLOGSIZE 20000000
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
char logstr[16000];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
void sleep_ms(int ms) {
    Sleep(ms);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
void sleep_ms(int ms) {
    usleep(ms*1000);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;


    if (NULL==pszFmt||0==pszFmt[0]) return;
    if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
            flog=fopen(logfilename1,"a");
            if (NULL==flog) return;
        }
        fclose(flog);
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;

    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
int No_Loop=0;
MYVOID testThread(void *pcn) {
    int n,i;

    n=(int)pcn;
    i=0;
    while (1) {
        sleep_ms(1000);
        Log("in testThread %d:i==%ds\n",n,++i);
        if (i>=5) No_Loop=1;
    }
}
int main(int argc,char * argv[]) {
    int i;
#ifdef WIN32
    InitializeCriticalSection(&cs_log);
#else
    pthread_mutex_init(&cs_log,NULL);
    pthread_t threads[1];
    int threadsN;
    int rc;
#endif
    Log("=========BEGIN==================\n");
#ifdef WIN32
    _beginthread((void(__cdecl *)(void *))testThread,0,(void *)1);
#else
    threadsN=0;
    rc=pthread_create(&(threads[threadsN++]),NULL,testThread,(void *)1);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1);
#endif
    i=0;
    while (1) {
        sleep_ms(100);
        Log("in main:i==%d\n",++i);
        if (No_Loop==1) break;//
    }
    Log("=========END====================\n");
#ifdef WIN32
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}
//2012-06-14 16:27:21.500 =========BEGIN==================
//2012-06-14 16:27:21.609 in main:i==1
//2012-06-14 16:27:21.718 in main:i==2
//2012-06-14 16:27:21.828 in main:i==3
//2012-06-14 16:27:21.937 in main:i==4
//2012-06-14 16:27:22.046 in main:i==5
//2012-06-14 16:27:22.156 in main:i==6
//2012-06-14 16:27:22.265 in main:i==7
//2012-06-14 16:27:22.375 in main:i==8
//2012-06-14 16:27:22.484 in main:i==9
//2012-06-14 16:27:22.500 in testThread 1:i==1s
//2012-06-14 16:27:22.593 in main:i==10
//2012-06-14 16:27:22.703 in main:i==11
//2012-06-14 16:27:22.812 in main:i==12
//2012-06-14 16:27:22.921 in main:i==13
//2012-06-14 16:27:23.031 in main:i==14
//2012-06-14 16:27:23.140 in main:i==15
//2012-06-14 16:27:23.250 in main:i==16
//2012-06-14 16:27:23.359 in main:i==17
//2012-06-14 16:27:23.468 in main:i==18
//2012-06-14 16:27:23.500 in testThread 1:i==2s
//2012-06-14 16:27:23.578 in main:i==19
//2012-06-14 16:27:23.687 in main:i==20
//2012-06-14 16:27:23.796 in main:i==21
//2012-06-14 16:27:23.906 in main:i==22
//2012-06-14 16:27:24.015 in main:i==23
//2012-06-14 16:27:24.125 in main:i==24
//2012-06-14 16:27:24.234 in main:i==25
//2012-06-14 16:27:24.343 in main:i==26
//2012-06-14 16:27:24.453 in main:i==27
//2012-06-14 16:27:24.500 in testThread 1:i==3s
//2012-06-14 16:27:24.562 in main:i==28
//2012-06-14 16:27:24.671 in main:i==29
//2012-06-14 16:27:24.781 in main:i==30
//2012-06-14 16:27:24.890 in main:i==31
//2012-06-14 16:27:25.000 in main:i==32
//2012-06-14 16:27:25.109 in main:i==33
//2012-06-14 16:27:25.218 in main:i==34
//2012-06-14 16:27:25.328 in main:i==35
//2012-06-14 16:27:25.437 in main:i==36
//2012-06-14 16:27:25.500 in testThread 1:i==4s
//2012-06-14 16:27:25.546 in main:i==37
//2012-06-14 16:27:25.656 in main:i==38
//2012-06-14 16:27:25.765 in main:i==39
//2012-06-14 16:27:25.875 in main:i==40
//2012-06-14 16:27:25.984 in main:i==41
//2012-06-14 16:27:26.093 in main:i==42
//2012-06-14 16:27:26.203 in main:i==43
//2012-06-14 16:27:26.312 in main:i==44
//2012-06-14 16:27:26.421 in main:i==45
//2012-06-14 16:27:26.500 in testThread 1:i==5s
//2012-06-14 16:27:26.531 in main:i==46
//2012-06-14 16:27:26.531 =========END====================
赵4老师 2015-01-08
  • 打赏
  • 举报
回复
MinGW不是号称在Windows下完全兼容Linux吗? 在Linux下怎么做就在MinGW下怎么做。

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧