给Windows Installer添加Custom Action

Windows Installer的Custom Action可以是exe, dll, vbs或js文件。

通过这个Custom Action,只要有足够的权限,基本上想做什么就做什么。

用C++编写Custom Action的例子(省略建各种工程的步骤):

1.EXE

(1).C++代码,Console Application。其中可以通过MsiDoAction调用一些别的Action。目前并没有了解。

通常通过Windows Installer 的Property来进行数据共享。

 1 #include <windows.h>
 2 #include <Msiquery.h>
 3 #include <stdio.h>
 4 #pragma comment(lib, "msi.lib")
 5 
 6 int main(int argc, char *argv[])
 7 {
 8     printf("This is a custom action");
 9     getchar();
10     //MsiOpenPackage();
11     //MsiDoAction();
12     //MsiCloseHandle();
13     return ERROR_SUCCESS;
14 }

(2).在部署项目的File System View中新建一个文件夹,并将此exe添加到该文件夹中。

(3).在Custom Actions View中添加Custom Action,选择该exe。

也可以将这多个项目集中到一个解决方案中,通过添加项目的Primary Output来进行添加。

在exe文件中,是无法获取当前installer的session的,只能通过DLL或script来操作当前installer,参考[4]。

 

2.DLL

(1)新建一个空的DLL项目。添加如下代码

 1 extern "C"
 2 {
 3 #include <windows.h>
 4 #include <msi.h>
 5 #include <Msiquery.h>
 6 #pragma comment(lib, "msi.lib")
 7 
 8     __declspec(dllexport) UINT __stdcall FirstAction(MSIHANDLE hInstall)
 9     {
10         MessageBox(0, TEXT("message"), TEXT("caption"), MB_OK);
11         return ERROR_SUCCESS;
12     }
13 };

(2)将此DLL添加到CustomAction,然后在EntryPoint指定为FirstAction。

最初的代码根据[7]编写,原文手工编写了def文件,我使用了dllexport省去了这一步,但是原文没有加extern,导致找不到FirstAction。

然后发现[6]中有这样一个Note:

For DLLs created using Visual C++, entry points must be defined using the extern keyword in order to be visible to Windows Installer. For more information, see Using extern to Specify Linkage

参考:

1.Visual Studio Setup - projects and custom actions 

2.Walkthrough: Creating a Custom Action

3.Custom Actions

4.Accessing the Current Installer Session from Inside a Custom Action

5.Dynamic-Link Libraries

6.Unspecified module entry point for custom action '<name>'

7.http://www.flexerasoftware.com/webdocuments/PDF/dlls-for-ipwi.pdf

8.Using extern to Specify Linkage

posted on 2010-10-08 20:11  lbsx  阅读(1890)  评论(0编辑  收藏  举报