title: Having problem dynamically invoking unmanaged VB COM dll from c#?
body: <p>I have a problem calling unmanaged VB COM dll from c#. This is dynamic invocation using loadLibrary and GetProcAddress.</p>
  
  <p>I can successfully loaded the dll using loadLibrary , but the GetProcAddress always return 0. It wasnt log any error msg and nothing. it just returns 0.</p>
  
  <p>below the sample code</p>
  
  <h2>VB COM</h2>
  
  <pre><code>VERSION 1.0 CLASS
  BEGIN
      MultiUse = -1 
      Persistable = 0  
      DataBindingBehavior = 0  
      DataSourceBehavior  = 0 
      MTSTransactionMode  = 0  
  END
  
  Attribute VB_Name = "Sample"
  Attribute VB_GlobalNameSpace = False
  Attribute VB_Creatable = True
  Attribute VB_PredeclaredId = False
  Attribute VB_Exposed = True
  
  Option Explicit
  
  Private Attribute1 As String
  Private Sub Class_Initialize()
      Attribute1 = "test"
  End Sub
  
  Public Sub TestSub()
  
  End Sub
  
  Public Function testFunction() As String
      testFunction = "default.html"
  End Function
  
  Public Function SetData(XML As String) As String
      SetData = Date + Time
  End Function
  </code></pre>
  
  <h2>c# code</h2>
  
  <pre><code>static class UnManagedInvoker
  {
      [DllImport("kernel32.dll")]
      private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string dllToLoad);
  
      [DllImport("kernel32.dll")]
      private static extern IntPtr GetProcAddress(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string procedureName);
  
      [DllImport("kernel32.dll")]
      private static extern bool FreeLibrary(IntPtr hModule);
  
      [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
      public delegate string MethodToInvoke(string sdata);
  
      public static string InvokeUnmanagedDll(string dllPath, string methodName)
      {
          IntPtr DIedDll = LoadLibrary(dllPath);
  
          IntPtr AddressOfFunction = GetProcAddress(DIedDll, methodName);
  
          MethodToInvoke MI = (MethodToInvoke)Marshal.GetDelegateForFunctionPointer(AddressOfFunction, typeof(MethodToInvoke));
  
          string data = MI("ssdasda");
  
          FreeLibrary(DIedDll);
          return data;
  
      }
  }
  </code></pre>
  
  <p>And the calling code</p>
  
  <pre><code> string res = UnManagedInvoker.InvokeUnmanagedDll("xx.dll","SetData");
  </code></pre>
  
  <p>Can someone help me out..</p>
  
  <h2>Update:</h2>
  
  <p>I can successfully call the methods if the component is registered. using the below code</p>
  
  <pre><code>Type Med = Type.GetTypeFromCLSID(new Guid("089DD8B0-E12B-439B-B52C-007CA72C93D0"));
  object MedObj = Activator.CreateInstance(Med);
  object[] parameter = new object[1];
  parameter[0] = "asdasd";
  var ss = Med.InvokeMember("SetData", System.Reflection.BindingFlags.InvokeMethod, null, MedObj, parameter);
  </code></pre>
  
  <p>is there a way if the dll not registered.?</p>