|
|
Title |
How to Call a COM object from C#.
|
Summary |
This is a very simple task that can be used to use code written in a verity of languages. |
Contributor |
John McTainsh
|
Published |
15-Feb-2001 |
Last updated |
15-Feb-2001 |
|
|
Description.
COM object are a fast and simple way to add power to a web page or application.
It allows functionality you to leverage your skills in another language from C#. I have
also found that if I don't have time to work out how to do it in C# then I write it
in C++ as a COM object and call the COM from C#. This simple code sample shows how
to do this with a simple COM object.
Here is a sample.
In this sample I will call an Object called Search in a DLL
called McTainshCom.dll . I will access a property called
BuildNo .
- First Open the "Solution Explorer" for the project you want to the COM object from.
- Expand the "Application" branch.
- Right click on the "References" branch and select "Add Reference".
- Select the "COM" Tab. (Wait a we while!)
- All registered COM objects should appear in the list. If the COM you want is not in the list, check it is registered.
- Highlight the Object you want to use "McTainshCOM".
- Press "Select" then "OK".
- The object should now appear in the references list.
The following code segement shows how to use the COM object
MCTAINSHCOMLib.SearchPage sr = new MCTAINSHCOMLib.SearchPage();
SearchPage sr = new SearchPage();
short nBuild = sr.BuildNo();
string sBuild = Int32.ToString( nBuild );
Alternatively you can include the namespace to save some typing.
using MCTAINSHCOMLib;
...
SearchPage sr = new SearchPage();
short nBuild = sr.BuildNo();
string sBuild = Int32.ToString( nBuild );
How to unload the COM object if using WebForms/ASPX.
Once the COM object has been loaded it will be kept in memory and can not be
replaced until IIS has been stopped. To do this either restart the computer
(quite serious) or stop the World Wide Web service. There is also a button on
the Internet Services Manager. Select the web site manager properties and open the
Home Directory tab. Here there is an Unload button. I have never had any
success with this button but it is worth a try.
|