본문 바로가기

Windows/MFC

Intercept SMS Messages In Native Code

[code]
//=================================================================
// MonitorThread - Monitors event for timer notification
//
DWORD WINAPI MonitorThread (PVOID pArg) {
    TEXT_PROVIDER_SPECIFIC_DATA tpsd;
    SMS_HANDLE smshHandle = (SMS_HANDLE)pArg;
    PMYMSG_STRUCT pNextMsg;
    BYTE bBuffer[MAXMESSAGELEN];
    PBYTE pIn;
    SYSTEMTIME st;
    HANDLE hWait[2];
    HRESULT hr;
    int rc;
    DWORD dwInSize, dwSize, dwRead = 0;

    hWait[0] = g_hReadEvent;        // Need two events since it isn't
    hWait[1] = g_hQuitEvent;    // allowed for us to signal SMS event.

    while (g_fContinue) {
        rc = WaitForMultipleObjects (2, hWait, FALSE, INFINITE);
        if (!g_fContinue || (rc != WAIT_OBJECT_0))
            break;
        // Point to the next free entry in the array
        pNextMsg = &g_pMsgDB->pMsgs[g_pMsgDB->nMsgCnt];

        // Get the message size
        hr = SmsGetMessageSize (smshHandle, &dwSize);
        if (hr != ERROR_SUCCESS) continue;

        // Check for message larger than std buffer
        if (dwSize > sizeof (pNextMsg->wcMessage)) {
            if (dwSize > MAXMESSAGELEN)
                continue;
            pIn = bBuffer;
            dwInSize = MAXMESSAGELEN;
        } else {
            pIn = (PBYTE)pNextMsg->wcMessage;
            dwInSize = sizeof (pNextMsg->wcMessage);
        }
// Set up provider specific data
        tpsd.dwMessageOptions = PS_MESSAGE_OPTION_NONE;
        tpsd.psMessageClass = PS_MESSAGE_CLASS0;
        tpsd.psReplaceOption = PSRO_NONE;
        tpsd.dwHeaderDataSize = 0;
// Read the message
        hr = SmsReadMessage (smshHandle, NULL, &pNextMsg->smsAddr, &st,
                             (PBYTE)pIn, dwInSize, (PBYTE)&tpsd,
                             sizeof(TEXT_PROVIDER_SPECIFIC_DATA),
                             &dwRead);
if (hr == ERROR_SUCCESS) {
            // Convert GMT message time to local time
            FILETIME ft, ftLocal;
            SystemTimeToFileTime (&st, &ft);
            FileTimeToLocalFileTime (&ft, &ftLocal);
            FileTimeToSystemTime (&ftLocal, &pNextMsg->stMsg);

            // If using alt buffer, copy to std buff
            if ((DWORD)pIn == (DWORD)pNextMsg->wcMessage) {
                pNextMsg->nSize = (int) dwRead;
            } else {
                memset (pNextMsg->wcMessage, 0,
                        sizeof(pNextMsg->wcMessage));
                memcpy (pNextMsg->wcMessage, pIn,
                        sizeof(pNextMsg->wcMessage)-2);
                pNextMsg->nSize = sizeof(pNextMsg->wcMessage);
            }
            // Increment message count
            if (g_pMsgDB->nMsgCnt < MAX_MSGS-1) {
                if (g_hMain)
                    PostMessage (g_hMain, MYMSG_TELLNOTIFY, 1,
                                 g_pMsgDB->nMsgCnt);
                g_pMsgDB->nMsgCnt++;
            }
        } else {
            ErrorBox (g_hMain, TEXT("Error %x (%d) reading msg"),
                     hr, GetLastError());
            break;
        }
    }
    SmsClose (smshHandle);
    return 0;
}
[/code]