#include #include "resource.h" #include "../Leaf/Framework.h" using namespace LeaF; #pragma comment(lib, "tapi32.lib") #ifndef _DEBUG #pragma comment(linker, "/merge:.rdata=.data") #pragma comment(linker, "/merge:.CRT=.data") #ifdef _M_IX86 #pragma comment(linker, "/merge:.text=.data") #pragma comment(linker, "/align:512") #endif #endif class MainWindow : public Dialog { public: ListBox *listBox; void LogMessage(String Msg); DECLARE_MSG_MAP(); bool OnInitDialog(); bool OnClose(); }; MainWindow *mainWindow; HLINEAPP hLineApp; HLINE hLine; AssociationList *phoneList; static void __stdcall LineCallback(DWORD hDevice, DWORD dwMsg, DWORD dwCallbackInstance, DWORD dwParam1, DWORD dwParam2, DWORD dwParam3) { static HCALL hCall; switch (dwMsg) { case LINE_APPNEWCALL: hCall = static_cast(dwParam2); return; case LINE_CALLSTATE: if (dwParam1 == LINECALLSTATE_IDLE) { LONG ret = lineDeallocateCall(hCall); assert(ret == 0); } return; case LINE_CALLINFO: if (dwParam1 == LINECALLINFOSTATE_CALLERID) { LINECALLINFO *lineCallInfo = static_cast(Object::malloc(sizeof(LINECALLINFO))); lineCallInfo->dwTotalSize = sizeof(LINECALLINFO); LONG ret = lineGetCallInfo(hCall, lineCallInfo); assert(ret == 0); if (lineCallInfo->dwNeededSize > lineCallInfo->dwTotalSize) { DWORD newSize = lineCallInfo->dwNeededSize; Object::free(lineCallInfo); lineCallInfo = static_cast(Object::malloc(newSize)); lineCallInfo->dwTotalSize = newSize; ret = lineGetCallInfo(hCall, lineCallInfo); assert(ret == 0); } assert(lineCallInfo->dwNeededSize <= lineCallInfo->dwTotalSize); mainWindow->LogMessage(reinterpret_cast(lineCallInfo) + lineCallInfo->dwCallerIDOffset); Object::free(lineCallInfo); } } } static void InitializeTAPI() { DWORD numDevs; DWORD version; version = 0x00020002; LINEINITIALIZEEXPARAMS *lineInitializeExParams = static_cast(Object::malloc(sizeof(LINEINITIALIZEEXPARAMS))); lineInitializeExParams->dwOptions = LINEINITIALIZEEXOPTION_USEHIDDENWINDOW; lineInitializeExParams->dwTotalSize = sizeof(LINEINITIALIZEEXPARAMS); LONG ret = lineInitializeEx(&hLineApp, Application::hInstance, &LineCallback, "Caller ID", &numDevs, &version, lineInitializeExParams); assert(ret == 0); if (lineInitializeExParams->dwNeededSize > lineInitializeExParams->dwTotalSize) { DWORD newSize = lineInitializeExParams->dwNeededSize; Object::free(lineInitializeExParams); lineInitializeExParams = static_cast(Object::malloc(newSize)); lineInitializeExParams->dwTotalSize = newSize; ret = lineInitializeEx(&hLineApp, Application::hInstance, &LineCallback, "Caller ID", &numDevs, &version, lineInitializeExParams); assert(ret == 0); } assert(lineInitializeExParams->dwNeededSize <= lineInitializeExParams->dwTotalSize); Object::free(lineInitializeExParams); while (numDevs > 0) { numDevs--; LINEEXTENSIONID lineExtensionID; ret = lineNegotiateAPIVersion(hLineApp, numDevs, version, version, &version, &lineExtensionID); if (ret == 0) { LINEDEVCAPS *lineDevCaps = static_cast(Object::malloc(sizeof(LINEDEVCAPS))); lineDevCaps->dwTotalSize = sizeof(LINEDEVCAPS); ret = lineGetDevCaps(hLineApp, numDevs, version, 0, lineDevCaps); assert(ret == 0); if (lineDevCaps->dwNeededSize > lineDevCaps->dwTotalSize) { DWORD newSize = lineDevCaps->dwNeededSize; Object::free(lineDevCaps); lineDevCaps = static_cast(Object::malloc(newSize)); lineDevCaps->dwTotalSize = newSize; ret = lineGetDevCaps(hLineApp, numDevs, version, 0, lineDevCaps); assert(ret == 0); } assert(lineDevCaps->dwNeededSize <= lineDevCaps->dwTotalSize); if (lineDevCaps->dwMediaModes & LINEMEDIAMODE_DATAMODEM) { String msg = "Using modem: "; msg += reinterpret_cast(lineDevCaps) + lineDevCaps->dwLineNameOffset; mainWindow->LogMessage(msg); Object::free(lineDevCaps); ret = lineOpen(hLineApp, numDevs, &hLine, version, 0, 0, LINECALLPRIVILEGE_OWNER, LINEMEDIAMODE_AUTOMATEDVOICE, NULL); assert(ret == 0); return; } Object::free(lineDevCaps); } else continue; } assert(false); } static String FormatTime() { SYSTEMTIME systemTime; GetLocalTime(&systemTime); char buf[12]; wsprintf(buf, "<%02d:%02d:%02d> ", systemTime.wHour, systemTime.wMinute, systemTime.wSecond); return buf; } void MainWindow::LogMessage(String Msg) { String tmp = FormatTime(); tmp += Msg; listBox->AppendItem(tmp); } bool MainWindow::OnInitDialog() { listBox = new ListBox(this, IDC_LIST); listBox->Dock = DockStyle::Fill; LogMessage("CallerID application started"); InitializeTAPI(); return true; } bool MainWindow::OnClose() { lineClose(hLine); lineShutdown(hLineApp); return true; } BEGIN_MSG_MAP(MainWindow) ON_WM_INITDIALOG(OnInitDialog); ON_WM_CLOSE(OnClose); END_MSG_MAP() void RunApplication() { HANDLE hFile = CreateFileA("phonebook.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile) { String entry[2]; DWORD len; int cur = 0; char ch; phoneList = new AssociationList(); while (ReadFile(hFile, &ch, 1, &len, NULL) > 0 && len > 0) switch (ch) { case '\n': if (entry[1].Length != 0) { phoneList->Add(entry[0], entry[1]); entry[0].Length = entry[1].Length = 0; } case ' ': cur = 1 - cur; case '\r': continue; default: entry[cur] += ch; } if (entry[1].Length != 0) phoneList->Add(entry[0], entry[1]); CloseHandle(hFile); } //phoneList->Trim(); mainWindow = new MainWindow(); mainWindow->Show(); Application::Run(); delete mainWindow; }