ActiveX DLL Late Bind

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Greetings,

I'm trying to late bind a ActiveX DLL that was made with VB6 in C#. Can
you give me an example or URL of this? I've been trying to google it, just
cant find a straight answer. Thanks!
 
Hi Chris,

Have you tried using Activator.CreateInstance()
using the type of the AxImp.exe generated Activex wrapper class ?.

Control ctrl = (Control)Activator.CreateInstance(
Type.GetType("....typeNameOfAxInteropWrapper..."));

// Add it to the form's control collection using the Controls property
Controls.Add(ctrl);
ctrl.Show();

Regards,
Aravind C
 
I have been using:

Type typeofobj = Type.GetTypeFromProgID( "AESRegLib.AESRegistryObject" );

object newobj = Activator.CreateInstance( typeofobj );

MemberInfo[] Mymemberinfoarray = typeofobj.GetMembers();

//Get and display the DeclaringType method.
Console.Write( "\nThere are {0} members in ",
Mymemberinfoarray.GetLength(0) );
Console.Write( "{0}.", typeofobj.FullName );
foreach ( MemberInfo Mymemberinfo in Mymemberinfoarray )
{
Console.Write( "\n" + Mymemberinfo.Name + " declaring type - " +
Mymemberinfo.DeclaringType );
}

But I get this in console:

There are 7 members in System.__ComObject.
CreateObjRef declaring type - System.MarshalByRefObject
InitializeLifetimeService declaring type - System.MarshalByRefObject
GetLifetimeService declaring type - System.MarshalByRefObject
GetHashCode declaring type - System.Object
Equals declaring type - System.Object
ToString declaring type - System.Object\

It doesnt seem to get the right info, not sure what I'm doing wrong.

Any ideas?

Thanks!
 
HI Chris,

Probably, I misunderstood your original question. I was assuming that
it was an ActiveX control that you were trying to host on a Windows Form.
Typically, the RCW metadata wrapper is exposed as a __ComObject
in managed code when importing a COM object.
Since the COM object is written in VB6, it would be IDispatch based.

Can you try calling one of the methods exposed by the object
using InvokeMember()
In your example, try something like to see if it invokes the methods
correctly:

object[] inputParams= { "My string input param" };

typeofobj.InvokeMember("MyMethodName",
BindingFlags.Default | BindingFlags.InvokeMethod,
null, newobj, inputParams);


Regards,
Aravind C


Chris said:
I have been using:

Type typeofobj = Type.GetTypeFromProgID( "AESRegLib.AESRegistryObject" );

object newobj = Activator.CreateInstance( typeofobj );

MemberInfo[] Mymemberinfoarray = typeofobj.GetMembers();

//Get and display the DeclaringType method.
Console.Write( "\nThere are {0} members in ",
Mymemberinfoarray.GetLength(0) );
Console.Write( "{0}.", typeofobj.FullName );
foreach ( MemberInfo Mymemberinfo in Mymemberinfoarray )
{
Console.Write( "\n" + Mymemberinfo.Name + " declaring type - " +
Mymemberinfo.DeclaringType );
}

But I get this in console:

There are 7 members in System.__ComObject.
CreateObjRef declaring type - System.MarshalByRefObject
InitializeLifetimeService declaring type - System.MarshalByRefObject
GetLifetimeService declaring type - System.MarshalByRefObject
GetHashCode declaring type - System.Object
Equals declaring type - System.Object
ToString declaring type - System.Object\

It doesnt seem to get the right info, not sure what I'm doing wrong.

Any ideas?

Thanks!

Aravind C said:
Hi Chris,

Have you tried using Activator.CreateInstance()
using the type of the AxImp.exe generated Activex wrapper class ?.

Control ctrl = (Control)Activator.CreateInstance(
Type.GetType("....typeNameOfAxInteropWrapper..."));

// Add it to the form's control collection using the Controls property
Controls.Add(ctrl);
ctrl.Show();

Regards,
Aravind C
 
It gives:

An unhandled exception of type 'System.Reflection.TargetInvocationException'
occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an
invocation.

When I add:

string Test = (string)typeofobj.InvokeMember("about",
BindingFlags.Default | BindingFlags.InvokeMethod,
null, newobj, null);


In Delpi to do what I'm wanting in C# is usually something like:

procedure TfrmMain.FormCreate(Sender: TObject);
var
AESreg:Variant; // ActiveX Object
begin

try
AESreg := CreateOleObject('AESRegLib.AESRegistryObject');
except
ShowMessage('Error cannot create object AESRegistryObject ');
Exit;
end;

ShowMessage(AESreg.About);
AESreg := unassigned;
end;


Thank for helping me Aravind, appreciate it a lot.


Aravind C said:
HI Chris,

Probably, I misunderstood your original question. I was assuming that
it was an ActiveX control that you were trying to host on a Windows Form.
Typically, the RCW metadata wrapper is exposed as a __ComObject
in managed code when importing a COM object.
Since the COM object is written in VB6, it would be IDispatch based.

Can you try calling one of the methods exposed by the object
using InvokeMember()
In your example, try something like to see if it invokes the methods
correctly:

object[] inputParams= { "My string input param" };

typeofobj.InvokeMember("MyMethodName",
BindingFlags.Default | BindingFlags.InvokeMethod,
null, newobj, inputParams);


Regards,
Aravind C


Chris said:
I have been using:

Type typeofobj = Type.GetTypeFromProgID( "AESRegLib.AESRegistryObject" );

object newobj = Activator.CreateInstance( typeofobj );

MemberInfo[] Mymemberinfoarray = typeofobj.GetMembers();

//Get and display the DeclaringType method.
Console.Write( "\nThere are {0} members in ",
Mymemberinfoarray.GetLength(0) );
Console.Write( "{0}.", typeofobj.FullName );
foreach ( MemberInfo Mymemberinfo in Mymemberinfoarray )
{
Console.Write( "\n" + Mymemberinfo.Name + " declaring type - " +
Mymemberinfo.DeclaringType );
}

But I get this in console:

There are 7 members in System.__ComObject.
CreateObjRef declaring type - System.MarshalByRefObject
InitializeLifetimeService declaring type - System.MarshalByRefObject
GetLifetimeService declaring type - System.MarshalByRefObject
GetHashCode declaring type - System.Object
Equals declaring type - System.Object
ToString declaring type - System.Object\

It doesnt seem to get the right info, not sure what I'm doing wrong.

Any ideas?

Thanks!

Aravind C said:
Hi Chris,

Have you tried using Activator.CreateInstance()
using the type of the AxImp.exe generated Activex wrapper class ?.

Control ctrl = (Control)Activator.CreateInstance(
Type.GetType("....typeNameOfAxInteropWrapper..."));

// Add it to the form's control collection using the Controls property
Controls.Add(ctrl);
ctrl.Show();

Regards,
Aravind C


Greetings,

I'm trying to late bind a ActiveX DLL that was made with VB6 in
C#.
Can
you give me an example or URL of this? I've been trying to google
it,
just
cant find a straight answer. Thanks!
 
Instead of using .InvokeMethod, I did .getmethod, and it worked!

Chris said:
It gives:

An unhandled exception of type 'System.Reflection.TargetInvocationException'
occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an
invocation.

When I add:

string Test = (string)typeofobj.InvokeMember("about",
BindingFlags.Default | BindingFlags.InvokeMethod,
null, newobj, null);


In Delpi to do what I'm wanting in C# is usually something like:

procedure TfrmMain.FormCreate(Sender: TObject);
var
AESreg:Variant; // ActiveX Object
begin

try
AESreg := CreateOleObject('AESRegLib.AESRegistryObject');
except
ShowMessage('Error cannot create object AESRegistryObject ');
Exit;
end;

ShowMessage(AESreg.About);
AESreg := unassigned;
end;


Thank for helping me Aravind, appreciate it a lot.


Aravind C said:
HI Chris,

Probably, I misunderstood your original question. I was assuming that
it was an ActiveX control that you were trying to host on a Windows Form.
Typically, the RCW metadata wrapper is exposed as a __ComObject
in managed code when importing a COM object.
Since the COM object is written in VB6, it would be IDispatch based.

Can you try calling one of the methods exposed by the object
using InvokeMember()
In your example, try something like to see if it invokes the methods
correctly:

object[] inputParams= { "My string input param" };

typeofobj.InvokeMember("MyMethodName",
BindingFlags.Default | BindingFlags.InvokeMethod,
null, newobj, inputParams);


Regards,
Aravind C


Chris said:
I have been using:

Type typeofobj = Type.GetTypeFromProgID( "AESRegLib.AESRegistryObject" );

object newobj = Activator.CreateInstance( typeofobj );

MemberInfo[] Mymemberinfoarray = typeofobj.GetMembers();

//Get and display the DeclaringType method.
Console.Write( "\nThere are {0} members in ",
Mymemberinfoarray.GetLength(0) );
Console.Write( "{0}.", typeofobj.FullName );
foreach ( MemberInfo Mymemberinfo in Mymemberinfoarray )
{
Console.Write( "\n" + Mymemberinfo.Name + " declaring type - " +
Mymemberinfo.DeclaringType );
}

But I get this in console:

There are 7 members in System.__ComObject.
CreateObjRef declaring type - System.MarshalByRefObject
InitializeLifetimeService declaring type - System.MarshalByRefObject
GetLifetimeService declaring type - System.MarshalByRefObject
GetHashCode declaring type - System.Object
Equals declaring type - System.Object
ToString declaring type - System.Object\

It doesnt seem to get the right info, not sure what I'm doing wrong.

Any ideas?

Thanks!

Hi Chris,

Have you tried using Activator.CreateInstance()
using the type of the AxImp.exe generated Activex wrapper class ?.

Control ctrl = (Control)Activator.CreateInstance(
Type.GetType("....typeNameOfAxInteropWrapper..."));

// Add it to the form's control collection using the Controls property
Controls.Add(ctrl);
ctrl.Show();

Regards,
Aravind C


Greetings,

I'm trying to late bind a ActiveX DLL that was made with VB6 in C#.
Can
you give me an example or URL of this? I've been trying to google it,
just
cant find a straight answer. Thanks!
 
Back
Top