Cross-language.

I have a programm which supports plug-ins and there is a 'manual' how to write it on C++.
I'd like to write my plug-in using C#.

Here is some valuable extracts from THAT manual.
1. Base application was written with C++ and using MFC. So they begins creating plug-ins by choosing 'Regular DLL using shared MFC DLL' in MSVC.

2. Next main step is to write two functions in

sample.h:
class SampleClass : public CWinApp
{
public:
SampleClass();

void OnLoad();
void OnInitialize(CMapStringToString&);
}

sample.cpp:
SampleClass theApp;

__declspec(dllexport) void OnLoad() {theApp.OnLoad();}
__declspec(dllexport) void OnInitialize(CMapStringToString& str) {theApp.OnInitialize(str);}

sample.def
OnLoad= OnLoad@@YAXXZ
OnInitialize= OnInitialize@@YAXAAVCMapStringToString@@@Z

3. Plugin forms must be in MDI style and the main program would call functions OnLoad() and OnInitialize(something)

Is it possible to write all of the above using C# and please tell me how to do it
Maybe show me any directions in which I could move. Maybe read something...
Thanks a lot.

Andy.



Answer this question

Cross-language.

  • Will Merydith

    I don't say it's impossible to call managed code from unmanaged code but you still have to write a DLL with a wrapper in C++. So you still have to learn C++ and it doesn't solve your problem.

  • stswordman

  • GSReddy

    I understand that C# is much easier, but you must understand that it compiles to MSIL instead of native code. It can only be run within the CLR and it has no possibility of exposing an interface in the way a classic dll did. And that is what you need. Trust me, it is not gonna work.

  • koldFUSiON

    I don't think so. It should take much more time to jump from c# to c++. I already tried and realised that i can't. C# is much more easy language than C++. And it hard TO ME write anything simple with C++.

    All I need is a 'cover' for this functions in c#!

    And I don't believe that it is impossible.


  • msaradhi

    Sounds very tricky. Calling unmanaged code from within managed code is very easy but you want it the other way around. Calling a managed dll from within unmanaged code. I have never seen it and I think it's gone take a lot more time than just writing your extensions in C++.

  • Tom De Cort

    I've been almost unerstood.

    Please micvos, correct me if I am not right.

    I must to write a C++ DLL, as shown in my manyal for base application. This project should contain a managed wrapper of unmanaged code which is called from base application (i.e. OnLoad and OnInitialize). Also in this metods I'd place a call of the similar external MANAGED metods (something like redirection).

    And with c# I write everything managed and put in in another DLL.

    So the base application calls OnLoad which is in c++ dll, and it calls (from inside) another OnLoad2 from my c# dll.

    Would it work at all (if I write all these dll as I describe before).


  • J_Craig

    Almost right. "This project should contain a unmanaged wrapper of managed code which is called from base application". This would work, but it's al lot of work to write it in this way. There is another problem which I'm not sure of how it will affect the application. If you're going to build MDI forms as defined in .NET framework than there might be a problem if the're going to be called from the C++ application which is based on the Microsoft Foundation Classes framework. Maybe you still have to import the MFC in your C# application or start working with WinAPI calls. Try it and see if it works. I still think that learning C++ is the faster way to accomplish things.

  • Cravensadie

    I don't see this as practical. (that is, I won't see it's "impossible", but it'll be really, really difficult)

    The problem is that OnInitialize expects a CMapStringToString object (from MFC). The class is conceptually similar to the StringDictionary class in .Net, but they aren't binary-compatible, which means you'll need module to manually handle the marshalling between the two. The module would have to be able to understand both a StringDictionary and a CMapStringToString and copy every item from one to the other. This might be possible in Managed C++ code with unsafe section --- but, this module would also need to derive from MFC's WinApp class, which I don't think is possible in Managed C++.



  • Cross-language.