看.Net Compact Framework里有关配置的文章时,看到可以调用Application Manager来实现安装CAB文件.Application Manager是ActiveSync里带的一个软件,它可以使用INI文件来作为配置文件而将CAB安装到设备上.书上的例子是用VC++实现的,本人不会C++,只会用VB6或者C#,考虑到C#还需要安装框架到客户的机器上,故使用VB6实现之,做了一些小的改进.
1.Form中的代码:
Option
Explicit
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Private
Sub cmdBegin_Click()
Sub cmdBegin_Click()
Dim rtn As Long
Dim key As Long
rtn = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\App Paths\CEAppMgr.exe", _
0, KEY_READ, key)
If rtn <> mdlGlobal.ERROR_SUCCESS Then
MsgBox "请先安装ActiveSync同步软件!", vbOKOnly + vbInformation, "提示"
Exit Sub
End If
Dim length As Long
Dim strAppMgr As String
strAppMgr = Space$(MAX_PATH)
length = Len(strAppMgr)
rtn = RegQueryValueEx(key, vbNullString, 0, REG_SZ, ByVal strAppMgr, length)
If rtn <> mdlGlobal.ERROR_SUCCESS Then
MsgBox "没有找到设备安装管理器,请尝试先安装ActiveSync同步软件!", vbOKOnly + vbInformation, "提示"
Exit Sub
End If
strAppMgr = Left$(strAppMgr, length - 1)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Dim files As String
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
files = Dir(App.Path & "\Setup-*.ini", vbNormal)
If files = "" Then
MsgBox "没有找到任何的安装配置文件!", vbOKOnly + vbInformation, "提示"
Exit Sub
End If
'*************************************************使用ShellExecuteEX*************************************
' Dim stru As mdlGlobal.SHELLEXECUTEINFO
'
'
' stru.cbSize = Len(stru)
' stru.fMask = 0
' stru.hwnd = Me.hwnd
' stru.lpVerb = vbNullString
'
' stru.lpFile = strAppMgr
' stru.lpDirectory = vbNullString
' stru.nShow = SW_SHOWDEFAULT
' stru.hInstApp = 0
'
' Do While files <> ""
' Debug.Print files
' '开始调用
' Dim tmp As String
' tmp = App.Path & "\" & files
' stru.lpParameters = tmp
' If ShellExecuteEx(stru) = ERROR_SUCCESS Then
' MsgBox "不能够调用应用程序管理器来安装程序!", vbOKOnly + vbCritical, "提示"
' End If
' files = Dir
' Loop
'**********************************************使用Shell**************************************************
' Do While files <> ""
' Shell strAppMgr & " " & App.Path & "\" & files, vbNormalFocus
' files = Dir
' Loop
'**********************************************使用线程同步************************************************
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ExitCode As Long
Dim hProcess As Long
Dim isDone As Long
Dim ret As Long
Dim s As String
Do While files <> ""
s = strAppMgr & " " & App.Path & "\" & files
ret& = CreateProcessA(vbNullString, s, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc)
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
Call GetExitCodeProcess(proc.hProcess, ExitCode)
Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)
If ExitCode = 0 Then
MsgBox "不能够调用应用程序管理器来安装程序!", vbOKOnly + vbCritical, "提示"
End If
files = Dir
Loop
End Sub
2.模块中的代码:
Option
Explicit
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Public
Const
HKEY_LOCAL_MACHINE
=
&
H80000002
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Public
Const
ERROR_SUCCESS
=
0
&
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Public
Const
READ_CONTROL
=
&
H20000
Public
Const
STANDARD_RIGHTS_READ
=
(READ_CONTROL)
Public
Const
KEY_QUERY_VALUE
=
&
H1
Public
Const
KEY_ENUMERATE_SUB_KEYS
=
&
H8
Public
Const
KEY_NOTIFY
=
&
H10
Public
Const
SYNCHRONIZE
=
&
H100000
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Public
Const
KEY_READ
=
((STANDARD_RIGHTS_READ
Or
KEY_QUERY_VALUE
Or
KEY_ENUMERATE_SUB_KEYS
Or
KEY_NOTIFY)
And
(
Not
SYNCHRONIZE))
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public
Declare
Function RegOpenKeyEx()
Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function RegQueryValueEx()Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, _
lpData As Any, lpcbData As Long) As Long
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Const REG_SZ = 1
Public Const MAX_PATH = 260
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
' Optional fields
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Const SW_SHOWDEFAULT = 10
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function ShellExecuteEx()Function ShellExecuteEx Lib "shell32.dll" (lpExecInfo As SHELLEXECUTEINFO) As Long
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Const SEE_MASK_INVOKEIDLIST = &HC
Public Const SEE_MASK_NOCLOSEPROCESS = &H40
Public Const SEE_MASK_FLAG_NO_UI = &H400
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'*************************调用进程****************************************
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function OpenProcess()Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessID As Long) As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function WaitForSingleObject()Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function CloseHandle()Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function GetExitCodeProcess()Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function TerminateProcess()Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function GetForegroundWindow()Function GetForegroundWindow Lib "user32" () As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function IsWindow()Function IsWindow Lib "user32" _
(ByVal hwnd As Long) As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function ShellExecute()Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function CreateProcessA()Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As String, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const STILL_ALIVE = &H103
Public Const INFINITE = &HFFFF
Public Const USEREXIT = &HC000013A
Public Const SW_SHOWNORMAL = 1
Public Const NORMAL_PRIORITY_CLASS = &H20&
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Option
Explicit
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Public
Const
HKEY_LOCAL_MACHINE
=
&
H80000002
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Public
Const
ERROR_SUCCESS
=
0
&
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Public
Const
READ_CONTROL
=
&
H20000
Public
Const
STANDARD_RIGHTS_READ
=
(READ_CONTROL)
Public
Const
KEY_QUERY_VALUE
=
&
H1
Public
Const
KEY_ENUMERATE_SUB_KEYS
=
&
H8
Public
Const
KEY_NOTIFY
=
&
H10
Public
Const
SYNCHRONIZE
=
&
H100000
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Public
Const
KEY_READ
=
((STANDARD_RIGHTS_READ
Or
KEY_QUERY_VALUE
Or
KEY_ENUMERATE_SUB_KEYS
Or
KEY_NOTIFY)
And
(
Not
SYNCHRONIZE))
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public
Declare
Function RegOpenKeyEx()
Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function RegQueryValueEx()Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, _
lpData As Any, lpcbData As Long) As Long
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Const REG_SZ = 1
Public Const MAX_PATH = 260
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
' Optional fields
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Const SW_SHOWDEFAULT = 10
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function ShellExecuteEx()Function ShellExecuteEx Lib "shell32.dll" (lpExecInfo As SHELLEXECUTEINFO) As Long
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Const SEE_MASK_INVOKEIDLIST = &HC
Public Const SEE_MASK_NOCLOSEPROCESS = &H40
Public Const SEE_MASK_FLAG_NO_UI = &H400
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'*************************调用进程****************************************
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function OpenProcess()Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessID As Long) As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function WaitForSingleObject()Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function CloseHandle()Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function GetExitCodeProcess()Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function TerminateProcess()Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function GetForegroundWindow()Function GetForegroundWindow Lib "user32" () As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function IsWindow()Function IsWindow Lib "user32" _
(ByVal hwnd As Long) As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function ShellExecute()Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Declare Function CreateProcessA()Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As String, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const STILL_ALIVE = &H103
Public Const INFINITE = &HFFFF
Public Const USEREXIT = &HC000013A
Public Const SW_SHOWNORMAL = 1
Public Const NORMAL_PRIORITY_CLASS = &H20&
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
最后使用的是线程同步的方式.其余两种也可以使用,但就是要安装的文件一起都跳出来了.但也不影响使用.其中关于SHELLEXECUTEINFO这个结构的声明居然费了我半天时间,因为FoxAPI里拷出来的结构的字段顺序是错误的,我们知道C里面的结构是按字节顺序排列的,顺序错了当然就要报错了.改成API Viewer拷出来的声明代码就OK了.看来,还是Microsoft的东西严谨些.呵呵.