?
Solved

using a COM component in c#

Posted on 2003-02-24
15
Medium Priority
?
751 Views
Last Modified: 2012-05-04
Hi
I'm new to C# language.  I need to use a COM component in my Windows program.  How exactly should I add the component into my program.  This component fires events, and my program will need to perform actions when the events are fired, how do I map the events to a function? like how VB does it.

Thanks
0
Comment
Question by:kusamson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 4
15 Comments
 
LVL 1

Expert Comment

by:man068
ID: 8012649
You need to create a project in C# and then add references to the com dll's containing the functions you want to use then use the functions.

I suggest you use VB.NET instead of C# if you are already familiar with VB.

If you give more info i can help. I have developed a com add-in for mSproject using C#

thanks
0
 

Author Comment

by:kusamson
ID: 8012795
Hi,
I've added reference to the com dll and in my classview there's a component and code looks like this:

private System.ComponentModel.IContainer component;

how do I use this component?
I need to call the functions in this com dll and be able to receive event that is fired from it.

I am actually not familiar with VB, I worked mostly with C++ before and I'm learning C#.
0
 
LVL 1

Expert Comment

by:man068
ID: 8013512
the System.ComponentModel.IContainer is inbuilt into .net framework and is used to add components etc.. into it. Why are you saying its being imported through com dll?

this can be added to a project using Add->new Item->component class into the project.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:kusamson
ID: 8019266
Hi man068
I see
So System.ComponentModel.IContainer is not the component itself.  I thought it seemed strange.

I tried using Add->new component class.  It created a component class, and how can I add the class in the com dll into it?  Do I simply declare it inside the component class like: hilapi.hilclass hilCOM; ?

Perhaps I can explain a bit.
I have a dll: hilapi.dll which is a COM that's developed in Dehphi.  It has a class called hil and there are several functions and events.

Could you help me out a bit more?
Thanks
0
 
LVL 1

Expert Comment

by:man068
ID: 8019377
yes you can use that dll in your project. just add a reference to that dll in your project. right click on the "references" on solution explorer under the project and "add References". you will get a screen where you can add either .net, com  or other projects as references. Select com and if your com dll is registered on the system (using regsvr32 i guess) you should the dll in the list, add it and the .net creates a wrapper dll for that com dll exposing all the public methods/events.

thanks
0
 

Author Comment

by:kusamson
ID: 8021868
ok
I'm at the point where I can declare a hilapi object with
private hilapi.hilClass hilCOM = new hilapi.hilClass();

I am abel to call the methods within the object.
Now I need to handle the events that's fired from the object.  Could you help me with this?

Thanks
0
 
LVL 1

Accepted Solution

by:
man068 earned 300 total points
ID: 8022196
I think handling the events should be real easy. I presume you have events on which you can register methods as in case form events. eg., if a form has button cancelBtn

this.cancelBtn.Click += new System.EventHandler(this.cancelBtn_Click);

and you then define the method cancelBtn_Click appropriately to handle the event.

thanks
0
 

Expert Comment

by:Volcano_88101
ID: 8022458
Actually using COM Objects in .NET Is as easy as ever. No matter what language you use.

A Sample of how easy this is is by doing this
Activate a Form in Design View
Right Click on Toolbox
Click on "Customize Toolbox" (please wait may take a few minutes on slow computers)
Then click on the "COM Components" tab
Scroll down and find Microsoft Web Browser Control.
Select it and add it
Hit Okay and go to your toolbox and scroll down if it is hidden
You will now see a globe that looks like the earth
double click this and a white Screen will appear on your form.
Then click on the Object .
If you are using C# right click on the object and select Properties and click on the "lightning" Tab to view your Events

Your Events should be published in there.
The Default Event for WebBrowser is statuschanged or DocumentComplete.

But the point being The Com object is there. I have tested this with my own COM components and it works well in any language, Visual Baisc, C++, C#, Vb.net
0
 

Author Comment

by:kusamson
ID: 8028012
Unfortunately I was not able to find my component in "Customize Toolbox", or it would be a lot easier.

I'm doing what man068 suggested to add my event handler:

hilCOM.clearallconfirm += new hilapi.IhilEvents_clearallconfirmEventHandler(OnClearAllConfirm);

It compiled ok... but an Exception occurred when I try to hook up more than 1 event:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in interop.hilapi.dll
Additional information: exception from HRESULT: 0x80040202
0
 

Expert Comment

by:Volcano_88101
ID: 8030384
If you cant find your component then go to Browse and find either the DLL or OCX that it is. remember if youare doing stuff manually you might have to regsvr32 your component but it should already be done so just go in and browse to where it is and add it there.

0
 

Author Comment

by:kusamson
ID: 8031553
I tried browsing for the component, but nothing happened when i selected my dll.  Anyways, I am going with writing event handler and connecting them.
The only problem i have is the exception when I connect with more than 1 event:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in interop.hilapi.dll
Additional information: exception from HRESULT: 0x80040202

I actually tried in VB .NET, it's the same
0
 

Expert Comment

by:Volcano_88101
ID: 8031639
Do you have your sink helper class set to Private instead of public? this could be a possibility as posted

http://www.dotnet247.com/247reference/msgs/13/65596.aspx

a even more likliehood might be
HRESULT 0x80040202:

"Unable to open the access token of the current thread"

This might be solved by
HKLM\SOFWARE\Microsoft\Windows\CurrentVersion\Explorer\AlwaysUnloadDLL="
1"

as per
http://www.faqchest.com/msdn/ATL/atl-98/atl-9801/atl98010923_24559.html
0
 

Author Comment

by:kusamson
ID: 8038786
Yesterday I actually tried changing the sink helper class to public according to the same link you provided.  
It did not help.

I tried one of the component I created with ATL and it works fine with .NET.  
Could be that the COM component(hilapi) is written in Delphi?
0
 

Expert Comment

by:Volcano_88101
ID: 8038826
Possibly could be. Thats why hopefully it will be nice when everybody is using .NET for everything lol and we hopefully will have less and less to worry about the differences between 5 different langauges
0
 

Author Comment

by:kusamson
ID: 8043932
Although I cannot resolve the problem with HRESULT:0x80040202 exception, I have to give credit to man068 for guiding me to work with event in C#(which was what I really needed).  
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question