裴国兴
unread,Oct 4, 2009, 3:34:31 AM10/4/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to btload
#include <stdio.h>
#include <assert.h>
#include <windows.h>
static int update_start()
{
HANDLE hProcess = NULL;
STARTUPINFO startupInfo = {0};
PROCESS_INFORMATION processInfo;
char module[256];
char cmdline[8192];
startupInfo.cb = sizeof(startupInfo);
hProcess = OpenProcess(SYNCHRONIZE, TRUE, GetCurrentProcessId());
size_t count = GetModuleFileName(NULL, module, sizeof(module));
assert(count < sizeof(module));
snprintf(cmdline, sizeof(cmdline),
"update.exe %p -wait %s -delete %s -copy %s -final",
hProcess, module, module, module);
if (CreateProcess("update.exe", cmdline, NULL, NULL, TRUE,
NORMAL_PRIORITY_CLASS,
NULL, NULL, &startupInfo, &processInfo) != TRUE) {
printf("Create Process Fail: %d!\n", GetLastError());
return -1;
}
CloseHandle(processInfo.hThread);
CloseHandle(processInfo.hProcess);
CloseHandle(hProcess);
return 0;
}
static int update_wait(const char *handle)
{
HANDLE hProcess = NULL;
int count = sscanf(handle, "%p", &hProcess);
assert(count == 1);
WaitForSingleObject(hProcess, INFINITE);
CloseHandle(hProcess);
return 0;
}
static int update_final(const char *path)
{
HANDLE hProcess = NULL;
STARTUPINFO startupInfo = {0};
PROCESS_INFORMATION processInfo;
char module[256];
char cmdline[8192];
startupInfo.cb = sizeof(startupInfo);
hProcess = OpenProcess(SYNCHRONIZE, TRUE, GetCurrentProcessId());
size_t count = GetModuleFileName(NULL, module, sizeof(module));
assert(count < sizeof(module));
snprintf(cmdline, sizeof(cmdline), "update.exe %p -wait %s -delete
-1 -clear",
hProcess, module, module);
if (CreateProcess(path, cmdline, NULL, NULL, TRUE,
NORMAL_PRIORITY_CLASS,
NULL, NULL, &startupInfo, &processInfo) != TRUE) {
CloseHandle(hProcess);
return -1;
}
CloseHandle(processInfo.hThread);
CloseHandle(processInfo.hProcess);
CloseHandle(hProcess);
return 0;
}
static int update_interpret(int argc, char *argv[])
{
int i;
char module[8192];
size_t count = GetModuleFileName(NULL, module, sizeof(module));
assert (count < sizeof(module));
for (i=1; i<argc; i++) {
if (!strcmp(argv[i], "-wait")) {
if (update_wait(argv[i-1]) != 0)
printf("update wait fail: %d\n", GetLastError());
}else if (!strcmp(argv[i], "-copy")){
if (CopyFile(module, argv[i-1], TRUE) != TRUE)
printf("copy %s to %s fail\n", module, argv[i-1]);
}else if (!strcmp(argv[i], "-delete")){
if (DeleteFile(argv[i-1]) != TRUE)
printf("delete %s fail\n", argv[i-1]);
}else if (!strcmp(argv[i], "-final")){
if (update_final(argv[i-1]) != 0)
printf("update final fail: %d\n", GetLastError());
}else if (!strcmp(argv[i], "-clear")){
return atoi(argv[i-1]);
}
}
return 0;
}
int main(int argc, char *argv[])
{
//FreeConsole();
if (argc > 1 ) {
if (!update_interpret(argc, argv))
return 0;
}else{
if (!update_start())
return 0;
}
for (;;) {
Sleep(1000);
printf("Hello World!\n");
}
return 0;
}