Using COM EXE in C#

N

NvrBst

My problem is that it keeps returing nothing after calling the
methods. I "TlbImp.exe TEST.exe /out:TESTCS.dll" and then in C# Right
click > Add Reference > Browse and give it the TESTCS.dll.

It adds "TESTCS" and I can use intellasence to see all the methods. I
do

TESTCS.Flower myFlower = new TESTCS.Flower();
MessageBox.Show(myFlower.get_GetFlower(2));

The "myFlower.GetFlower(2)" COM is suppose to return the 2nd flower
(stored in registry), which is a string. When I do this it says to
call accessor method directly "myFlower.get_GetFlower(2);" and this
just returns null.



I can use the COM EXE from my ASP/JScript pages using "object myFlower
= Server.CreateObject("TEST.myFlower);" and "myFlower.GetFlower(2);".
Just doesn't want to work in a Windows Form C# App.

Any suggestions? I've tried late binding, it seems more complicated
and I don't need late binding but it doesn't work either. Late
Binding code below:

Type objClassType;
objClassType = Type.GetTypeFromProgID("TEST.myFlower");
object objApp_Late = Activator.CreateInstance(objClassType);
objApp_Late.GetType().InvokeMember("GetFlower",
BindingFlags.InvokeMethod, null, objApp_Late, new object[] {55555});

It crashes at runtime with a "TargetInvocationException: Exception has
been thrown by the target of an Invocation." on the last line.

Thanks NB
 
N

NvrBst

My problem is that it keeps returing nothing after calling the
methods. I "TlbImp.exe TEST.exe /out:TESTCS.dll" and then in C# Right
click > Add Reference > Browse and give it the TESTCS.dll.

It adds "TESTCS" and I can use intellasence to see all the methods. I
do

TESTCS.Flower myFlower = new TESTCS.Flower();
MessageBox.Show(myFlower.get_GetFlower(2));

The "myFlower.GetFlower(2)" COM is suppose to return the 2nd flower
(stored in registry), which is a string. When I do this it says to
call accessor method directly "myFlower.get_GetFlower(2);" and this
just returns null.

I can use the COM EXE from my ASP/JScript pages using "object myFlower
= Server.CreateObject("TEST.myFlower);" and "myFlower.GetFlower(2);".
Just doesn't want to work in a Windows Form C# App.

Any suggestions? I've tried late binding, it seems more complicated
and I don't need late binding but it doesn't work either. Late
Binding code below:

Type objClassType;
objClassType = Type.GetTypeFromProgID("TEST.myFlower");
object objApp_Late = Activator.CreateInstance(objClassType);
objApp_Late.GetType().InvokeMember("GetFlower",
BindingFlags.InvokeMethod, null, objApp_Late, new object[] {55555});

It crashes at runtime with a "TargetInvocationException: Exception has
been thrown by the target of an Invocation." on the last line.

Thanks NB

Update: Reasion its not working in the C# Application is because the
"CmyFlower::OnPageStart(IUnknown* piUnk) {...}" isn't being called
automatically. I created an ASP.NET/C# page and in the default
codebehind file I make a function

public string GetFlower(){TEST.Flower myFlower = new TEST.Flower();
return myFlower.get_GetFlower(2);}

And in my form I do "<% Response.Write(GetFlower()); %>" But I still
only get null because a varible in the COMs "OnPageStart(IUnknown*
piUnk)" method isn't getting set. Does "new TEST.Flower();" not call
that function automatically like "Server.CreateObject("TEST.Flower");"
does?

I don't mind calling the "OnPageStart(IUnknown)" manually but I'm
unable to find what they want as the arg ("piUnk"). My ASP.NET Code
behind file extends System.Web.Page so I think I should have the
"piUnk" they want someplace... I tried giving it a random number or
string but it just throws an exception.

Any help would be much apprisiated.

NB
 
N

Nicholas Paldino [.NET/C# MVP]

You are not going to be able to make this component work in a Windows
Forms app.

When you call CreateObject on the server, it detects the presence of
this method, and passes the ASP (not ASP.NET) context to the object to be
created (and this seems odd, quite honestly, since you only have one ActiveX
exe running, it could have the context changed often).

When you just call new, it will create the object like it normally does,
without looking for the OnPageStart method.

To get around this, you will need to change the code in the ActiveX exe
to not try and access the intrinsic ASP objects (server, request, response)
if the OnPageStart method is not created.

Curious, why was this created as an ActiveX exe in the first place? Why
not as an in process server?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

NvrBst said:
My problem is that it keeps returing nothing after calling the
methods. I "TlbImp.exe TEST.exe /out:TESTCS.dll" and then in C# Right
click > Add Reference > Browse and give it the TESTCS.dll.

It adds "TESTCS" and I can use intellasence to see all the methods. I
do

TESTCS.Flower myFlower = new TESTCS.Flower();
MessageBox.Show(myFlower.get_GetFlower(2));

The "myFlower.GetFlower(2)" COM is suppose to return the 2nd flower
(stored in registry), which is a string. When I do this it says to
call accessor method directly "myFlower.get_GetFlower(2);" and this
just returns null.

I can use the COM EXE from my ASP/JScript pages using "object myFlower
= Server.CreateObject("TEST.myFlower);" and "myFlower.GetFlower(2);".
Just doesn't want to work in a Windows Form C# App.

Any suggestions? I've tried late binding, it seems more complicated
and I don't need late binding but it doesn't work either. Late
Binding code below:

Type objClassType;
objClassType = Type.GetTypeFromProgID("TEST.myFlower");
object objApp_Late = Activator.CreateInstance(objClassType);
objApp_Late.GetType().InvokeMember("GetFlower",
BindingFlags.InvokeMethod, null, objApp_Late, new object[] {55555});

It crashes at runtime with a "TargetInvocationException: Exception has
been thrown by the target of an Invocation." on the last line.

Thanks NB

Update: Reasion its not working in the C# Application is because the
"CmyFlower::OnPageStart(IUnknown* piUnk) {...}" isn't being called
automatically. I created an ASP.NET/C# page and in the default
codebehind file I make a function

public string GetFlower(){TEST.Flower myFlower = new TEST.Flower();
return myFlower.get_GetFlower(2);}

And in my form I do "<% Response.Write(GetFlower()); %>" But I still
only get null because a varible in the COMs "OnPageStart(IUnknown*
piUnk)" method isn't getting set. Does "new TEST.Flower();" not call
that function automatically like "Server.CreateObject("TEST.Flower");"
does?

I don't mind calling the "OnPageStart(IUnknown)" manually but I'm
unable to find what they want as the arg ("piUnk"). My ASP.NET Code
behind file extends System.Web.Page so I think I should have the
"piUnk" they want someplace... I tried giving it a random number or
string but it just throws an exception.

Any help would be much apprisiated.

NB
 
N

NvrBst

You are not going to be able to make this component work in a Windows
Forms app.

When you call CreateObject on the server, it detects the presence of
this method, and passes the ASP (not ASP.NET) context to the object to be
created (and this seems odd, quite honestly, since you only have one ActiveX
exe running, it could have the context changed often).

When you just call new, it will create the object like it normally does,
without looking for the OnPageStart method.

To get around this, you will need to change the code in the ActiveX exe
to not try and access the intrinsic ASP objects (server, request, response)
if the OnPageStart method is not created.

Curious, why was this created as an ActiveX exe in the first place? Why
not as an in process server?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




My problem is that it keeps returing nothing after calling the
methods. I "TlbImp.exe TEST.exe /out:TESTCS.dll" and then in C# Right
click > Add Reference > Browse and give it the TESTCS.dll.
It adds "TESTCS" and I can use intellasence to see all the methods. I
do
TESTCS.Flower myFlower = new TESTCS.Flower();
MessageBox.Show(myFlower.get_GetFlower(2));
The "myFlower.GetFlower(2)" COM is suppose to return the 2nd flower
(stored in registry), which is a string. When I do this it says to
call accessor method directly "myFlower.get_GetFlower(2);" and this
just returns null.
I can use the COM EXE from my ASP/JScript pages using "object myFlower
= Server.CreateObject("TEST.myFlower);" and "myFlower.GetFlower(2);".
Just doesn't want to work in a Windows Form C# App.
Any suggestions? I've tried late binding, it seems more complicated
and I don't need late binding but it doesn't work either. Late
Binding code below:
Type objClassType;
objClassType = Type.GetTypeFromProgID("TEST.myFlower");
object objApp_Late = Activator.CreateInstance(objClassType);
objApp_Late.GetType().InvokeMember("GetFlower",
BindingFlags.InvokeMethod, null, objApp_Late, new object[] {55555});
It crashes at runtime with a "TargetInvocationException: Exception has
been thrown by the target of an Invocation." on the last line.
Thanks NB
Update: Reasion its not working in the C# Application is because the
"CmyFlower::OnPageStart(IUnknown* piUnk) {...}" isn't being called
automatically. I created an ASP.NET/C# page and in the default
codebehind file I make a function
public string GetFlower(){TEST.Flower myFlower = new TEST.Flower();
return myFlower.get_GetFlower(2);}
And in my form I do "<% Response.Write(GetFlower()); %>" But I still
only get null because a varible in the COMs "OnPageStart(IUnknown*
piUnk)" method isn't getting set. Does "new TEST.Flower();" not call
that function automatically like "Server.CreateObject("TEST.Flower");"
does?
I don't mind calling the "OnPageStart(IUnknown)" manually but I'm
unable to find what they want as the arg ("piUnk"). My ASP.NET Code
behind file extends System.Web.Page so I think I should have the
"piUnk" they want someplace... I tried giving it a random number or
string but it just throws an exception.
Any help would be much apprisiated.
NB- Hide quoted text -

- Show quoted text -

My apologies. The ActiveX EXE was created for a web site, however, I
was running into problems with getting the COM object to work with the
ASP.NET/C# pages so I made a new C# App and thought if I got it workin
in this I'd get it working for the ASP.NET/C# (I'm more confortable in
C# App than ASP.NET/C#). I made a new blank ASP.NET/C# page and
working with that now (moved this topic to ASP.NET section).

Thanks very much for the reply :)
 

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