zoukankan      html  css  js  c++  java
  • windows mobile ,wince 系统,用代码启动cab文件安装

    有时候需要用代码来启动安装cab,以下是代码。不能实现静默安装。

    启动后会提示用户是否安装,需要用户点击是才行。

    using System;
    
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.IO;
    using System.Diagnostics;
    using System.Windows.Forms;
    public class BLLInstallCab
    {
    
        #region Const
    
        private const int STILL_ACTIVE = 0x103;
    
        #endregion
    
        #region P/Invoke
    
        [DllImport("coredll.dll", EntryPoint = "CreateProcess", SetLastError = true)]
        private static extern bool CreateProcess(string pszImageName, string pszCmdLine, IntPtr psaProcess, IntPtr psaThread, int fInheritHandles, int fdwCreate, IntPtr pvEnvironment, IntPtr pszCurDir, IntPtr psiStartInfo, ProcessInfo pi);
    
        [DllImport("coredll.dll", SetLastError = true)]
        private static extern bool GetExitCodeProcess(int hProcess, ref int lpExitCode);
    
        #endregion
    
        public sealed class ProcessInfo
        {
            public IntPtr hProcess = IntPtr.Zero;
            public IntPtr hThread = IntPtr.Zero;
            public int dwProcessID = 0;
            public int dwThreadID = 0;
        }
    
        /// <summary>
        /// 安装指定目录下多Cab包
        /// </summary>
        /// <param name="SetupDir">Cab包目录路径</param>
        public void SetupFiles(string SetupDir)
        {
            if (System.IO.Directory.Exists(SetupDir) == true)
            {
                ProcessInfo pi = new ProcessInfo();
                DirectoryInfo DirInfo = new DirectoryInfo(SetupDir);
                FileInfo[] Files = DirInfo.GetFiles("*.cab");
                foreach (FileInfo file in Files)
                {
                    bool rc = CreateProcess("windows\wceload.exe", """ + file.FullName + "" /nodelete",
                        IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, pi);
    
                    int lpExitCode = STILL_ACTIVE;
    
                    int ErrorCode = 0;
    
                    while ((rc == true) && (lpExitCode == STILL_ACTIVE))
                    {
                        Application.DoEvents();
                        rc = GetExitCodeProcess(pi.hProcess.ToInt32(), ref lpExitCode);
                        if (rc == true)
                        {
                            if (lpExitCode == STILL_ACTIVE)
                                System.Threading.Thread.Sleep(1000);
                        }
                        else
                        {
                            ErrorCode = Marshal.GetLastWin32Error();
                        }
                    }
                }
            }
        }
    
        /// <summary>
        /// 检查系统安装CF版本
        /// </summary>
        /// <param name="version">版本</param>
        /// <returns></returns>
        //private bool HaveNETCF2(char version)
        //{
        //    RegistryKey NETCFKey = null;
        //    try
        //    {
        //        bool Result = true;
        //        NETCFKey = Registry.LocalMachine.OpenSubKey("Software\Microsoft\.NETCompactFramework", false);
        //        if (NETCFKey == null)
        //            return Result;
        //        string[] valueNames = NETCFKey.GetValueNames();
        //        if (valueNames == null)
        //        {
        //            NETCFKey.Close();
        //            return Result;
        //        }
        //        for (int i = 0; i < valueNames.Length; i++)
        //        {
        //            //枚举注册表Software\Microsoft\.NETCompactFrameworkCF版本值
        //            if ((valueNames[i] != null) && (valueNames[i].Length > 0) && (valueNames[i][0] == version))
        //            {
        //                Result = true;
        //                break;
        //            }
        //            else
        //            {
        //                Result = false;
        //            }
        //        }
        //        return Result;
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //    finally
        //    {
        //        if (NETCFKey != null)
        //            NETCFKey.Close();
        //    }
        //}
    }
    

      

  • 相关阅读:
    算法思想杂谈【原创】
    OpenGL坐标变换专题
    XSS的原理分析与解剖:第三章(技巧篇)【转】
    php实现字符串翻转
    (基础) --- php session原理和多台服务器session共享问题
    (基础) --- php get和post的区别
    (基础)--- PHP单引号和双引号区别
    MySQL主从复制原理解析
    详解MYSQL各种优化原理
    mysql索引详解
  • 原文地址:https://www.cnblogs.com/Jerseyblog/p/9076436.html
Copyright © 2011-2022 走看看