SetTimer 不好用

PS12345 2009-04-23 05:41:50
我在vc6.0 中建的win32工程,为什么TimerProc函数不被调用

static void CALLBACK TimerProc(HWND hWnd,UINT nMsg,UINT nTimerid,DWORD dwTime)
{
printf("heart heart\n\n");
}

int main(int argc, char* argv[])
{
SetTimer(NULL,0,1,TimerProc);
while(1)
{}
}
...全文
153 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lori227 2009-05-07
  • 打赏
  • 举报
回复
呀~~~~自己给自己回帖。。
PS123456 2009-05-07
  • 打赏
  • 举报
回复
你没窗口怎么可能调用呢。
SetTimer必须要一个窗口
fairchild811 2009-04-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 shexinwei 的回复:]
引用 1 楼 akirya 的回复:
你没窗口怎么可能调用呢。
SetTimer必须要一个窗口

hWnd
[in] Handle to the window to be associated with the timer. This window must be owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored.
[/Quote]

up。要有窗口handle
aaaa3105563 2009-04-23
  • 打赏
  • 举报
回复
猛·····
shexinwei 2009-04-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 akirya 的回复:]
你没窗口怎么可能调用呢。
SetTimer必须要一个窗口
[/Quote]
hWnd
[in] Handle to the window to be associated with the timer. This window must be owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored.
「已注销」 2009-04-23
  • 打赏
  • 举报
回复
供参考:
// newThunk.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>

//////////////////////////////////////////////////////////////////////////
//取类成员函数的地址.vc6版本.
template <class ToType, class FromType>
void GetMemberFuncAddr_VC6(ToType& addr,FromType f)
{
union
{
FromType _f;
ToType _t;
}ut;

ut._f = f;

addr = ut._t;
}

//////////////////////////////////////////////////////////////////////////

void ThunkTemplate(DWORD& addr1,DWORD& addr2,int calltype=0)
{
int flag = 0;
DWORD x1,x2;

if(flag)
{
__asm //__thiscall
{
thiscall_1: mov ecx,-1; //-1占位符,运行时将被替换为this指针.
mov eax,-2; //-2占位符,运行时将被替换为CTimer::CallBcak的地址.
jmp eax;
thiscall_2: ;
}

__asm //__stdcall
{
stdcall_1: push dword ptr [esp] ; //保存(复制)返回地址到当前栈中
mov dword ptr [esp+4], -1 ; //将this指针送入栈中,即原来的返回地址处
mov eax, -2;
jmp eax ; //跳转至目标消息处理函数(类成员函数)
stdcall_2: ;
}
}

if(calltype==0)//this_call
{
__asm
{
mov x1,offset thiscall_1; //取 Thunk代码段 的地址范围.
mov x2,offset thiscall_2 ;
}
}
else
{
__asm
{
mov x1,offset stdcall_1;
mov x2,offset stdcall_2 ;
}
}

addr1 = x1;
addr2 = x2;
}

void ReplaceCodeBuf(BYTE *code,int len, DWORD old,DWORD x)
{
int i=0;

for(i=0;i<len-4;++i)
{
if(*((DWORD *)&code[i])==old)
{
*((DWORD *)&code[i]) = x;

return ;
}
}
}

class CTimer
{
public:

CTimer();

void set(int uElapse);

void begin();
void end();

void /*__stdcall*/ CallBcak(HWND hwnd, UINT uMsg, unsigned int idEvent, DWORD dwTime);

private:

int m_uElapse;
int m_TimerID;
BYTE m_thunk[100];
};

CTimer::CTimer()
{
m_uElapse = 20;
m_TimerID = 30;

DWORD FuncAddr;
GetMemberFuncAddr_VC6(FuncAddr,&CTimer::CallBcak);

DWORD addr1,addr2;
ThunkTemplate(addr1,addr2,0);

memset(m_thunk,0,100);
memcpy(m_thunk,(void*)addr1,addr2-addr1);

ReplaceCodeBuf(m_thunk,addr2-addr1,-1,(DWORD)((void*)this));
ReplaceCodeBuf(m_thunk,addr2-addr1,-2,FuncAddr);
}

void CTimer::set(int uElapse)
{
m_uElapse = uElapse;
}

void CTimer::begin()
{
m_TimerID = SetTimer(NULL,0,m_uElapse,(TIMERPROC)&m_thunk[0]);
}

void CTimer::end()
{
KillTimer(NULL,m_TimerID);
}

void CTimer::CallBcak(HWND hwnd, UINT uMsg, unsigned int idEvent, DWORD dwTime)
{
printf("\n CTimer::CallBcak,ID=%d, Elapse=%d\n",m_TimerID,m_uElapse);
printf("hwnd=%x, nMsg=%d, idEvent=%d, dwTime=%d\n",hwnd, uMsg,idEvent,dwTime);
}


int main(int argc, char* argv[])
{
CTimer xx;
xx.set(400);
xx.begin();

MSG msg;
BOOL bRet;

while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return 0;
}

mLee79 2009-04-23
  • 打赏
  • 举报
回复
没消息循环。。
damo_xu 2009-04-23
  • 打赏
  • 举报
回复
倒!!!
rlink 2009-04-23
  • 打赏
  • 举报
回复
LZ 在回调函数的消息响应里面是不是没有响应 WM_TIMER 这个消息啊.如果没有响应此消息,哪么没有反应时正常,在消息拦截处
switch(message)
{
case WM_TIMER: 加上响应此消息就OK了,呵呵
break;
}
子晞 2009-04-23
  • 打赏
  • 举报
回复
出错在哪里?
  • 打赏
  • 举报
回复
你没窗口怎么可能调用呢。
SetTimer必须要一个窗口

65,210

社区成员

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

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