织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>杂项技术>VC(MFC)>

windows 下的自动更新

jackyhwei 发布于 2011-09-02 16:30 点击:次 
Windows下实现自动更新的实现代码。
TAG:

#include <stdio.h>
#include <assert.h>
#include <windows.h>

static int update_start(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 %s -copy %s -final",
            hProcess, module, module, module);
    if (CreateProcess(path, 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;
}

static int autorun_set(const char *regpath)
{
    HKEY hAutoRunKey = NULL;
    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath, 0,
               KEY_ALL_ACCESS, &hAutoRunKey) != ERROR_SUCCESS){
    printf("RegOpenKeyEx Fail\n");
    return -1;
    }
    char module[8192];
    size_t count = GetModuleFileName(NULL, module, sizeof(module));
    assert(count < sizeof(module));
    RegSetValueEx(hAutoRunKey, "rpcserver", 0, REG_SZ,
        (LPBYTE)module, strlen(module));
    RegCloseKey(hAutoRunKey);
    return 0;
}

int main(int argc, char *argv[])
{
    //FreeConsole();

    if (argc > 1 ) {
        if (!update_interpret(argc, argv))
            return 0;
    }else{
        if (!update_start("update.exe"))
            return 0;
    }

    autorun_set("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");

    HANDLE hMutex = CreateMutex(NULL, FALSE, "rg4.net/rpcserver");
    if (GetLastError() == ERROR_ALREADY_EXISTS){
    CloseHandle(hMutex);
    return -1;
    }

    for (;;) {
        Sleep(1000);
        printf("Hello World!\n");
    }

    CloseHandle(hMutex);
    return 0;
}

(pagx)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201109/14940.html]
本文出处:blog.chinaunix.net 作者:pagx
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
相关文章
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容