teleport/client/tp_assist_win/tp_assist.cpp

232 lines
5.6 KiB
C++
Raw Blame History

#include "stdafx.h"
#include <shellapi.h>
#include <fcntl.h>
#include "resource.h"
#include "dlg_main.h"
#include "ts_http_rpc.h"
#ifdef _DEBUG
# include <vld.h>
#endif
#pragma comment(lib, "shlwapi.lib")
#define WMU_INSTANCE_EXIT (WM_USER + 2)
#define WMU_SHOW_EXIST_DLGUI (WM_USER + 3)
static ExLogger g_ex_logger;
static ATOM MyRegisterClass();
static BOOL InitInstance();
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void failed(const char* msg);
void failed(const wchar_t* msg);
static HANDLE g_SingleInstanceMutexHandle = NULL;
HINSTANCE g_hInstance = NULL;
ULONG g_ulSingleInstanceMsgId = 0;
static TCHAR szKernalName[MAX_PATH] = { 0 };
HWND g_hwndBase = NULL;
int g_argc = 0;
wchar_t** g_argv = NULL;
#define EOM_ASSIST_GUID _T("A6EFE1250C5F4416BFA819FE92CBD4B4")
#define EOM_ASSIST_INSTANCE _T("TS_ASSIST_SINGLE_INSTANCE")
#define EOM_ASSIST_WIN_CLASS _T("TS_ASSIST_WINDOW_CLASS")
#define MAKEDWORD(low, high) ((DWORD)(((WORD)(((DWORD_PTR)(low)) & 0xffff)) | ((DWORD)((WORD)(((DWORD_PTR)(high)) & 0xffff))) << 16))
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
http_rpc_main_loop();
return 0;
}
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
EXLOG_USE_LOGGER(&g_ex_logger);
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
return 0;
}
if (LOBYTE(wsaData.wVersion) != 1 ||
HIBYTE(wsaData.wVersion) != 1) {
WSACleanup();
return 0;
}
g_env.init();
#ifdef EX_DEBUG
EXLOG_LEVEL(EX_LOG_LEVEL_DEBUG);
#else
EXLOG_LEVEL(EX_LOG_LEVEL_INFO);
#endif
EXLOG_FILE(L"tp_assist.log", g_env.m_log_path.c_str(), EX_LOG_FILE_MAX_SIZE, EX_LOG_FILE_MAX_COUNT);
// g_cfgSSH.init();
// g_cfgScp.init();
// g_cfgTelnet.init();
g_cfg.init();
g_hInstance = hInstance;
_stprintf_s(szKernalName, MAX_PATH, _T("%s_%s"), EOM_ASSIST_GUID, EOM_ASSIST_INSTANCE);
g_ulSingleInstanceMsgId = RegisterWindowMessage(szKernalName);
if (0 == g_ulSingleInstanceMsgId)
return FALSE;
LPWSTR szCmdLine = (LPWSTR)::GetCommandLineW(); //<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD>
g_argv = CommandLineToArgvW(szCmdLine, &g_argc); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
for (int i = 0; i < g_argc; ++i)
{
if(0 == lstrcmp(g_argv[i], _T("--stop")))
{
PostMessage(HWND_BROADCAST, g_ulSingleInstanceMsgId, WMU_INSTANCE_EXIT, 0);
LocalFree(g_argv);
g_argv = NULL;
return -1;
}
}
// make sure run single instance.
_stprintf_s(szKernalName, MAX_PATH, _T("%s_%s"), EOM_ASSIST_GUID, EOM_ASSIST_INSTANCE);
g_SingleInstanceMutexHandle = CreateMutex(NULL, FALSE, szKernalName);
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
PostMessage(HWND_BROADCAST, g_ulSingleInstanceMsgId, WMU_SHOW_EXIST_DLGUI, 0);
CloseHandle(g_SingleInstanceMutexHandle);
LocalFree(g_argv);
g_argv = NULL;
return 0;
}
// create dialog-box window.
MyRegisterClass();
// Perform application initialization:
if (!InitInstance())
{
CloseHandle(g_SingleInstanceMutexHandle);
LocalFree(g_argv);
g_argv = NULL;
return FALSE;
}
HANDLE hThread = NULL;
DWORD dwThreadId;
hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, &dwThreadId);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
http_rpc_stop();
WaitForSingleObject(hThread, INFINITE);
CloseHandle(g_SingleInstanceMutexHandle);
LocalFree(g_argv);
g_argv = NULL;
return 0;
}
void failed(const char* msg)
{
OutputDebugStringA(msg);
}
void failed(const wchar_t* msg)
{
OutputDebugStringW(msg);
}
ATOM MyRegisterClass()
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = g_hInstance;
wcex.hIcon = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_NORMAL_BIG));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDR_ASSIST);
wcex.lpszClassName = EOM_ASSIST_WIN_CLASS;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_NORMAL_SMALL));
return RegisterClassEx(&wcex);
}
BOOL InitInstance(void)
{
g_hwndBase = CreateWindow(EOM_ASSIST_WIN_CLASS, _T(""), WS_OVERLAPPEDWINDOW, 8, 0, 8, 0, NULL, NULL, g_hInstance, NULL);
if (!g_hwndBase)
return FALSE;
ShowWindow(g_hwndBase, SW_HIDE);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (g_ulSingleInstanceMsgId == message)
{
if (WMU_INSTANCE_EXIT == wParam)
{
PostMessage(g_hDlgMain, WM_COMMAND, MAKEDWORD(IDCANCEL, 0), NULL);
return 0;
}
else if (WMU_SHOW_EXIST_DLGUI == wParam)
{
ShowWindow(g_hDlgMain, SW_SHOW);
SetWindowPos(g_hDlgMain, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
SetActiveWindow(g_hDlgMain);
BringWindowToTop(g_hDlgMain);
SetWindowPos(g_hDlgMain, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
return 0;
}
}
switch (message)
{
case WM_CREATE:
PostMessage(hWnd, WM_COMMAND, MAKEDWORD(IDM_MAIN, 0), NULL);
return DefWindowProc(hWnd, message, wParam, lParam);
break;
case WM_COMMAND:
if (IDM_MAIN == LOWORD(wParam))
{
CreateDialog(g_hInstance, MAKEINTRESOURCE(IDD_DLG_MAIN), hWnd, eomDlgMainProc);
ShowWindow(g_hDlgMain, SW_HIDE);
}
break;
case WM_DESTROY:
SendMessage(g_hDlgMain, WMU_DLG_MAIN_EXIT, NULL, NULL);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}