Tag Archives: app.config

Utilising a .NET DLL with a .config file in VB6

This isn’t great practice but it’s sometimes necessary to use a config file for a DLL developed in .NET that then needs to be called from VB6.

To debug a VB6 app/exe that uses a .NET dll with a .config file you must create a vb6.exe.config file in the same folder as the VB6 executable. Typically this is C:\Program Files\Microsoft Visual Studio\VB98.

When the VB6 dll/exe is compiled the config file must be named the same as the dll/exe with a .config extension and be placed in the same location as the dll/exe. e.g. MyVB6Program.exe.config

The .NET DLL can use code like this to retrieve, for instance, connection strings:

'Fetch the connection string from the app.config file
Dim settings As ConnectionStringSettings = ConfigurationManager.ConnectionStrings(name)
If settings IsNot Nothing Then
' Retrieve the partial connection string.
Dim connectString As String = settings.ConnectionString
' Create a new SqlConnectionStringBuilder based on the partial connection string retrieved from the config file.
Dim builder As DbConnectionStringBuilder
'We need to act differently depending on wether we are using Access or SQL
If settings.ProviderName.ToUpper = "SYSTEM.DATA.ODBC" Then
builder = New OdbcConnectionStringBuilder(connectString)
' Supply the additional values.
builder.Add("Uid", "USER_NAME_HERE")
builder.Add("Pwd", "PASSWORD_HERE")
'This is a bit of a hack but we can check the odbc connection string to see if it contains the phrase 'SQL Server'
'as all sql server odbc connection strings up to and including version 11 will contain this phrase
If settings.ConnectionString.IndexOf("SQL Server", 0, StringComparison.CurrentCultureIgnoreCase) <> -1 Then
useSQLServer = True 'Set the useSQLServer flag
Throw New Exception("Provider '" & settings.ProviderName & "' is not allowed.")
End If
Return builder.ConnectionString
Throw New Exception("Unable to retrieve app.config connectionstrings.")
End If