I have a vb6 COM dll with a class LoginClass and a function LoginUser in it. I need to dynamically call this vb6 COM dll from C#. I am trying below C# code to access it dynamically but the GetProcAddress is returning 0 even after a pointer returned by LoadLibrary.
static class NativeMethods
{
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32")]
public static extern bool FreeLibrary(IntPtr hModule);
}
class COMCommands
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate string Login(string userName, string Password, bool Result);
public string CallLoginCommand(string UserName, string Password, ref bool Result)
{
IntPtr pDll = NativeMethods.LoadLibrary(@"D:\MyCOMdll.dll");
IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "LoginUser");
Login CallLogin = (Login)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(Login));
string theResult = CallLogin(UserName, Password, Result);
bool result = NativeMethods.FreeLibrary(pDll);
return theResult;
}
}