Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COM Component calling a method in C# .Net app 1

Status
Not open for further replies.

traimo

Programmer
May 7, 2003
13
0
0
US
In my VS .NET Solution I have two projects. One is the main windows application(EXE) and the other is a COM component(DLL). The com component talks to an IE Toolbar. My problem is that I need the COM component to also talk to the main windows app(EXE). My first crack at this was to have the main app write the data to the database and then my COM component can read the data from the DB. This worked but it is too slow because of the DB calls. My second try was to create the COM component inside the main app. Therefore, there would be no DLL project. I registered the com component via "regasm app.exe", but I get an error in my javascript(IE toolbar) on "var x = new ActiveXObject('MyApp.COMComp');". It is like the COM component was never registered. Does anyone have any suggestions on how I can get a COM component to call a method of a C# .NET application?

Thank you,
Tony
 
I believe you typically create a com object as a dll and use regsvr32.

Your application should be recieving events from the com object as long as the communication is really only 1 way.

COM -> Throws Event -> Application does something

Otherwise you need to register you application (possibly using an interface) to have your dll talk to your app directly.


Does that get you started?
 
We just dealt with this exact issue. Where were you 2 weeks ago?

Star for you!
 
B00gyeMan,

I read those articles. Thanks for the help. However, I still can't get it to work. In the properties of my project the "Register for COM Interop" field is greyed out. Is it possible to register a COM Component in an executable (EXE). The methods that I want to expose to COM are in my EXE and not a DLL. I tried to register the EXE manually via REGASM but I still get an Automation Error like the EXE is not registered. I think I'm on the right track but I just need to put the pieces together. Any clue?

Thanks,
 
After doing a little more research, it looks like "Register for COM Interop" is only available for Class Libraries (DLL). So, I guess my question is this:

Under the SAME VS .NET Solution, can a method in my DLL call a method in my EXE? I may be making this a lot harder than it really is.

Thank you,
 
Just to clarify, if I had a solution with 2 projects that consisted of my main app (EXE) and a DLL and I wanted the EXE to call a method in my DLL, I would create a reference to the DLL in my main app (EXE). Then I could instantiate an object and call the method.

Is there anyway to do the reverse of that? Meaning, can you call a method in the EXE from the DLL. I need to do this because my IE toolbar calls COM methods of that DLL and I need the DLL to access data that resides in the EXE.

Thank you,
 
You would create an interface in your dll that a class in your exe uses. Then you can set an instance of that interface in your dll for it to use.

Your dll doesn't know what object it is but it does know that it uses the interface so it must have specific methods.
 
First of all, thanks for all your help on this. I really appreciate it.

I'm getting the following message:
Cannot create an instance of the abstract class or interface 'MyDLL.ifFormField'


Here is my code:

in my DLL
------------------
public interface ifFormField
{
int Test();
}

public int getTestValue()
{
ifFormField ifFF = new ifFormField();
return ifFF.Test();
}


In my EXE
--------------------------
public class User: MyDLL.ifFormField
..
..
..
public int Test()
{
return 5;
}
 
You cannot create an instance of an interface, rather an object that uses that interface.

You should set your ifFormField like this:

private ifFormField frm;

public void SetFormField(ifFormField f)
{
frm = f;
}


Now you can use the methods in that interface.

frm.getTestValue();
 
OK, I get it. But, where do I call SetFormField? In the EXE or the DLL? And what do I pass into it? Thanks for your patience and help.

Tony
 
you would create a class in your exe that implements the ifFormField interface.

public class myFormField : ifFormField()
{
...
}

Then your exe would use that class to setFormField.

public Form1()
{
InitializeComponents();

myFormField ff1 = new myFormField();

mydllclass.SetFormField(ff1);
}


Hope that gets you started. You might also want to google interface tutorials in C#
 
I guess I still don't see how this is going to allow me to invoke a method in my EXE from my DLL. I'll check out some tutorials and then try your code.

Tony
 
After doing some more research on interfaces, I think I know what you are suggesting. However, I don't think it will work in my case. The reason being is that my EXE is not initiating the call to the COM component in my DLL. The initial call is coming from my IE toolbar via javascript. Basically, the calls are as follows:

1. javascript in IE toolbar calls COM DLL
2. COM DLL calls method in EXE.

The reason I'm trying to do this is that the EXE is running and has already accessed the database and the data is contained in a DataSet. I'm trying to access that data directly from the DataSet in the EXE so my COM component doesn't have to make a DB call. I'm not sure if this is possible. What do you think?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top