Referencing unmanaged ActiveX control methods in C#

M

Michiel

Hello,

I have an ActiveX control which I can call fine from HTML pages, using the
object tag like <OBJECT ID="UserControl1" WIDTH="0" HEIGHT="0"
CLASSID="CLSID:85A5119D-0000-0000-0000-5467CB71943E" CODEBASE =
http://mywebsite/MyActiveX.ocx#Version=1,1,0,0 VIEWASTEXT></OBJECT>

, and then simply adding them to the object's id with a . like

UserControl1.MyFunction()

in JavaScript.


But how do I do this from C# ?

I can't reference to the file directly (Visual Studio says "not a valid COM
component") so I figure I have to use something like

[ComImport, Guid("85A5119D-0000-0000-0000-5467CB71943E"), TypeLibType(4160)]
public class MyActiveX
{
[PreserveSig, MethodImpl(MethodImplOptions.InternalCall,
MethodCodeType=MethodCodeType.Runtime)]
public virtual extern int MyFunction();
}
And then calling it like

MyActiveX oAX=new MyActiveX();
oAX.MyFunction();

However, this gives me an ExecutionEngineException at the last line.



Thanks for any clues !

Michiel.
 
M

Michiel

Hi Ollie, thanks for your reply !

The ActiveX control doesn't have a .tlb file, is it possible to generate
this somehow ?

I have the ActiveX source, but I'm not an expert on it (it's written by
someone else).

If I use the ActiveX on the website, it somehow calls the GetIDsOfNames
function (member of IDispatch) which it passes the method's name via it's
second parameter (LPOLESTR *rgszNames) which is then translated to an
internal ActiveX function by the ActiveX.

So somehow I have to let it automatically call this GetIDsOfNames function
just like IE does I think ?

Michiel.

Ollie Riches said:
why not generate an interop assembly using the type library importer if
you have access to the original COM type library

http://msdn.microsoft.com/library/d...l/cpcongeneratingprimaryinteropassemblies.asp

HTH

Ollie Riches


Michiel said:
Hello,

I have an ActiveX control which I can call fine from HTML pages, using
the object tag like <OBJECT ID="UserControl1" WIDTH="0" HEIGHT="0"
CLASSID="CLSID:85A5119D-0000-0000-0000-5467CB71943E" CODEBASE =
http://mywebsite/MyActiveX.ocx#Version=1,1,0,0 VIEWASTEXT></OBJECT>

, and then simply adding them to the object's id with a . like

UserControl1.MyFunction()

in JavaScript.


But how do I do this from C# ?

I can't reference to the file directly (Visual Studio says "not a valid
COM component") so I figure I have to use something like

[ComImport, Guid("85A5119D-0000-0000-0000-5467CB71943E"),
TypeLibType(4160)]
public class MyActiveX
{
[PreserveSig, MethodImpl(MethodImplOptions.InternalCall,
MethodCodeType=MethodCodeType.Runtime)]
public virtual extern int MyFunction();
}
And then calling it like

MyActiveX oAX=new MyActiveX();
oAX.MyFunction();

However, this gives me an ExecutionEngineException at the last line.



Thanks for any clues !

Michiel.
 
O

Ollie Riches

if you have the dll\ocx for the active x control you can run tlbimp against
that, I guess that the tlb might be embedded into the dll\ocx.

Ollie

Michiel said:
Hi Ollie, thanks for your reply !

The ActiveX control doesn't have a .tlb file, is it possible to generate
this somehow ?

I have the ActiveX source, but I'm not an expert on it (it's written by
someone else).

If I use the ActiveX on the website, it somehow calls the GetIDsOfNames
function (member of IDispatch) which it passes the method's name via it's
second parameter (LPOLESTR *rgszNames) which is then translated to an
internal ActiveX function by the ActiveX.

So somehow I have to let it automatically call this GetIDsOfNames function
just like IE does I think ?

Michiel.

Ollie Riches said:
why not generate an interop assembly using the type library importer if
you have access to the original COM type library

http://msdn.microsoft.com/library/d...l/cpcongeneratingprimaryinteropassemblies.asp

HTH

Ollie Riches


Michiel said:
Hello,

I have an ActiveX control which I can call fine from HTML pages, using
the object tag like <OBJECT ID="UserControl1" WIDTH="0" HEIGHT="0"
CLASSID="CLSID:85A5119D-0000-0000-0000-5467CB71943E" CODEBASE =
http://mywebsite/MyActiveX.ocx#Version=1,1,0,0 VIEWASTEXT></OBJECT>

, and then simply adding them to the object's id with a . like

UserControl1.MyFunction()

in JavaScript.


But how do I do this from C# ?

I can't reference to the file directly (Visual Studio says "not a valid
COM component") so I figure I have to use something like

[ComImport, Guid("85A5119D-0000-0000-0000-5467CB71943E"),
TypeLibType(4160)]
public class MyActiveX
{
[PreserveSig, MethodImpl(MethodImplOptions.InternalCall,
MethodCodeType=MethodCodeType.Runtime)]
public virtual extern int MyFunction();
}
And then calling it like

MyActiveX oAX=new MyActiveX();
oAX.MyFunction();

However, this gives me an ExecutionEngineException at the last line.



Thanks for any clues !

Michiel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top