-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAutoUpdate.cpp
More file actions
147 lines (122 loc) · 3.43 KB
/
Copy pathAutoUpdate.cpp
File metadata and controls
147 lines (122 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// AutoUpdate.cpp : 定义应用程序的类行为。
//
#include "stdafx.h"
#include "AutoUpdate.h"
#include "AutoUpdateDlg.h"
#include ".\autoupdate.h"
#include <tlhelp32.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAutoUpdateApp
BEGIN_MESSAGE_MAP(CAutoUpdateApp, CWinApp)
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
// CAutoUpdateApp 构造
CAutoUpdateApp::CAutoUpdateApp()
{
// TODO: 在此处添加构造代码,
// 将所有重要的初始化放置在 InitInstance 中
}
// 唯一的一个 CAutoUpdateApp 对象
CAutoUpdateApp theApp;
// CAutoUpdateApp 初始化
extern CString GetModuleDirectory();
BOOL CAutoUpdateApp::InitInstance()
{
// 如果一个运行在 Windows XP 上的应用程序清单指定要
// 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
//则需要 InitCommonControls()。否则,将无法创建窗口。
InitCommonControls();
CWinApp::InitInstance();
AfxEnableControlContainer();
// 标准初始化
// 如果未使用这些功能并希望减小
// 最终可执行文件的大小,则应移除下列
// 不需要的特定初始化例程
// 更改用于存储设置的注册表项
// TODO: 应适当修改该字符串,
// 例如修改为公司或组织名
SetRegistryKey(_T("应用程序向导生成的本地应用程序"));
CString csServerURL = GetCommandLine();
{
int iIndex = csServerURL.Find(" ");
if(iIndex >= 0)
{
csServerURL = csServerURL.Mid(iIndex+1);
}
}
CAutoUpdateDlg dlg;
dlg.m_csDownloadURL = csServerURL;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: 在此放置处理何时用“确定”来关闭
//对话框的代码
}
else if (nResponse == IDCANCEL)
{
// TODO: 在此放置处理何时用“取消”来关闭
//对话框的代码
}
if(dlg.m_bFileOK) //表示文件下载下来了
{
CString csFile = GetModuleDirectory()+"YLBSetup.exe";
StartInstall(csFile);
}
// 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序,
// 而不是启动应用程序的消息泵。
return FALSE;
}
void CAutoUpdateApp::StartInstall(LPCSTR lpFileName)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
char temp = '\"';
CString strCmdLine;
strCmdLine = CString(temp) + lpFileName + CString(temp);
char* p_CmdLine;
p_CmdLine=strCmdLine.GetBuffer(strCmdLine.GetLength());
if( !CreateProcess( NULL, // No module name (use command line).
p_CmdLine, // Command line.
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.
)
{
//AfxMessageBox( "CreateProcess failed." );
return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return;
}
void CAutoUpdateApp::WaitForProcessOK(LPCSTR lpProcName)
{
HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
PROCESSENTRY32 processInfo;
processInfo.dwSize=sizeof(PROCESSENTRY32);
while(Process32Next(hSnapShot,&processInfo)!=FALSE)
{
if(stricmp(processInfo.szExeFile,lpProcName) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,processInfo.th32ProcessID);
TerminateProcess((HANDLE)(hProcess),0);
CloseHandle(hProcess);
break;
}
}
CloseHandle(hSnapShot);
}