469,603 Members | 2,273 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,603 developers. It's quick & easy.

how to call custom config section from com interop

I have a huge complex app.config and one of the sections in the config is connection strings.
I have a dot net class that uses this app.config and a com interop is generated for this dot net class. I developed an unmanaged c++ app and it calls the com interop tlb. The problem I was getting was when I call from C++ the tlb I get an null reference exception at the following line in .NetClass:
Expand|Select|Wrap|Line Numbers
  1.                 MessageBox.Show(System.Configuration.ConfigurationManager.ConnectionStrings["TestConnString"].ConnectionString);
If I run in c# alone it has no problem identifying the connection string.
Does any one has any idea?.
I dont want to put the connection in app settings(which is visible for com interop) because this would involve a significant change in the application. Added to this there are numerous tags I need to read and retrieve
Thanks for any suggestions in advance



DotNet Class:
Expand|Select|Wrap|Line Numbers
  1. using System.ComponentModel;
  2. using System.Data;
  3. using System.Drawing;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Runtime.InteropServices;
  7.  
  8. namespace NetClass
  9. {
  10.     [Guid("E4F73391-D64D-401a-B0B5-7C4559043313")]
  11.     public interface IForm
  12.     {
  13.         void LaunchTestForm();
  14.     }
  15.  
  16.     [Guid("2DF7C2CE-1068-4ff0-A053-0F641070568C")]
  17.     [ClassInterface(ClassInterfaceType.None)] 
  18.     public class TestClass : IForm
  19.     {
  20.         public TestClass()
  21.         {
  22.         }
  23.         public void LaunchTestForm()
  24.         {
  25.             try
  26.             {
  27.                 MessageBox.Show(System.Configuration.ConfigurationManager.ConnectionStrings["TestConnString"].ConnectionString);
  28.                 Form1 frm = new Form1();
  29.                 frm.ShowDialog();
  30.             }
  31.             catch (Exception ex)
  32.             {
  33.                 MessageBox.Show(ex.ToString());
  34.             }
  35.  
  36.         }
  37.     }
  38. }
  39.  
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.     <connectionStrings>
  4.         <add name="TestConnString" connectionString="Data Source=someserver;Initial Catalog=somedb;User ID=someuserid;Password=somepassword"
  5.           providerName="System.Data.SqlClient" />
  6.     </connectionStrings>
  7. </configuration>
  8.  
C++ WIN32 CONSOLE Application:

Expand|Select|Wrap|Line Numbers
  1. CoInitialize(NULL);    
  2.  
  3. NetClass::IFormPtr ptr;    
  4. HRESULT hRes = ptr.CreateInstance(NetClass::CLSID_TestClass);
  5.  
  6. if (hRes == S_OK)
  7. {
  8. ptr->LaunchTestForm();
  9. }
Aug 27 '08 #1
0 843

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Søren Lund | last post: by
6 posts views Thread by Tabi | last post: by
2 posts views Thread by Laurent Bugnion | last post: by
6 posts views Thread by Jeff Hegedus | last post: by
7 posts views Thread by =?Utf-8?B?RG91Z2llIEJyb3du?= | last post: by
reply views Thread by =?Utf-8?B?UGhpbGlw?= | last post: by
reply views Thread by =?Utf-8?B?TWlrZQ==?= | last post: by
reply views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.