WinMain.cpp
资源名称:p2p_vod.rar [点击查看]
上传用户:liguizhu
上传日期:2015-11-01
资源大小:2422k
文件大小:13k
源码类别:
P2P编程
开发平台:
Visual C++
- /*
- * Openmysee
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
- #include "stdafx.h"
- #include "LocalServer.h"
- #include "ChaosUI.h"
- #include "ChannelMenu.h"
- #include "MultiLanguageMgr.h"
- #include "About.h"
- volatile HINSTANCE g_hInstance = NULL;
- volatile HWND g_hMainWnd = NULL;
- const TCHAR g_sWndClassName[MAX_PATH] = _TEXT("MainWndCls");
- const TCHAR g_sWndName[MAX_PATH] = _TEXT("MainWindow");
- TCHAR g_sAppPath[MAX_PATH] = _T("");
- CChaosUI *g_pChaosUI = NULL;
- CLocalServer *g_pLocalServer = NULL;
- namespace {
- #include <Tlhelp32.h>
- void GetAndKillWMP()
- {
- HANDLE hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if(hModuleSnap == INVALID_HANDLE_VALUE)
- return ;
- // Set the size of the structure before using it.
- PROCESSENTRY32 prc32;
- prc32.dwSize = sizeof(PROCESSENTRY32);
- prc32.cntUsage = 0;
- // Retrieve information about the first module,
- // and exit if unsuccessful
- if(!Process32First(hModuleSnap, &prc32)) {
- CloseHandle(hModuleSnap); // Must clean up the snapshot object!
- return;
- }
- // Now walk the module list of the process,
- // and display information about each module
- DWORD wmpid = 0;
- do
- {
- /* TCHAR tmp[MAX_PATH];
- TCHAR* dest = _tcsrchr(prc32.szExeFile, _T('\'));
- if(dest == NULL)
- continue;
- _tcscpy(tmp, dest + 1);*/
- if(_tcsicmp(prc32.szExeFile, _T("wmplayer.exe")) == 0)
- {
- wmpid = prc32.th32ProcessID; // Find target module name
- break;
- }
- } while(Process32Next(hModuleSnap, &prc32));
- // Do not forget to clean up the snapshot object.
- CloseHandle(hModuleSnap);
- if(wmpid == 0)
- return;
- HANDLE hwmp = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE , FALSE, wmpid);
- if(hwmp == NULL)
- return;
- TerminateProcess(hwmp, 0);
- WaitForSingleObject(hwmp, 2000);
- CloseHandle(hwmp);
- }
- BOOL GetRegString(HKEY hkey, LPCTSTR regToBeGet, LPCTSTR valueName, LPTSTR pdata)
- {
- HKEY hkSub;
- DWORD dis;
- if(RegCreateKeyEx(hkey, regToBeGet, 0, _T(""), 0,
- KEY_READ, NULL, &hkSub, &dis) != ERROR_SUCCESS)
- return FALSE;
- DWORD len = 0;
- if(RegQueryValueEx(hkSub, valueName, 0, NULL, NULL, &len) != ERROR_SUCCESS)
- return FALSE;
- if(RegQueryValueEx(hkSub, valueName, 0, NULL, (BYTE*)pdata, &len) != ERROR_SUCCESS)
- return FALSE;
- RegCloseKey(hkSub);
- return TRUE;
- }
- CString m_strBrowser;
- LPCTSTR GetBrowser(void)
- {
- // Have we already got the browser?
- if (m_strBrowser.IsEmpty())
- {
- // Get the default browser from HKCRhttpshellopencommand
- BOOL ret = GetRegString(HKEY_CLASSES_ROOT, _T("http\shell\open\command"), NULL, m_strBrowser.GetBuffer(1024));
- m_strBrowser.ReleaseBuffer();
- if(!ret) //fail, try another position: HKCRhtmlfileshellopencommand
- {
- ret = GetRegString(HKEY_CLASSES_ROOT, _T("http\htmlfile\shell\open\command"), NULL, m_strBrowser.GetBuffer(1024));
- m_strBrowser.ReleaseBuffer();
- }
- // Do we have the browser?
- if(!ret) //fail again, we had to use a default string
- {
- m_strBrowser = _T("iexplore"); //use default string
- return m_strBrowser;
- }
- //remove the quote in path
- m_strBrowser.Remove('"');
- int nIndex = m_strBrowser.ReverseFind('\');
- if(nIndex > 0)
- {
- //script the total path of browser
- int nSpace = m_strBrowser.Find(' ', nIndex);
- if(nSpace > 0)
- m_strBrowser = m_strBrowser.Left(nSpace);
- }
- }
- return m_strBrowser;
- }
- void OnUpdate()
- {
- int ret = g_pLocalServer->GetChannelNum();
- if(ret != 0)
- {
- MultiLanguage LanguageDll;
- if(MessageBox(g_hMainWnd, LanguageDll.GetStringByStr(_T("更新程序将中断当前的播放过程,确定要更新吗?")), _T("Warning"), MB_YESNO | MB_ICONQUESTION) != IDYES)
- return;
- }
- TCHAR* updatepath = new TCHAR[MAX_PATH + 32];
- memset(updatepath, 0, sizeof(TCHAR)*(MAX_PATH+32));
- GetModuleFileName(NULL, updatepath, MAX_PATH);
- TCHAR* appendpos = _tcsrchr(updatepath, _T('\')) + 1;
- _tcscpy(appendpos, _T("xtvupdater.exe"));
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- ZeroMemory( &si, sizeof(si) );
- si.cb = sizeof(si);
- ZeroMemory( &pi, sizeof(pi) );
- CreateProcess( // No module name (use command line).
- updatepath,
- NULL, // 如果不需要强制升级,则使用/S参数避免显示升级界面
- NULL, // Process handle not inheritable.
- NULL, // Thread handle not inheritable.
- FALSE, // Set handle inheritance to FALSE.
- 0, // No creation flags.
- NULL, // Use parent's environment block.
- NULL, // Use parent's starting directory.
- &si, // Pointer to STARTUPINFO structure.
- &pi ) // Pointer to PROCESS_INFORMATION structure.
- ;
- delete[] updatepath;
- }
- void OnHelp()
- {
- TCHAR* pbuffer = new TCHAR[1024];
- LoadString(g_hInstance, IDS_HELPURL, pbuffer, 1024);
- ShellExecute(NULL, NULL, GetBrowser() ,pbuffer, NULL, SW_SHOWNORMAL);
- delete[] pbuffer;
- }
- void OnHomePage()
- {
- TCHAR* pbuffer = new TCHAR[1024];
- LoadString(g_hInstance, IDS_HOMEPAGE, pbuffer, 1024);
- ShellExecute(NULL, NULL, GetBrowser() ,pbuffer, NULL, SW_SHOWNORMAL);
- delete[] pbuffer;
- }
- void OnEmail()
- {
- TCHAR* pbuffer = new TCHAR[1024];
- LoadString(g_hInstance, IDS_SUPPORTEMAIL, pbuffer, 1024);
- ShellExecute(NULL, NULL, pbuffer ,NULL, NULL, SW_SHOWNORMAL);
- delete[] pbuffer;
- }
- void OnSelectNetWork()
- {
- TCHAR* pbuffer = new TCHAR[1024];
- LoadString(g_hInstance, IDS_NETWORKSELECTURL, pbuffer, 1024);
- ShellExecute(NULL, NULL, GetBrowser() ,pbuffer, NULL, SW_SHOWNORMAL);
- delete[] pbuffer;
- }
- void OnAbout()
- {
- // CBalloonHelp::LaunchBalloon("adsfadf", "dafadf", CPoint(100, 100), IDI_EXCLAMATION, CBalloonHelp::unSHOW_CLOSE_BUTTON,
- // g_hMainWnd);
- CAbout aboutdlg;
- }
- void OnExit()
- {
- int ret = g_pLocalServer->GetChannelNum();
- if(ret != 0)
- {
- MultiLanguage LanguageDll;
- MessageBox(g_hMainWnd, LanguageDll.GetStringByStr(_T("请关闭正在播放的内容后再退出。")), _T("Warning"), MB_OK | MB_ICONWARNING);
- }
- else
- {
- if(g_pChaosUI)
- g_pChaosUI->DeliverExit();
- if(g_pLocalServer)
- {
- g_pLocalServer->Stop();
- g_pLocalServer->FireExit();
- }
- }
- }
- BOOL OnOpenChannel(DWORD cmdID)
- {
- LPCTSTR url = CChannelMenu::GetInstance()->GetChannelByCmdID(cmdID);
- if(url != NULL)
- {
- //强行杀掉WMP进程等于让WMP非正常退出. 会导致下次启动WMP时禁用所有插件.所以取消掉
- // GetAndKillWMP();
- TCHAR* pplayerpath = new TCHAR[MAX_PATH];
- BOOL bOK = FALSE;
- if(GetRegString(HKEY_LOCAL_MACHINE, _T("SOFTWARE\Microsoft\Multimedia\WMPlayer"),
- _T("Player.Path"), pplayerpath))
- bOK = TRUE;
- else if(GetRegString(HKEY_LOCAL_MACHINE, _T("SOFTWARE\Microsoft\Multimedia\MPlayer2"),
- _T("Player.Path"), pplayerpath))
- bOK = TRUE;
- if(!bOK)
- {
- MultiLanguage langDll;
- MessageBox(g_hMainWnd, langDll.GetStringByStr(_T("找不到windows media player播放器")),
- langDll.GetStringByStr(_T("错误")), MB_OK);
- return TRUE;
- }
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- ZeroMemory( &si, sizeof(si) );
- si.cb = sizeof(si);
- ZeroMemory( &pi, sizeof(pi) );
- TCHAR* tmpurl = new TCHAR[1024];
- tmpurl[0] = _T(' ');
- _tcscpy(tmpurl + 1, url);
- CreateProcess( // No module name (use command line).
- pplayerpath,
- tmpurl, // cmdLIne
- NULL, // Process handle not inheritable.
- NULL, // Thread handle not inheritable.
- FALSE, // Set handle inheritance to FALSE.
- 0, // No creation flags.
- NULL, // Use parent's environment block.
- NULL, // Use parent's starting directory.
- &si, // Pointer to STARTUPINFO structure.
- &pi ) // Pointer to PROCESS_INFORMATION structure.
- ;
- delete[] tmpurl;
- delete[] pplayerpath;
- return TRUE;
- }
- return FALSE;
- }
- }
- LRESULT CALLBACK _MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch(uMsg)
- {
- case WM_SENDTOLOCALSERVER:
- if(g_pLocalServer)
- g_pLocalServer->HandleLocalSrvNotify(wParam, lParam);
- break;
- case WM_SENDTOLAYER1:
- if(g_pLocalServer)
- g_pLocalServer->UpdateLayer1Notify(wParam, lParam);
- break;
- // 收到一个ChannelMgr需要退出的消息
- case WM_CHANNELMGR_QUIT:
- if(g_pLocalServer)
- g_pLocalServer->DeleteChannel(CHANNELID(wParam));
- break;
- case WM_EXIT_EX: //清理所有内容并退出程序
- CChaosUI::DeleteInstance();
- g_pChaosUI = NULL;
- CLocalServer::DeleteInstance();
- g_pLocalServer = NULL;
- DestroyWindow(g_hMainWnd);
- CChannelMenu::ReleaseInstance();
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_TASKBARMSG:
- SetForegroundWindow(g_hMainWnd); //保证在按菜单外部时能关闭菜单
- switch(lParam)
- {
- case WM_RBUTTONUP:
- if(CChannelMenu::GetInstance())
- {
- CChannelMenu::GetInstance()->ShowRButtonTIMenu(hwnd);
- }
- break;
- case WM_LBUTTONDOWN:
- g_pChaosUI->ShowBalloon();
- break;
- default:
- break;
- }
- break;
- case WM_INITMENUPOPUP :
- if(CChannelMenu::GetInstance())
- CChannelMenu::GetInstance()->OnPopupMenu((HMENU) wParam, lParam);
- break;
- case WM_COMMAND:
- {
- int wmId, wmEvent;
- wmId = LOWORD(wParam);
- wmEvent = HIWORD(wParam);
- // Parse the menu selections:
- if(OnOpenChannel(wmId))
- break;
- switch (wmId)
- {
- case TRAYWM_LANGUAGECHANGE:
- case TRAYWM_UPDATECHANNEL:
- CChannelMenu::GetInstance()->UpdateChannelNow();
- break;
- case TRAYWM_EXIT:
- OnExit();
- break;
- case TRAYWM_ABOUT:
- OnAbout();
- break;
- case TRAYWM_HELP:
- OnHelp();
- break;
- case TRAYWM_UPDATE:
- OnUpdate();
- break;
- case TRAYWM_HOMEPAGE:
- OnHomePage();
- break;
- case TRAYWM_EMAIL:
- OnEmail();
- break;
- case TRAYWM_NETWORK:
- OnSelectNetWork();
- break;
- default:
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- }
- break;
- default:
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- return 0;
- }
- ATOM RegisterMainWndClass(HINSTANCE hInstance)
- {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;;
- wcex.lpfnWndProc = (WNDPROC)_MainWndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = ::LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYSEE_1));
- wcex.hCursor = NULL;
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = g_sWndClassName;
- wcex.hIconSm = NULL;
- return RegisterClassEx(&wcex);
- }
- int WINAPI WinMain( HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow )
- {
- // { 保证单个实例
- HANDLE hSingleInstMutex = CreateMutex(NULL, TRUE, _TEXT("SingleInst"));
- if(NULL == hSingleInstMutex)
- {
- assert(0);
- return 0;
- }
- else
- {
- if(ERROR_ALREADY_EXISTS == GetLastError())
- {
- CloseHandle(hSingleInstMutex);
- return 0;
- }
- }
- // 保证单个实例 }
- //处理命令行消息
- _strlwr(lpCmdLine);
- // BOOL useChannelMenu = strstr(lpCmdLine, "l") != NULL;
- if(strstr(lpCmdLine, "home") != NULL)
- OnHomePage(); //直接调用函数打开主页
- BOOL useChannelMenu = TRUE;
- //
- // 生成程序目录
- GetModuleFileName(NULL, g_sAppPath, MAX_PATH);
- {
- TCHAR* pos = _tcsrchr(g_sAppPath, _T('\'));
- if(pos != NULL)
- *pos = _T(' ');
- }
- //
- g_hInstance = hInstance;
- RegisterMainWndClass(hInstance);
- g_hMainWnd = CreateWindowEx(WS_EX_TOPMOST, g_sWndClassName, g_sWndName,
- WS_OVERLAPPEDWINDOW, 0, 0, 200, 200, NULL, NULL, hInstance, NULL);
- ShowWindow(g_hMainWnd, SW_HIDE);
- UpdateWindow(g_hMainWnd);
- // { 开始创建各模块
- CChannelMenu::CreateInstance(useChannelMenu);
- g_pLocalServer = CLocalServer::CreateInstance();
- assert(g_pLocalServer);
- if(g_pLocalServer)
- {
- g_pChaosUI = CChaosUI::CreateInstance(g_pLocalServer);
- if(!g_pLocalServer->Run())
- {
- assert(0);
- }
- }
- // }创建各模块结束
- MSG msg;
- while(GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- //所有清理都在WM_EXIT_EX中完成了
- UnregisterClass(g_sWndClassName, hInstance);
- CloseHandle(hSingleInstMutex);
- return 0;
- }