简体   繁体   中英

Configuring an MSI file to launch and kill a program upon installation

I have an application I've developed in Visual Studio 2010 and need to configure the installer (as a .msi file) to launch the program and then kill the task. I have a custom action in the msi to launch the program, but I'm having trouble adding a step to kill the task as well. Any suggestions would be great. Thank you!

You will need to create a custom action in order to handle this. Windows Installer doesn't have a kill process event. Below is a native C++ app that I created to handle this.

// KillProc.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
#include <algorithm>
#include <string> 

BOOL KillProcess(_TCHAR* szProcessToKill);
TCHAR* AcTcharlower(TCHAR *pString);

int _tmain(int argc, _TCHAR* argv[])
{

    if(argc > 1){
        KillProcess(argv[1]);
    }else{
        // No Process found
    }
    return 0;
}

BOOL KillProcess(_TCHAR* szProcessToKill){
    HANDLE hProcessSnap;
    HANDLE hProcess;
    PROCESSENTRY32 processEntry32;
    LPWSTR lpszBuffer = new TCHAR[MAX_PATH];
    LPWSTR lpszProcessToKill = new TCHAR[MAX_PATH];
    lpszProcessToKill = AcTcharlower(szProcessToKill);
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);  // Takes a snapshot of all the processes
    if(hProcessSnap == INVALID_HANDLE_VALUE){
        return( FALSE );
    }

    processEntry32.dwSize = sizeof(PROCESSENTRY32);
    if(!Process32First(hProcessSnap, &processEntry32))
    {
        CloseHandle(hProcessSnap);     
        return( FALSE );
    }

    do
    {
        lpszBuffer = processEntry32.szExeFile;
        if(!wcsicmp(lpszBuffer,szProcessToKill)){    // Is this our Process
            hProcess = OpenProcess(PROCESS_TERMINATE,0, processEntry32.th32ProcessID);  // It is so get the process handle
            TerminateProcess(hProcess,0);
            CloseHandle(hProcess); 
        } 
    }while(Process32Next(hProcessSnap,&processEntry32));  // gets next member of snapshot

    CloseHandle(hProcessSnap);  // closes the snapshot handle
    return( TRUE );
}

TCHAR* AcTcharlower(TCHAR *pString)
{
    static TCHAR pBuffer[MAX_PATH];
    TCHAR *s = pString;
    TCHAR *t = pBuffer;
    while (*s != '\0')
    {
        *t = tolower(*s);
        s++;
        t++;
    }
    return(pBuffer);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM