Calling methods within my C# application

Started by
5 comments, last by steg 20 years, 9 months ago
Hi all, I was wondering whether it is possible to have some sort of interface into my C# application that other programs can call ? I know you can use com/dcom objects but my program is a windows exe application. Thus, I just want to expose certain methods from within my program, is this possible ? Kind regards, Steve

If it isn't working, take a bath, have a think and try again...

Advertisement
quote:Original post by steg
I know you can use com/dcom objects but my program is a windows exe application.

It doesn''t really matter - you can still register your assembly for COM interop. This can be done either by checking the "Register for COM interop" checkbox in VS.NET, or by running regasm on the assembly after compilation.

This will register all public classes in the assembly as COM objects. There are also some attributes you can apply to control things like CLSIDs and progids.


AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Thanks Arild,

I'm quite new to C#, been a C++ developer for some ten years so assemblies don't mean a great deal to me. The scenario I have is that I have my C# application and a colleague has their MFC/C++ application. Now, their application wants to be able to call methods within my C# application, we are doing this at the moment by posting custom windows messages to and from each other. It would be nice if their C++ application could just call methods within my C# application (pretty much like calling a method within a COM object). Now, you are saying I just have to make the classes I want to make available as public and register these for COM interoperability ? I'm still unsure how the C++ application would call the methods in these classes - do they get a CLSID or something ?

AKA :

CComPtr srpDotNet;
srpDotNet.CreateInstance(CLSID_DotNetClass, CLSCTX_ALL);

I'm also taking it you need to create a type library from an assembly ?

Does this all work if my application is a standard EXE file and not a DLL ?

Sorry to sound confused.

Many thanks for your help.
Steve


[edited by - steg on July 6, 2003 5:33:45 AM]

If it isn't working, take a bath, have a think and try again...

quote:Original post by steg
I''m still unsure how the C++ application would call the methods in these classes - do they get a CLSID or something ?

Unless you explicitly apply the GuidAttribute to the class(es) in question, the registration process will select a random CLSID. This is hardly desirable, so I suggest you use the GuidAttribute. It will also get a progid with the namespace and the class name unless you specify the ProgId attribute.
quote:
I''m also taking it you need to create a type library from an assembly ?

You can export a type library by using the /tlb flag to regasm.
quote:
Does this all work if my application is a standard EXE file and not a DLL ?

Yes, it works.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
One thing you could try is make a DLL that contains a bunch of .NET interfaces for the things you want to expose. Your application would implement all of these interfaces. You could then make use of ye olde windows messaging system to use your C# application as a factory of sorts, feeding instances of those interfaces to the MFC application, which could use them by way of a little bit of managed C++.

Just be sure that you hold onto an instance over in C# land, or you may throw the garbage collector for a loop.

(this is all theory; I haven''t tried this myself)

I''m hip because I say "M$" instead of "MS".
"There is only one everything"
Arild - I have read a little more about this problem and the use of assemblies, it seems that to use COM interop I have to design/code my app as a class library, now as I have stated before, my application is a C# Windows form application - standard gui stuff, I can''t make this COM interop or certain classes within it(option is greyed out). Do you have to create a class library with COM interop that my C# app can use and also the C++ app will be able to use, problem is, will this ''class lib'' not be a different instance for each application ?

This is what I want to be able to do :

STANDARD C# WINDOWS FORM APPLICATION
/ \
|
| call method SetInformation(...) from C++ application
STANDARD MFC/C++ APPLICATION

Does this make sense ?

the Speed Bump - Thanks for the suggestions mate, trying to get away from windows messaging though :-)

Kind regards,
Steve

If it isn't working, take a bath, have a think and try again...

quote:Original post by steg
it seems that to use COM interop I have to design/code my app as a class library, now as I have stated before, my application is a C# Windows form application - standard gui stuff, I can''t make this COM interop or certain classes within it(option is greyed out).

regasm works just fine on exes.

Do you have to create a class library with COM interop that my C# app can use and also the C++ app will be able to use, problem is, will this ''class lib'' not be a different instance for each application ?

Well, factoring the logic of your application out into a separate DLL is a good idea in any case. The forms themselves shouldn''t contain anything thats not related to GUI logic.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

This topic is closed to new replies.

Advertisement