Browse network for SQLs if Test connection fails - Advanced Installer Community

We’re moving our forums to a new platform.

You can still browse and search existing topics, but you can no longer create new topics or post replies.

For questions, technical issues, or feature requests, please don’t hesitate to contact our support team at support@advancedinstaller.com.

Thank you for your understanding!

nvsoft
Posts: 19
Joined: Sat Feb 12, 2011 9:34 am

Browse network for SQLs if Test connection fails

Hello,

I'm building an installer to a .NET application using Advanced Installer Enterprise 8.0.2.
I added a dialog to the "First Time Install" in which I'm trying to achieve the following behavior:

1) Check if the host computer is running an SQL server with instance name TestSQL, has a DB named TestDB and a table named TestTable.
2) If the answer to (1) is NO, I'd like to search the local network for SQL servers and display the found servers in an editable comboBox for the user to select or enter the server.
3) Test the selected server for the same parameters as in (1) (SQL-Instance-Name + DB + Table).
4) Progress with the installation only in case a valid server was found.

This process should be performed before the files deployment (before the ProgressDlg dialog) and so, any Custom Action should be an attached one.

I read this answer (http://www.advancedinstaller.com/user-g ... ction.html) about an SQL connection however, as I understood it is not possible to suppress the error message displayed in case the connection is not valid (Not having the DB on the local-host is a perfectly valid condition and I don't want an error message displayed).

My installed project does not have unmanaged code and I do not want to add and have to maintain an unmanaged DLL just for a Custom Action that tests an SQL connection.
I could not find a straightforward way to call a function in an attached .NET DLL and get the return code in a property.
Also, the overall approach of using .NET DLLs is problematic as you'd need a separate DLL for every C.A. function you'd require. I could not find a way to specify the class-name implementing Installer so if that's correct, there can be only one function of a kind in a DLL (one "install", one "commit", etc.).

Please advise.
Thanks.
mihai.petcu
Posts: 3860
Joined: Thu Aug 05, 2010 8:01 am

Re: Browse network for SQLs if Test connection fails

Hello and welcome to the Advanced Installer Forums,
Check if the host computer is running an SQL server with instance name TestSQL, has a DB named TestDB and a table named TestTable.
Unfortunately there is no predefined functionality to detect a certain table in a SQL Database. However, you can verify if a certain database exists by setting the "SqlConnectionString" and performing the "Test ODBC Connection" custom action from this tutorial. The logic is if you can connect to a database, then it obviously exists and is available.
After discussing the matter with the development team I found out we also have the this feature on our TODO list with a high priority even, so it will be available in a soon release.
In the meanwhile you can still implement this functionality yourself from within your own custom action.
If the answer to (1) is NO, I'd like to search the local network for SQL servers and display the found servers in an editable comboBox for the user to select or enter the server.
This step can be accomplished. Please follow this user guide link to find out how.
(Not having the DB on the local-host is a perfectly valid condition and I don't want an error message displayed)
I'm afraid this error message is not displayed by Advanced Installer, but by the MSDE hence we don't have any control over it.
I could not find a straightforward way to call a function in an attached .NET DLL and get the return code in a property.
You can use an "Attached Custom Action". In the case of a DLL, the "Function Name" list will be automatically populated with the names of the functions included in the DLL. To get the functions listed you must use a certain function signature, as in the following example. You can read more about this custom action here.
To get the return code in a property you would need to use the MsiSetProperty function from within the DLL function.
The return codes of the DLL function must be those of a custom action, as it follows :
  • 0 action not executed
  • 1 success
  • 2 user canceled
  • 3 fatal error
  • 4 suspended, waiting for a reboot
Hope this helps.

Best,
Mihai
Mihai Petcu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
nvsoft
Posts: 19
Joined: Sat Feb 12, 2011 9:34 am

Re: Browse network for SQLs if Test connection fails

Hello Mihai and thank you for your quick reply.

Testing the SQL connection: Due to the error message displayed in case the test-fails, I cannot use this approach and so, must use a Custom Action. As I described in my original post, I have to use a .NET DLL. The example you referenced talks about function signatures in C++ DLLs.
How can I reference a specific function in a .NET DLL and get the return code in a property?

As I wrote in my original post:
My installed project does not have unmanaged code and I do not want to add and have to maintain an unmanaged DLL just for a Custom Action that tests an SQL connection.
Using the approach you suggested, if i try adding a .NET DLL as an "Attached Custom Action", the "Function Name" list only shows "Your Function" and the ComboBox is empty.

.NET DLLs Custom Actions in general: In case I want to run several separate custom actions (Func1(), Func2(), Func3()) from a .NET DLL. Is there a way to run an Attached Custom Action from a .NET DLL other than extending the "Installer" class?
The problem with extending the "Installer" class is that I cannot have many functions in a DLL but only one function. So I'd need several DLLs to implement several functions.
What other way can I use to call general functions in a .NET DLL?

Thanks.
mihai.petcu
Posts: 3860
Joined: Thu Aug 05, 2010 8:01 am

Re: Browse network for SQLs if Test connection fails

Hi,
How can I reference a specific function in a .NET DLL and get the return code in a property?
Unfortunately Windows Installer supports only native C++ DLLs for custom actions. Therefore, Advanced Installer cannot use a .NET DLL.

Best,
Mihai
Mihai Petcu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
nvsoft
Posts: 19
Joined: Sat Feb 12, 2011 9:34 am

Re: Browse network for SQLs if Test connection fails

I searched this forum thoroughly, trying to find a way to call a Custom Action implemented in C# (.NET Managed DLL).
I found a lot of questions asking for this ability but only 1 answer: "It's not supported by Windows Installer. Extend "Installer" or implement in C++".
So after digging some more I found this great Visual Studio Template by Robert Giesecke: http://sites.google.com/site/robertgies ... gedexports
It enables you to write your code in plain C# and still be able to use the produced DLL as an Attached Custom Action (and even have the "Function Name" Combo filled with your functions).

Note: I had to set Execution Properties to "Synchronous execution, ignore return code" (I'm sure it's fixable but I ran out of playing time. You can still set any return value you want through properties).

Here's a sample method:

Code: Select all

using System.Text;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;

namespace ManagedCustomActionLib
{
    internal static class ManagedCustomActions
    {
        [DllExport("PassProperty", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
        public static void PassProperty(int hInstall)
        {
            // Uncomment this line to get a debug session going
            // System.Diagnostics.Debugger.Launch();

            StringBuilder sb = new StringBuilder();

            int stringSize = 256;

            int ret = MsiGetProperty(hInstall, "InputString", sb, ref stringSize);

            if (0 == ret)
            {
                ret = MsiSetProperty(hInstall, "OutputString", sb.ToString());
            }
        }

        [DllImport("msi.dll", CharSet = CharSet.Unicode)]
        static extern int MsiSetProperty(int hInstall, string szName, string szValue);

        [DllImport("msi.dll", CharSet = CharSet.Unicode)]
        static extern int MsiGetProperty(int hInstall, string szName, [Out] StringBuilder szValueBuf, ref int pchValueBuf);
    }
}

Return to “Common Problems”