0

Now, the issue is, when I run this code on my created MSI file, then the last query i.e the query for the delayed start becomes failed and gives me the exception with msi errorcode 1627:

Microsoft.Deployment.WindowsInstaller.InstallerException: Function failed during execution. at Microsoft.Deployment.WindowsInstaller.View.Execute(Record executeParams) at Microsoft.Deployment.WindowsInstaller.Database.Execute(String sql, Record record) at Microsoft.Deployment.WindowsInstaller.Database.Execute(String sqlFormat, Object[] args) at MSIManager.Program.UpdateMSIService(String msi, String serviceName, String displayName, String fileName) in C:\Users\Chirag Pathak\3D Objects\bizbrain-agent-v7\Installers\MSIManager\Program.cs:line 96

Below is my code

   class Program
            {
                private static int msidbServiceConfigEventInstall = 1;
                private static int SERVICE_CONFIG_DELAYED_AUTO_START = 3;

                private static string BIN_FILE_PATH_X64 = @"C:\Users\Chirag Pathak\3D Objects\bizbrain-agent-v7\x64\Debug\";
                private static string MSI_NAME_X64 = "MyTallyApp Agent Installer (x64).msi";

                private static string PATH_PREFIX = "C:\\Users\\Chirag Pathak\\3D Objects\\bizbrain-agent-v7\\Installers\\Installers\\";

        static void Main(string[] args)
                {
                    UpdateMSIService(MSI_NAME_X64, "BizBrainAgentWindowsService-x64", "MyTallyApp Agent Service (64 bit)", "BizBrainAgentWindowsService-x64.exe");
                    UpdateMSIService(MSI_NAME_X64, "BizBrainAgentWindowsServiceLauncher-x64", "MyTallyApp Agent Service Launcher (64 bit)", "BizBrainAgentWindowsServiceLauncher-x64.exe");

                }

              static bool UpdateMSIService(string msi, string serviceName, string displayName, string fileName)
                {
                    string databasePath = PATH_PREFIX + msi;
                    if (!File.Exists(databasePath))
                    {
                        Console.WriteLine("MSI File " + msi + " does not exist!");   
                        return false;
                    }

                    using(Database database = new Database(databasePath, DatabaseOpenMode.Direct))
                    {
                        try
                        {
                            serviceName = "'" + serviceName + "'";
                            displayName = "'" + displayName + "'";

                            Microsoft.Deployment.WindowsInstaller.View view = database.OpenView("Select Component_,FileName from File");
                            view.Execute();
                            Record[] records = view.ToArray<Record>();
                            string componentId = "'" + GetComponentId(records, fileName) + "'";
                            view.Close();
                            Console.WriteLine("Service ComponentId=:" + componentId);
                            //Do this after the search
                            fileName = "'" + fileName + "'";

                            string sqlDeleteServiceInstall = "DELETE FROM `ServiceInstall` WHERE `ServiceInstall`=" + serviceName;
                            database.Execute(sqlDeleteServiceInstall);


                            string sqlInsertServiceInstall = "INSERT INTO `ServiceInstall` (`ServiceInstall`,`Name`,`DisplayName`,`ServiceType`,`StartType`,`ErrorControl`,`Component_`,`Description`) VALUES (" + serviceName + "," + serviceName + "," + displayName + ",16,2,1," + componentId + "," + displayName + ")";
                            database.Execute(sqlInsertServiceInstall);

                            string sqlDeleteServiceControl = "DELETE FROM `ServiceControl` WHERE `ServiceControl`=" + serviceName;
                            database.Execute(sqlDeleteServiceControl);

                            string sqlInsertServiceControl = "INSERT INTO `ServiceControl` (`ServiceControl`,`Name`,`Event`,`Component_`) VALUES(" + serviceName + "," + serviceName + ",1," + componentId + ")"; 
                            database.Execute(sqlInsertServiceControl);


                  ****//For Automatic [Delayed Start]
                                string sqlInsertMSIServiceConfig = "INSERT INTO `MsiServiceConfig`(`MsiServiceConfig`,`Name`,`Event`,`ConfigType`,`Argument`,`Component_`) VALUES('AutoStartDelayed'," + serviceName + "," + msidbServiceConfigEventInstall + "," + SERVICE_CONFIG_DELAYED_AUTO_START + ",1," + componentId + ")";  
                                database.Execute(sqlInsertMSIServiceConfig);//****//This is the line where exception occures.
                            return true;

                        }
                        catch (Exception e)
                        { }
                    }  
                }

{"msiErrorCode":1627,"ClassName":"Microsoft.Deployment.WindowsInstaller.InstallerException","Message":"Function failed during execution.","Data":null,"InnerException":null,"HelpURL":null,"StackTraceString":" at Microsoft.Deployment.WindowsInstaller.View.Execute(Record executeParams)\r\n at Microsoft.Deployment.WindowsInstaller.Database.Execute(String sql, Record record)\r\n at Microsoft.Deployment.WindowsInstaller.Database.Execute(String sqlFormat, Object[] args)\r\n at MSIManager.Program.UpdateMSIService(String msi, String serviceName, String displayName, String fileName) in C:\Users\Chirag Pathak\3D Objects\bizbrain-agent-v7\Installers\MSIManager\Program.cs:line 96","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":"8\nExecute\nMicrosoft.Deployment.WindowsInstaller, Version=3.0.0.0, Culture=neutral, PublicKeyToken=ce35f76fcda82bad\nMicrosoft.Deployment.WindowsInstaller.View\nVoid Execute(Microsoft.Deployment.WindowsInstaller.Record)","HResult":-2146233087,"Source":"Microsoft.Deployment.WindowsInstaller","WatsonBuckets":null}

chirag pathak
  • 121
  • 1
  • 14
  • If you use WiX to make your setup, there are built-in features to set the service to delay start without any coding. [Sample here](https://github.com/google/fleetspeak/blob/83879c011e4810a210ee23d8d8e5ef11b70e2c1e/fleetspeak/client-win/fleetspeak.wxs). Search for `ServiceConfig`. [More samples](https://github.com/search?p=1&q=ServiceInstall+ServiceControl+ServiceConfig+extension%3Awxs+DelayedAutoStart%3D%22yes%22&type=Code). [WiX quick start info](https://stackoverflow.com/a/25005864/129130). And [how to debug custom actions](https://stackoverflow.com/a/52880033/129130) (Visual Studio). – Stein Åsmul Oct 09 '19 at 11:23
  • @SteinÅsmul Even I am trying to add the entry manually to MsiServiceConfig table using Orca, the table doesn't allow me to add the second entry and gives me **"Failed to add row to the MSI Service Config table"** , however first entry always created sucessfully. – chirag pathak Oct 09 '19 at 12:41
  • [Have a look in the MSI SDK documentation again](https://learn.microsoft.com/en-us/windows/win32/msi/msiserviceconfig-table). Maybe you have an incorrect value for the component id. It is a foreign key and needs to match an existing entry in the Component Table. – Stein Åsmul Oct 09 '19 at 12:53

0 Answers0