Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

using a COM component in c#

Posted on 2003-02-24
15
Medium Priority
?
769 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

572 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