Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / April 2006

Tip: Looking for answers? Try searching our database.

AxHost and ActiveX initialization

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Raj Wall - 23 Apr 2006 18:15 GMT
I have a third-party ActiveX component that I have brought into my c#
application using the AxHost wrapper. This gives me two assemblies,
AxInterop.Foo and Interop.Foo. Inside of each of these is a class with the
methods I expected to find--the functionality I want from the ActiveX
component.

Two questions: Which of these assemblies contains the "real" interface to
the component? Does it make a difference? I am currently using the
AxInterop.Foo assembly (which contains a namespace AxFooLib and a class
AxFoo.

Second, what is the correct way to initialize/setup the component? If I
create a new AxFoo and try calling one of the methods (e.g., connect())  I
get an InvalidActivXStateException error. I tried calling BeginInit() and
EndInit() before calling connect(), but that didn't help.

Thanks for your help!

Regards,
Raj
"Peter Huang" [MSFT] - 24 Apr 2006 06:46 GMT
Hi,

For commonly COM DLL(not an ActiveX), the IDE will only generate one
assembly Interop.Foo.
For the ActiveX, it will generate one more DLL, AxInterop.Foo.

After we add the ActiveX via IDE, it will appear in the toolbox and so we
can add the item onto the form directly just as we do with a common winform
control.
Here is a link for your reference.
Importing ActiveX Controls
http://www.codeproject.com/dotnet/activexnet.asp

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Raj Wall - 24 Apr 2006 17:34 GMT
Peter, hi,

Thanks for the link. What I have done is:

- Brought the ActiveX component into VisualStudio using that link's method 2
(Customize Toolbox, browse to it, etc)
- Dragged (drug? "Drag, Dragged, Drug, hast Gedruggen") the control onto the
application form (though it is NOT a graphical element but a server
proxy--does that make a difference?)
- Created a new instance of the AxInterop object
- Used the AxInterop methods as exposed (solution references include both
the AxInterop and Interop) to interact with the component

At this point all the documentation agrees: everything should be working
fine at this point. However, the first method one calls with this server
proxy is it's connect() method, and it is at that point that I get the
"InvalidActiveXStateException".

Is there something else I should do if the ActiveX component is a server
proxy?
Is there something I should do to make sure the component is started
properly?
Or is an Invalid State and indication of some other problem?

Thanks for your help!

Regards,
Raj

> Hi,
>
[quoted text clipped - 21 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
"Peter Huang" [MSFT] - 25 Apr 2006 03:30 GMT
Hi Raj,

If this is a COM DLL without UI, I think you may try to add reference to
the COM( but not add it into the toolbox and drag drop onto the form).
After adding reference, there will generate only one DLL.
We can create the object in the wrap class.
e.g.
The reference may be ABCLib
So we can create it similar with below.
IABCObj o = new ABCObjClass();

You may have a try and let me know the result.
Also what do you mean by "Server proxy", do you mean it is an object for an
proxy of a remote COM+ server?
Commonly, .NET code will do the same job as win32, except that it needs a
wrap.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Raj Wall - 25 Apr 2006 17:40 GMT
Peter, hi,

Thanks again for all your help. Let me change my question a bit as I think I figured out part of what my problem was.

First, yes the ActiveX component is a connection to a remote server (broker's order entry system). What I really need to do is have multiple copies of the ActiveX component. That is, I need a connection to the broker for each account being traded. The problem is my program does not know before hand how many accounts it will be needing to make connections for. Apparently just creating a "new" instance of the component for each new account doesn't set them up properly.

The advantage of dragging the component on to a form seems to be that VS generates a bunch of code for me:

--------------------------------------------------------------------------------

this.axTws1.Enabled = true; this.axTws1.Location = new System.Drawing.Point(89, 199);this.axTws1.Name = "axTws1";this.axTws1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axTws1.OcxState")));this.axTws1.Size = new System.Drawing.Size(100, 50);this.axTws1.TabIndex = 0;this.axTws1.tickPrice += new AxTWSLib._DTwsEvents_tickPriceEventHandler(this.axTws1_tickPrice);--------------------------------------------------------------------------------If I try to duplicate this generated code for objects I create manually, I get a compiler error about the context of the "resources" variable. And, since it is really "non-visible", I don't care about the size, location, etc.

So, here's my new question:

Should I (can I) create a complete new copy of the form built in the designer with the ActiveX component on it--and hopefully get them all started up properly? Or is there a way to "drop" multiple copies of the component onto a running form?

Thanks again for your help!

Regards,
Raj

> Hi Raj,
>
[quoted text clipped - 23 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 26 Apr 2006 03:56 GMT
Hi

Do you mean you just want one instance of the ActiveX class in memory in
the lifetime of the form1?
If so, we can delare the object in the form level but not in certian method.
e.g.
class Form1
{
IABCObj o = new ABCObjClass();
Form1()
{
}
....
}

Also we can create more than one form1 instance.
e.g.
In certian method, we can call.
Form1 fm = new Form1();
fm.Show();

So that another form1 will show in addition to the original one.

"Or is there a way to "drop" multiple copies of the component onto a
running form? "
I did not understand the question. Do you mean  you want to drop more than
one ActiveX component onto form1 and run Form1?
If so, we just need to drag&drop more than one onto the form.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Raj Wall - 26 Apr 2006 20:49 GMT
Peter, hi,

Thanks for the response, and sorry for the confusion.  What I need a copy of
the ActiveX component for each account. Each copy will manage a  connection
to the server. If I just create an instance of the component, however, the
component is not initialized properly. That gives me the
InvalidActiveXStateException with the first call to thecomponent.

If instead I only use the object generated by VS from pretending the
component is visible (placing it on a form), I have no problem.

Finally, if I pursue option One (simply create instances of the component as
I need them) and then duplicate the code VS uses to initialize, I again get
the InvalidActiveXStateException error.

So my question is, what am I missing in my initialization? I'll include my
code at the end of this message. What is the "secret sauce" VS is performing
to initialize a "visible" component that I need to do on an "invisible"
component? In particular, I don't understand what the correct syntax for the
resources.GetObject("MytwsObj.OcxState")

should be.

Thanks for your help!

Regards,
Raj

Here is my initialization code "copied" from similar code generated by VS:
private AxTWSLib.AxTws MytwsObj;

MytwsObj = new AxTWSLib.AxTws();

System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(TWSInterface));

((System.ComponentModel.ISupportInitialize)(MytwsObj)).BeginInit();

MytwsObj.Enabled = true;

MytwsObj.Name = "axTws0" + account;

MytwsObj.OcxState =
((System.Windows.Forms.AxHost.State)(resources.GetObject("MytwsObj.OcxState")));

> Hi
>
[quoted text clipped - 37 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
"Peter Huang" [MSFT] - 27 Apr 2006 04:46 GMT
Hi

The Designer will store some ocxstate property data into winform
application's resource section.
The code you mention is to retrieve the data back.

Based on my test, even if we comment out the code line
//this.axMyUserCnt1.OcxState =
((System.Windows.Forms.AxHost.State)(resources.GetObject("axMyUserCnt1.OcxSt
ate")));
The application will run OK.

Also I am somewhat confused about your scenario.
When did you get the InvalidActiveXStateException, use the AxInterop.dll
and Interop.dll and drag & drop from toolbox or use the Interop.dll only
and add reference to the dll only?

What is the difference using the two approach.

Also I think there may have some incompatible to use the control in .NET,
so you may try to wrap the control in a VB6 ActiveX control and then use
that control in .NET to see if that works.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Raj Wall - 27 Apr 2006 19:55 GMT
Peter, hi,

Thanks for all the time you've spent on this. It turns out I should have
been paying better attention to one of the responses I got to my first
message, which suggested adding

formFoo.Controls.Add(newComponent);

which solved the problem. Anyway, thanks for your help!

Regards,
Raj

> Hi
>
[quoted text clipped - 30 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
"Peter Huang" [MSFT] - 28 Apr 2006 02:38 GMT
Hi Raj,

I am glad that you have resolved the issue.
Thanks!

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Christian Fröschlin - 27 Apr 2006 08:17 GMT
> If instead I only use the object generated by VS from pretending the
> component is visible (placing it on a form), I have no problem.

If it is an ActiveX control it needs a container, so you have to
add it to a Form or the likes, regardless of whether you want it
to be visible or not. What exactly is the problem? You can add as
many instances to a Form in code as you like, and they don't need
to be visible (probably you don't even need to show the Form).
Christian Fröschlin - 24 Apr 2006 08:49 GMT
> Second, what is the correct way to initialize/setup the component? If I
> create a new AxFoo and try calling one of the methods (e.g., connect())  I
> get an InvalidActivXStateException error.

Try adding the control to a Form before using it (Controls.Add).

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.