调用Application Manager实现安装CAB文件到移动设备上!

看.Net Compact Framework里有关配置的文章时,看到可以调用Application Manager来实现安装CAB文件.Application Manager是ActiveSync里带的一个软件,它可以使用INI文件来作为配置文件而将CAB安装到设备上.书上的例子是用VC++实现的,本人不会C++,只会用VB6或者C#,考虑到C#还需要安装框架到客户的机器上,故使用VB6实现之,做了一些小的改进.

1.Form中的代码:

 

None.gif Option   Explicit
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Private   Sub cmdBegin_Click() Sub cmdBegin_Click()
InBlock.gif    
Dim rtn As Long
InBlock.gif    
Dim key As Long
InBlock.gif    rtn 
= RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\App Paths\CEAppMgr.exe", _
InBlock.gif        
0, KEY_READ, key)
InBlock.gif    
If rtn <> mdlGlobal.ERROR_SUCCESS Then
InBlock.gif        
MsgBox "请先安装ActiveSync同步软件!", vbOKOnly + vbInformation, "提示"
InBlock.gif        
Exit Sub
InBlock.gif    
End If
InBlock.gif    
InBlock.gif    
Dim length As Long
InBlock.gif    
Dim strAppMgr As String
InBlock.gif    
InBlock.gif    strAppMgr 
= Space$(MAX_PATH)
InBlock.gif    length 
= Len(strAppMgr)
InBlock.gif    
InBlock.gif    rtn 
= RegQueryValueEx(key, vbNullString, 0, REG_SZ, ByVal strAppMgr, length)
InBlock.gif    
If rtn <> mdlGlobal.ERROR_SUCCESS Then
InBlock.gif         
MsgBox "没有找到设备安装管理器,请尝试先安装ActiveSync同步软件!", vbOKOnly + vbInformation, "提示"
InBlock.gif         
Exit Sub
InBlock.gif    
End If
InBlock.gif    
InBlock.gif    strAppMgr 
= Left$(strAppMgr, length - 1)
InBlock.gif
InBlock.gif    
Dim files As String
InBlock.gif
InBlock.gif    files 
= Dir(App.Path & "\Setup-*.ini", vbNormal)
InBlock.gif    
If files = "" Then
InBlock.gif        
MsgBox "没有找到任何的安装配置文件!", vbOKOnly + vbInformation, "提示"
InBlock.gif        
Exit Sub
InBlock.gif    
End If
InBlock.gif
'*************************************************使用ShellExecuteEX*************************************
InBlock.gif'
    Dim stru As mdlGlobal.SHELLEXECUTEINFO
InBlock.gif'
InBlock.gif'
InBlock.gif'
    stru.cbSize = Len(stru)
InBlock.gif'
    stru.fMask = 0
InBlock.gif'
    stru.hwnd = Me.hwnd
InBlock.gif'
    stru.lpVerb = vbNullString
InBlock.gif'
InBlock.gif'
    stru.lpFile = strAppMgr
InBlock.gif'
    stru.lpDirectory = vbNullString
InBlock.gif'
    stru.nShow = SW_SHOWDEFAULT
InBlock.gif'
    stru.hInstApp = 0
InBlock.gif'
InBlock.gif'
    Do While files <> ""
InBlock.gif'
        Debug.Print files
InBlock.gif'
        '开始调用
InBlock.gif'
        Dim tmp As String
InBlock.gif'
        tmp = App.Path & "\" & files
InBlock.gif'
        stru.lpParameters = tmp
InBlock.gif'
        If ShellExecuteEx(stru) = ERROR_SUCCESS Then
InBlock.gif'
            MsgBox "不能够调用应用程序管理器来安装程序!", vbOKOnly + vbCritical, "提示"
InBlock.gif'
        End If
InBlock.gif'
        files = Dir
InBlock.gif'
    Loop
InBlock.gif'
**********************************************使用Shell**************************************************
InBlock.gif'
    Do While files <> ""
InBlock.gif'
        Shell strAppMgr & " " & App.Path & "\" & files, vbNormalFocus
InBlock.gif'
        files = Dir
InBlock.gif'
    Loop
InBlock.gif'
**********************************************使用线程同步************************************************
InBlock.gif
    Dim proc As PROCESS_INFORMATION
InBlock.gif    
Dim start As STARTUPINFO
InBlock.gif    
Dim ExitCode As Long
InBlock.gif    
Dim hProcess As Long
InBlock.gif    
Dim isDone As Long
InBlock.gif    
Dim ret As Long
InBlock.gif    
Dim s As String
InBlock.gif    
Do While files <> ""
InBlock.gif        s 
= strAppMgr & " " & App.Path & "\" & files
InBlock.gif        ret
& = CreateProcessA(vbNullString, s, 0&0&1&, _
InBlock.gif            NORMAL_PRIORITY_CLASS, 
0&, vbNullString, start, proc)
InBlock.gif            ret
& = WaitForSingleObject(proc.hProcess, INFINITE)
InBlock.gif            
Call GetExitCodeProcess(proc.hProcess, ExitCode)
InBlock.gif            
Call CloseHandle(proc.hThread)
InBlock.gif            
Call CloseHandle(proc.hProcess)
InBlock.gif        
If ExitCode = 0 Then
InBlock.gif            
MsgBox "不能够调用应用程序管理器来安装程序!", vbOKOnly + vbCritical, "提示"
InBlock.gif        
End If
InBlock.gif        files 
= Dir
InBlock.gif    
Loop
ExpandedBlockEnd.gif
End Sub

None.gif

 

2.模块中的代码:

 

None.gif Option   Explicit
None.gif
None.gif
Public   Const  HKEY_LOCAL_MACHINE  =   & H80000002
None.gif
None.gif
Public   Const  ERROR_SUCCESS  =   0 &
None.gif
None.gif
Public   Const  READ_CONTROL  =   & H20000
None.gif
Public   Const  STANDARD_RIGHTS_READ  =  (READ_CONTROL)
None.gif
Public   Const  KEY_QUERY_VALUE  =   & H1
None.gif
Public   Const  KEY_ENUMERATE_SUB_KEYS  =   & H8
None.gif
Public   Const  KEY_NOTIFY  =   & H10
None.gif
Public   Const  SYNCHRONIZE  =   & H100000
None.gif
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
ExpandedBlockStart.gifContractedBlock.gif
Public   Declare   Function RegOpenKeyEx() Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
InBlock.gif                        (
ByVal hKey As LongByVal lpSubKey As StringByVal ulOptions As LongByVal samDesired As Long, phkResult As LongAs Long
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public Declare Function RegQueryValueEx()Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
InBlock.gif                        (
ByVal hKey As LongByVal lpValueName As StringByVal lpReserved As Long, lpType As Long, _
InBlock.gif                        lpData 
As Any, lpcbData As LongAs Long
InBlock.gif
InBlock.gif
Public Const REG_SZ = 1
InBlock.gif
Public Const MAX_PATH = 260
InBlock.gif
InBlock.gif
Public Type SHELLEXECUTEINFO
InBlock.gif        cbSize 
As Long
InBlock.gif        fMask 
As Long
InBlock.gif        hwnd 
As Long
InBlock.gif        lpVerb 
As String
InBlock.gif        lpFile 
As String
InBlock.gif        lpParameters 
As String
InBlock.gif        lpDirectory 
As String
InBlock.gif        nShow 
As Long
InBlock.gif        hInstApp 
As Long
InBlock.gif        
'  Optional fields
InBlock.gif
        lpIDList As Long
InBlock.gif        lpClass 
As String
InBlock.gif        hkeyClass 
As Long
InBlock.gif        dwHotKey 
As Long
InBlock.gif        hIcon 
As Long
InBlock.gif        hProcess 
As Long
InBlock.gif
End Type
InBlock.gif
InBlock.gif
InBlock.gif
Public Const SW_SHOWDEFAULT = 10
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public Declare Function ShellExecuteEx()Function ShellExecuteEx Lib "shell32.dll" (lpExecInfo As SHELLEXECUTEINFO) As Long
InBlock.gif
InBlock.gif
Public Const SEE_MASK_INVOKEIDLIST = &HC
InBlock.gif
Public Const SEE_MASK_NOCLOSEPROCESS = &H40
InBlock.gif
Public Const SEE_MASK_FLAG_NO_UI = &H400
InBlock.gif
InBlock.gif
'*************************调用进程****************************************
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public Declare Function OpenProcess()Function OpenProcess Lib "kernel32" _
InBlock.gif                        (
ByVal dwDesiredAccess As LongByVal bInheritHandle As Long, _
InBlock.gif                        
ByVal dwProcessID As LongAs Long
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public Declare Function WaitForSingleObject()Function WaitForSingleObject Lib "kernel32" _
InBlock.gif                        (
ByVal hHandle As LongByVal dwMilliseconds As LongAs Long
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public Declare Function CloseHandle()Function CloseHandle Lib "kernel32" _
InBlock.gif                        (
ByVal hObject As LongAs Long
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public Declare Function GetExitCodeProcess()Function GetExitCodeProcess Lib "kernel32" _
InBlock.gif                        (
ByVal hProcess As Long, lpExitCode As LongAs Long
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public Declare Function TerminateProcess()Function TerminateProcess Lib "kernel32" _
InBlock.gif                        (
ByVal hProcess As LongByVal uExitCode As LongAs Long
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public Declare Function GetForegroundWindow()Function GetForegroundWindow Lib "user32" () As Long
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public Declare Function IsWindow()Function IsWindow Lib "user32" _
InBlock.gif                        (
ByVal hwnd As LongAs Long
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public Declare Function ShellExecute()Function ShellExecute Lib "shell32.dll" Alias _
InBlock.gif                        
"ShellExecuteA" (ByVal hwnd As LongByVal lpOperation As String, _
InBlock.gif                        
ByVal lpFile As StringByVal lpParameters As String, _
InBlock.gif                        
ByVal lpDirectory As StringByVal nShowCmd As LongAs Long
InBlock.gif                        
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public Declare Function CreateProcessA()Function CreateProcessA Lib "kernel32" (ByVal _
InBlock.gif                        lpApplicationName 
As StringByVal lpCommandLine As StringByVal _
InBlock.gif                        lpProcessAttributes 
As LongByVal lpThreadAttributes As Long, _
InBlock.gif                        
ByVal bInheritHandles As LongByVal dwCreationFlags As Long, _
InBlock.gif                        
ByVal lpEnvironment As LongByVal lpCurrentDirectory As String, _
InBlock.gif                        lpStartupInfo 
As STARTUPINFO, lpProcessInformation As _
InBlock.gif                        PROCESS_INFORMATION) 
As Long
InBlock.gif
InBlock.gif
Public Const PROCESS_QUERY_INFORMATION = &H400
InBlock.gif
Public Const STILL_ALIVE = &H103
InBlock.gif
Public Const INFINITE = &HFFFF
InBlock.gif
Public Const USEREXIT = &HC000013A
InBlock.gif
Public Const SW_SHOWNORMAL = 1
InBlock.gif
Public Const NORMAL_PRIORITY_CLASS = &H20&
InBlock.gif
InBlock.gif
Public Type STARTUPINFO
InBlock.gif    cb 
As Long
InBlock.gif    lpReserved 
As String
InBlock.gif    lpDesktop 
As String
InBlock.gif    lpTitle 
As String
InBlock.gif    dwX 
As Long
InBlock.gif    dwY 
As Long
InBlock.gif    dwXSize 
As Long
InBlock.gif    dwYSize 
As Long
InBlock.gif    dwXCountChars 
As Long
InBlock.gif    dwYCountChars 
As Long
InBlock.gif    dwFillAttribute 
As Long
InBlock.gif    dwFlags 
As Long
InBlock.gif    wShowWindow 
As Integer
InBlock.gif    cbReserved2 
As Integer
InBlock.gif    lpReserved2 
As Long
InBlock.gif    hStdInput 
As Long
InBlock.gif    hStdOutput 
As Long
InBlock.gif    hStdError 
As Long
InBlock.gif
End Type
InBlock.gif
InBlock.gif
Public Type PROCESS_INFORMATION
InBlock.gif    hProcess 
As Long
InBlock.gif    hThread 
As Long
InBlock.gif    dwProcessID 
As Long
InBlock.gif    dwThreadID 
As Long
InBlock.gif
End Type
InBlock.gif

 

None.gif Option   Explicit
None.gif
None.gif
Public   Const  HKEY_LOCAL_MACHINE  =   & H80000002
None.gif
None.gif
Public   Const  ERROR_SUCCESS  =   0 &
None.gif
None.gif
Public   Const  READ_CONTROL  =   & H20000
None.gif
Public   Const  STANDARD_RIGHTS_READ  =  (READ_CONTROL)
None.gif
Public   Const  KEY_QUERY_VALUE  =   & H1
None.gif
Public   Const  KEY_ENUMERATE_SUB_KEYS  =   & H8
None.gif
Public   Const  KEY_NOTIFY  =   & H10
None.gif
Public   Const  SYNCHRONIZE  =   & H100000
None.gif
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
ExpandedBlockStart.gifContractedBlock.gif
Public   Declare   Function RegOpenKeyEx() Function  RegOpenKeyEx  Lib   " advapi32.dll "   Alias   " RegOpenKeyExA "  _
InBlock.gif                        (
ByVal  hKey  As   Long ByVal  lpSubKey  As   String ByVal  ulOptions  As   Long ByVal  samDesired  As   Long , phkResult  As   Long As   Long
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public   Declare   Function RegQueryValueEx() Function  RegQueryValueEx  Lib   " advapi32.dll "   Alias   " RegQueryValueExA "  _
InBlock.gif                        (
ByVal  hKey  As   Long ByVal  lpValueName  As   String ByVal  lpReserved  As   Long , lpType  As   Long , _
InBlock.gif                        lpData 
As  Any, lpcbData  As   Long As   Long
InBlock.gif
InBlock.gif
Public   Const  REG_SZ  =   1
InBlock.gif
Public   Const  MAX_PATH  =   260
InBlock.gif
InBlock.gif
Public  Type SHELLEXECUTEINFO
InBlock.gif        cbSize 
As   Long
InBlock.gif        fMask 
As   Long
InBlock.gif        hwnd 
As   Long
InBlock.gif        lpVerb 
As   String
InBlock.gif        lpFile 
As   String
InBlock.gif        lpParameters 
As   String
InBlock.gif        lpDirectory 
As   String
InBlock.gif        nShow 
As   Long
InBlock.gif        hInstApp 
As   Long
InBlock.gif        
'   Optional fields
InBlock.gif
        lpIDList  As   Long
InBlock.gif        lpClass 
As   String
InBlock.gif        hkeyClass 
As   Long
InBlock.gif        dwHotKey 
As   Long
InBlock.gif        hIcon 
As   Long
InBlock.gif        hProcess 
As   Long
InBlock.gif
End  Type
InBlock.gif
InBlock.gif
InBlock.gif
Public   Const  SW_SHOWDEFAULT  =   10
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public   Declare   Function ShellExecuteEx() Function  ShellExecuteEx  Lib   " shell32.dll "  (lpExecInfo  As  SHELLEXECUTEINFO)  As   Long
InBlock.gif
InBlock.gif
Public   Const  SEE_MASK_INVOKEIDLIST  =   & HC
InBlock.gif
Public   Const  SEE_MASK_NOCLOSEPROCESS  =   & H40
InBlock.gif
Public   Const  SEE_MASK_FLAG_NO_UI  =   & H400
InBlock.gif
InBlock.gif
' *************************调用进程****************************************
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public   Declare   Function OpenProcess() Function  OpenProcess  Lib   " kernel32 "  _
InBlock.gif                        (
ByVal  dwDesiredAccess  As   Long ByVal  bInheritHandle  As   Long , _
InBlock.gif                        
ByVal  dwProcessID  As   Long As   Long
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public   Declare   Function WaitForSingleObject() Function  WaitForSingleObject  Lib   " kernel32 "  _
InBlock.gif                        (
ByVal  hHandle  As   Long ByVal  dwMilliseconds  As   Long As   Long
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public   Declare   Function CloseHandle() Function  CloseHandle  Lib   " kernel32 "  _
InBlock.gif                        (
ByVal  hObject  As   Long As   Long
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public   Declare   Function GetExitCodeProcess() Function  GetExitCodeProcess  Lib   " kernel32 "  _
InBlock.gif                        (
ByVal  hProcess  As   Long , lpExitCode  As   Long As   Long
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public   Declare   Function TerminateProcess() Function  TerminateProcess  Lib   " kernel32 "  _
InBlock.gif                        (
ByVal  hProcess  As   Long ByVal  uExitCode  As   Long As   Long
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public   Declare   Function GetForegroundWindow() Function  GetForegroundWindow  Lib   " user32 "  ()  As   Long
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public   Declare   Function IsWindow() Function  IsWindow  Lib   " user32 "  _
InBlock.gif                        (
ByVal  hwnd  As   Long As   Long
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public   Declare   Function ShellExecute() Function  ShellExecute  Lib   " shell32.dll "   Alias  _
InBlock.gif                        
" ShellExecuteA "  ( ByVal  hwnd  As   Long ByVal  lpOperation  As   String , _
InBlock.gif                        
ByVal  lpFile  As   String ByVal  lpParameters  As   String , _
InBlock.gif                        
ByVal  lpDirectory  As   String ByVal  nShowCmd  As   Long As   Long
InBlock.gif                        
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public   Declare   Function CreateProcessA() Function  CreateProcessA  Lib   " kernel32 "  ( ByVal  _
InBlock.gif                        lpApplicationName 
As   String ByVal  lpCommandLine  As   String ByVal  _
InBlock.gif                        lpProcessAttributes 
As   Long ByVal  lpThreadAttributes  As   Long , _
InBlock.gif                        
ByVal  bInheritHandles  As   Long ByVal  dwCreationFlags  As   Long , _
InBlock.gif                        
ByVal  lpEnvironment  As   Long ByVal  lpCurrentDirectory  As   String , _
InBlock.gif                        lpStartupInfo 
As  STARTUPINFO, lpProcessInformation  As  _
InBlock.gif                        PROCESS_INFORMATION) 
As   Long
InBlock.gif
InBlock.gif
Public   Const  PROCESS_QUERY_INFORMATION  =   & H400
InBlock.gif
Public   Const  STILL_ALIVE  =   & H103
InBlock.gif
Public   Const  INFINITE  =   & HFFFF
InBlock.gif
Public   Const  USEREXIT  =   & HC000013A
InBlock.gif
Public   Const  SW_SHOWNORMAL  =   1
InBlock.gif
Public   Const  NORMAL_PRIORITY_CLASS  =   & H20 &
InBlock.gif
InBlock.gif
Public  Type STARTUPINFO
InBlock.gif    cb 
As   Long
InBlock.gif    lpReserved 
As   String
InBlock.gif    lpDesktop 
As   String
InBlock.gif    lpTitle 
As   String
InBlock.gif    dwX 
As   Long
InBlock.gif    dwY 
As   Long
InBlock.gif    dwXSize 
As   Long
InBlock.gif    dwYSize 
As   Long
InBlock.gif    dwXCountChars 
As   Long
InBlock.gif    dwYCountChars 
As   Long
InBlock.gif    dwFillAttribute 
As   Long
InBlock.gif    dwFlags 
As   Long
InBlock.gif    wShowWindow 
As   Integer
InBlock.gif    cbReserved2 
As   Integer
InBlock.gif    lpReserved2 
As   Long
InBlock.gif    hStdInput 
As   Long
InBlock.gif    hStdOutput 
As   Long
InBlock.gif    hStdError 
As   Long
InBlock.gif
End  Type
InBlock.gif
InBlock.gif
Public  Type PROCESS_INFORMATION
InBlock.gif    hProcess 
As   Long
InBlock.gif    hThread 
As   Long
InBlock.gif    dwProcessID 
As   Long
InBlock.gif    dwThreadID 
As   Long
InBlock.gif
End  Type
InBlock.gif

 

最后使用的是线程同步的方式.其余两种也可以使用,但就是要安装的文件一起都跳出来了.但也不影响使用.其中关于SHELLEXECUTEINFO这个结构的声明居然费了我半天时间,因为FoxAPI里拷出来的结构的字段顺序是错误的,我们知道C里面的结构是按字节顺序排列的,顺序错了当然就要报错了.改成API Viewer拷出来的声明代码就OK了.看来,还是Microsoft的东西严谨些.呵呵.

转载于:https://www.cnblogs.com/fxwdl/archive/2006/09/29/517977.html

你可能感兴趣的:(shell,c#)