adding / calling C# code inside my C++ MFC app 
Author Message
 adding / calling C# code inside my C++ MFC app

What is the latest methodology for adding and calling C# code
inside my 71,000 line C++ MFC app ?

Will this get any better in the future ?

Thanks,
Lynn McGuire



Sat, 08 Jan 2005 00:04:25 GMT  
 adding / calling C# code inside my C++ MFC app

Quote:
> What is the latest methodology for adding and calling C# code
> inside my 71,000 line C++ MFC app ?

> Will this get any better in the future ?

Build your C# code into an assembly:
CSharpCode.cs   [csc /r:System.Windows.Forms.dll /t:library CSharpCode.cs]
----------------
public class CSharpClass {
    public static void DoSomething(string msg) {
        System.Windows.Forms.MessageBox.Show(msg);
    }

Quote:
}

Define functions that will call into your C# code:
MFCCode.h
-------------
#include "tchar.h"
void CallMyCSharpCode(TCHAR *msg);

Implement those functions using Managed C++:
MFCCode.cpp   [cl /clr /c /MD MFCCode.cpp]
----------------
#using <mscorlib.dll>
#using <CSharpCode.dll> // reference your C# assembly
#include "MFCCode.h"
void CallMyCSharpCode(TCHAR *msg) {
    CSharpClass::DoSomething(new System::String(msg));

Quote:
}

Call those functions from your 71,000 line app:
Main.cpp  [cl /MD Main.cpp MFCCode.obj]
----------
#include "tchar.h"
#include "MFCCode.h"
int _tmain(int argc, TCHAR **argv) {
    CallMyCSharpCode(argv[0]);
    return 0;
Quote:
}



Sat, 08 Jan 2005 02:21:59 GMT  
 adding / calling C# code inside my C++ MFC app

Quote:
> Build your C# code into an assembly:

So I have to put my C# code into a DLL ?  Bummer, I was really
hoping that I could debug from my exe into the C# code.   Or, am
I wrong about the debugging ?

Quote:
> Define functions that will call into your C# code:

Does this managed C++ code go in my exe or the dll ?

Thanks,
Lynn



Sat, 08 Jan 2005 02:48:58 GMT  
 adding / calling C# code inside my C++ MFC app

Quote:
> > Build your C# code into an assembly:

> So I have to put my C# code into a DLL ?  Bummer, I was really
> hoping that I could debug from my exe into the C# code.   Or, am
> I wrong about the debugging ?

Not a problem; the de{*filter*} can easily step between unmanaged C++, Managed
C++, C#, VB.NET, etc...

Quote:
> > Define functions that will call into your C# code:

> Does this managed C++ code go in my exe or the dll ?

The managed C++ code gets compiled into the exe, which BTW, introduces a
dependency on the .NET runtime.

-Sean



Sat, 08 Jan 2005 05:39:58 GMT  
 adding / calling C# code inside my C++ MFC app
If this is still to be written code, the easiest way would just to be to
write in (M)C++, then you have the most flexibility in where to put the code
(inside your exe, inside a separate DLL, ...).

Ronald Laeremans
Visual C++ compiler and libraries team


Quote:
> What is the latest methodology for adding and calling C# code
> inside my 71,000 line C++ MFC app ?

> Will this get any better in the future ?

> Thanks,
> Lynn McGuire



Sat, 08 Jan 2005 05:59:28 GMT  
 adding / calling C# code inside my C++ MFC app

Quote:
> If this is still to be written code, the easiest way would just to be to
> write in (M)C++, then you have the most flexibility in where to put the code
> (inside your exe, inside a separate DLL, ...).

I assume that the (M) means "managed" C++ code.

How does one add resources to managed C++ code ?  This feature
does not seem to work in vs.NET.

This code is yet to be written.  We are converting a 115K line exe
written in the old Whitewater Actor language (win16) to a more
modern environment.   This Win16 exe uses WM_COPYDATA to
communicate with our 71K line C++ MFC exe.  We would like to
combine the 2 exe's into a single exe.  C# conversion of our Actor
code seems to be much more natural than that of C++.

Thanks,
Lynn



Sat, 08 Jan 2005 07:01:19 GMT  
 adding / calling C# code inside my C++ MFC app

Quote:
> I assume that the (M) means "managed" C++ code.

yes

Quote:
> How does one add resources to managed C++ code ?  This feature
> does not seem to work in vs.NET.

unmanaged resources are the same as before, but for managed resources you do
not have the facilities that C# offers. However, changing your managed C++
projects to have managed resources is quite simple.

The managed resource can be compiled or not, depending on how you want to
read it. If it is not compiled then you need to pass the name of the
resource using the linker /assemblyresource switch (in VC++  project
properties, linker, input, embed managed resource file).

If the resource is compiled (so that you can load it with ResourceManager)
you need to add the source to the project and from its property pages go to
custom build step and in the command line use

resgen $(InputFileName) $(IntDir)/$(InputName).resources

then add the compiled resources (in the intermediate folder) to the linker
as above.

You can also create satellite assemblies with C++, but it is more involved
and I won't go into the details here.

Note that there are no tools in VS.NET for C++ projects to add binary
resources to .resx files (the source files for resgen). If you want to add
binary resources (eg icons and cursors) as compiled managed resources, it is
best to do this in a C# project and copy the .resx to the C++ project.
Details of all of this is in my C++ book for MSPress.

Richard

--
Richard Grimes [MVP]
author: "Programming with Managed Extensions for Visual C++ .NET",
the Microsoft Press book about Managed C++



Sat, 08 Jan 2005 22:40:27 GMT  
 adding / calling C# code inside my C++ MFC app
Another option for creating images/strings based resources is:
http://msdn.microsoft.com/library/en-us/cptutorials/html/resource_edi...
seditor_.asp

By default it is under:
X:\Program
Files\Microsoft.NET\FrameworkSDK\Samples\Tutorials\resourcesandloc
alization\reseditor\



Quote:


> > I assume that the (M) means "managed" C++ code.

> yes

> > How does one add resources to managed C++ code ?  This feature
> > does not seem to work in vs.NET.

> unmanaged resources are the same as before, but for managed resources you
do
> not have the facilities that C# offers. However, changing your managed C++
> projects to have managed resources is quite simple.

> The managed resource can be compiled or not, depending on how you want to
> read it. If it is not compiled then you need to pass the name of the
> resource using the linker /assemblyresource switch (in VC++  project
> properties, linker, input, embed managed resource file).

> If the resource is compiled (so that you can load it with ResourceManager)
> you need to add the source to the project and from its property pages go
to
> custom build step and in the command line use

> resgen $(InputFileName) $(IntDir)/$(InputName).resources

> then add the compiled resources (in the intermediate folder) to the linker
> as above.

> You can also create satellite assemblies with C++, but it is more involved
> and I won't go into the details here.

> Note that there are no tools in VS.NET for C++ projects to add binary
> resources to .resx files (the source files for resgen). If you want to add
> binary resources (eg icons and cursors) as compiled managed resources, it
is
> best to do this in a C# project and copy the .resx to the C++ project.
> Details of all of this is in my C++ book for MSPress.

> Richard

> --
> Richard Grimes [MVP]
> author: "Programming with Managed Extensions for Visual C++ .NET",
> the Microsoft Press book about Managed C++



Sat, 08 Jan 2005 23:19:32 GMT  
 adding / calling C# code inside my C++ MFC app

Quote:
> The managed C++ code gets compiled into the exe, which BTW, introduces a
> dependency on the .NET runtime.

Found an MSDN article on it.  I even have the issue at home somewhere.

Visual Studio .NET: Managed Extensions Bring .NET CLR Support to C++
  http://msdn.microsoft.com/msdnmag/issues/01/07/vsnet/vsnet.asp

We can add managed C++ code to our MFC C++ exe.  Then we can
create a DLL of our Actor replacement C# code to be called by the
managed C++ code in the exe.

It's doable.  Also allows you to use Forms which I vastly prefer over MFC
dialogs (much more flexible).

Thanks,
Lynn



Sun, 09 Jan 2005 08:01:03 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. calling C# from C++ MFC app ?

2. Calling another application from inside your MFC app

3. New C# Source Code Sample - Running JScript.NET Code from C# Apps

4. Can I call C# dlls from C++ Apps?

5. C# client crashs when calling into Managed C++ which calls unmanaged c++ function

6. calling C# code (dialog) from MFC message map

7. Calling MFC code from Win32 app?

8. Calling C++ from C/Linking (was: C++ code to C Code)

9. JavaScript calling c++ functions defined in MFC app

10. Embed VB code inside of C++ code?

11. NewbieQ: Using old C++ code in MFC app

12. Adding a DSN from inside an App

 

 
Powered by phpBB® Forum Software