0

We are trying to use Microsoft.Deployment.WindowsInstaller dll (C#) and install the MSI package. I couldn't find much examples regarding this. The installation is successfull. In case of error I want to display the error message in specific language using lcid. so I use the below method passing the error code. The MSI used has language English.

// Sample code for installing
        try
        {
            Installer.InstallProduct(@"Sample.msi", "ALLUSERS=1 ADDLOCAL=ALL");
        }
        catch (InstallCanceledException ex)
        {
            errorList.Add(ex.ErrorCode + " " + ex.Message);

        }
        catch (InstallerException ex)
        {
            errorList.Add("Exit Code: " + ex.ErrorCode + ", " + ex.Message);
            // Translate error message to different language
            // ex.GetErrorRecord is always null,so the below method doesn't work.
            string langError = Installer.GetErrorMessage(ex.GetErrorRecord(),System.Globalization.CultureInfo.GetCultureInfo(1031));
        }

Am I using the method right? Please provide / point me to example where I can get the correct error message in specific language.

Thanks an lot in advance.

Sanketh P B
  • 394
  • 2
  • 16

2 Answers2

2

The API you are calling gets its messages from this list, not this one.

The API that will get you the message you are seeking can be accessed via the Win32Exception class (I'd make this a link, but I don't have enough points yet, although I'm sure you can find the class), but since you can't pass it an LCID, you will need to change your thread's culture, create the exception using your error code, then revert your thread's culture.

Hope this helps

B. Murri
  • 366
  • 1
  • 5
  • thank you for the clarification, I am able to retrieve the message only in default language. is there any simpler way to get error message in specific language? – Sanketh P B Sep 07 '15 at 15:05
  • This Q&A (http://stackoverflow.com/questions/197127/prevent-exception-messages-from-being-translated-into-the-users-language) says that setting the culture of the thread works "sometimes", I don't really know better than that what to do. Other threads say that doing pinvoke of the FormatMessage api is unreliable/crashes. It's possible to do, but it tends to require very close attention to the details. – B. Murri Sep 08 '15 at 10:20
1

You should show more of your code so we can see where you're getting that error from, so some of this may be what you're already doing.

If you use Installer.InstallProduct then you get an InstallerException if it fails, and that already contains a Message as well as an ErrorCode. Basically you need the result from (underneath everything) the call to MsiInstallProduct, and this is the list including your 1603:

https://msdn.microsoft.com/en-us/library/aa368542(v=vs.85).aspx

But you are using the error message function that returns errors during the actual install that includes the "file in use" 1603:

https://msdn.microsoft.com/en-us/library/aa372835(v=vs.85).aspx

You may have done all this, if so, then your question may be about how you get the error message from the InstallerException in the appropriate language. So maybe you need to call the GetErrorMessage overload that uses GetErrorRecord from the InstallerException and the culture info as parameters.

PhilDW
  • 20,260
  • 1
  • 18
  • 28
  • thank you for the clarification, I added the code, as per your suggestion, GetErrorMessage overload doesn't work because ex.GetErrorRecord is always null. Any other suggestions? – Sanketh P B Sep 07 '15 at 15:03