模仿MFC尋找訊息響應函式指標的 核心 程式碼
阿新 • • 發佈:2019-01-08
typedef struct
{
UINT nMessage;
UINT nCode;
UINT nID;
UINT nLastID;
UINT nSig;
int * p;
}TEST;
const TEST* AFXAPI
MyAfxFindMessageEntry(const TEST* lpEntry,
UINT nMsg, UINT nCode, UINT nID)
{
ASSERT(offsetof(TEST, nMessage) == 0);
ASSERT(offsetof(TEST, nCode) == 4);
ASSERT(offsetof(TEST, nID) == 8);
ASSERT(offsetof(TEST, nLastID) == 12);
ASSERT(offsetof(TEST, nSig) == 16);
_asm
{
MOV EBX,lpEntry
MOV EAX,nMsg
MOV EDX,nCode
MOV ECX,nID
__loop:
CMP DWORD PTR [EBX+16],0 ; nSig (0 => end)
JZ __failed
CMP EAX,DWORD PTR [EBX] ; nMessage
JE __found_message
__next:
ADD EBX,SIZE AFX_MSGMAP_ENTRY
JMP short __loop
__found_message:
CMP EDX,DWORD PTR [EBX+4] ; nCode
JNE __next
// message and code good so far
// check the ID
CMP ECX,DWORD PTR [EBX+8] ; nID
JB __next
CMP ECX,DWORD PTR [EBX+12] ; nLastID
JA __next
// found a match
MOV lpEntry,EBX ; return EBX
JMP short __end
__failed:
XOR EAX,EAX ; return NULL
MOV lpEntry,EAX
__end:
}
return lpEntry;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
const TEST t[] = { {7, 1, 2, 5, 7, NULL}, {4, 5, 6, 8,7, NULL}, {8, 9, 10 ,12, 7, NULL}, {0, 0, 0, 0, 0, NULL} };
const TEST * n = MyAfxFindMessageEntry(t, 0, 0, 0);
if (n != NULL)
{
printf("%p %d", n, n->nLastID);
}
else
{
printf("沒有找到");
}
system("pause");
return nRetCode;
}
MFC是用匯編實現的。 也有C++的實現。