//---------------------------------------------------------------------------- // ReportError //---------------------------------------------------------------------------- private void ReportError(Series.Cause eErrCause, Series.Exception eErrException) { UInt32 dwErrCause; UInt32 dwErrExcept; string strCode; string strMsg; string strNameCause; string strNameExcep; string strTitle; // Init dwErrCause = (UInt32)eErrCause; dwErrExcept = (UInt32)eErrException; strNameCause = eErrCause.ToString(); strNameExcep = eErrException.ToString(); // Prepare error message strCode = $"0x{dwErrCause:X2}{dwErrExcept:X2}"; strMsg = $"{strCode}\r\n{strNameCause}\r\n{strNameExcep}\r\n\r\n{mstrDetail}"; strTitle = moResources.GetString(EnumResx.ShortTitle, CProgram.CultureInfo); MessageBox.Show(strMsg, strTitle, MessageBoxButtons.OK, MessageBoxIcon.Stop); // Always close the app after an error meAction = EnumFormAction.DoCloseApp; }
//---------------------------------------------------------------------------- // TransferFiles //---------------------------------------------------------------------------- private Boolean TransferFiles(string strSection, string strFolder) { Boolean bfSuccess; string strDest; string strFilename; string strSource; WORD wFileNum; // Init bfSuccess = false; wFileNum = 0; meErrCause = Series.Cause.None; // Enumerate all file names in the specified section of the Deployment.ini while (true) { // Fetch next file listed in specified section wFileNum++; strFilename = moDeployIni.GetString(strSection, wFileNum.ToString(CProgram.CultureInfo)); // Stop if next filename was not found if (string.IsNullOrEmpty(strFilename)) { break; } // Construct source file spec strSource = $"{mstrWorking}\\{strFilename}"; // Construct destination file spec strDest = $"{strFolder}\\{strFilename}"; // Perform file overwrite copy to specified destination folder try { System.IO.File.Copy(strSource, strDest, true); } catch (UnauthorizedAccessException) { meErrException = Series.Exception.Access; } catch (ArgumentException) { meErrException = Series.Exception.Argument; } catch (PathTooLongException) { meErrException = Series.Exception.PathTooLong; } catch (DirectoryNotFoundException) { meErrException = Series.Exception.DirNotFound; } catch (FileNotFoundException) { meErrException = Series.Exception.FileNotFound; } catch (IOException) { meErrException = Series.Exception.IO; } catch (NotSupportedException) { meErrException = Series.Exception.NotSupported; } // Intercept caught exceptions if (meErrException != Series.Exception.None) { meErrCause = Series.Cause.FileCopy; mstrDetail = $"Source:\r\n{strSource}\r\n\r\nDest:\r\n{strDest}"; goto exit_method; } } // Success bfSuccess = true; exit_method: return(bfSuccess); }
//############################################################################ // Event_Timer //---------------------------------------------------------------------------- /* * Event_Timer() is called every 100ms as specified in SetUpAutomation(). * With each call, the form command code is examined and acted on if it is * set to a value other than "FormAction.DoNothing". * * This effect was designed to operate while the Bossac programmer is * operating to show that the Update utility is itself busy. */ private void Event_Timer(Object oTimer, EventArgs eventArgs) { EnumFormAction action; Series.Cause eErrCause; Series.Exception eErrException; // Check if an error was detected if (meErrCause != Series.Cause.None) { // Controlled access to the error reporting methods eErrCause = meErrCause; eErrException = meErrException; // Reset global members meErrCause = Series.Cause.None; meErrException = Series.Exception.None; ReportError(eErrCause, eErrException); } // Init action = meAction; // Ignore if no action to be tacken yet if (meAction == EnumFormAction.DoNothing) { return; } // Prevent from repeating incoming action meAction = EnumFormAction.DoNothing; // Distribute control on action switch (action) { case EnumFormAction.DoStep: switch ((EnumStep)mwStep) { case EnumStep.SelectTargetFolders: DoSelectFolders(); break; case EnumStep.DeployProgramFiles: DoDeployment(); break; case EnumStep.CloseApp: DoCloseApp(); break; } break; case EnumFormAction.DoCloseApp: DoCloseApp(); break; } }
//**************************************************************************** // Methods //**************************************************************************** //============================================================================ // CForm //---------------------------------------------------------------------------- public CForm() { // Prepare form window WindowState = FormWindowState.Normal; AutoScaleMode = AutoScaleMode.Dpi; // Global inits moResources = Strings.ResourceManager; meAction = EnumFormAction.DoStep; meErrCause = Series.Cause.None; meErrException = Series.Exception.None; mwStep = (WORD)EnumStep.SelectTargetFolders; // Start up window InitializeComponent(); // Fetch base folder names from environment mstrSystem = Environment.GetFolderPath(Environment.SpecialFolder.System); mstrCommon = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); mstrPrivate = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); mstrSoftware = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); mstrWorking = Environment.CurrentDirectory; mstrAccount = Environment.UserName; // Set up automated form timing.Start(); }
//---------------------------------------------------------------------------- // InstallDrivers //---------------------------------------------------------------------------- private Boolean InstallDrivers() { Boolean bfProcInfo; Boolean bfRedirect; Boolean bfSuccess; Boolean bfWow64; string strArgs; string strPnPUtil; string strDriverPath; string strFilename; WORD wDriverNum; IntPtr hProcess; IntPtr hRestore; Process process; ProcessStartInfo startinfo; // Init bfSuccess = false; wDriverNum = 0; meErrCause = Series.Cause.None; // Get a handle to this current process hProcess = Process.GetCurrentProcess().Handle; hRestore = IntPtr.Zero; // Check if it is running on a 64-bit processor bfProcInfo = NativeMethods.IsWow64Process(hProcess, out bfWow64); if (!bfProcInfo) { meErrCause = Series.Cause.IsWow64Process; goto exit_method; } // Disable file system redirection for this thread if (bfWow64) { bfRedirect = NativeMethods.Wow64DisableWow64FsRedirection(out hRestore); if (!bfRedirect) { meErrCause = Series.Cause.Wow64Disable; goto exit_method; } } // Enumerate all file names in the specified section of the Deployment.ini while (true) { // Fetch next file listed in specified section wDriverNum++; // Get driver's INF file name from deployment.ini [DeviceDrivers] 1='' (Can be more than one in the future) strFilename = moDeployIni.GetString(EnumSections.DeviceDrivers, wDriverNum.ToString(CProgram.CultureInfo)); // Stop if next filename was not found if (string.IsNullOrEmpty(strFilename)) { break; } // Construct fully pathed INF file spec strDriverPath = $"{mstrWorking}\\{strFilename}"; // Get location of "Windows\\System32\\InfDefaultInstall.exe" strPnPUtil = $"{mstrSystem}\\pnputil.exe"; // Construct parameters for driver installer strArgs = $"/add-driver {strDriverPath}"; // Invoke device driver installation process = new Process(); startinfo = new ProcessStartInfo(); startinfo.UseShellExecute = false; startinfo.CreateNoWindow = true; startinfo.FileName = strPnPUtil; startinfo.Arguments = strArgs; process.StartInfo = startinfo; try { process.Start(); } catch (InvalidOperationException) { meErrException = Series.Exception.InvalidOp; } catch (Win32Exception) { meErrException = Series.Exception.Win32; } // System.ComponentModel catch (PlatformNotSupportedException) { meErrException = Series.Exception.NotSupported; } // Intercept caught exceptions if (meErrException == Series.Exception.None) { process.WaitForExit(); } process.Dispose(); // Stop without success if (meErrCause != Series.Cause.None) { meErrCause |= Series.Cause.PnPUtil; goto exit_method; } } // Success bfSuccess = true; exit_method: // Restore file system redirection for this thread if (bfWow64) { NativeMethods.Wow64RevertWow64FsRedirection(hRestore); } return(bfSuccess); }