![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
Combining C++ and C#
Wasn't sure whether to put this in the this forum or the C# one ... anyway...
Is it possible to write a nice user interface in C# but get it to call a number-crunching routine written in C++ (and compiled to native code, rather than bytecode)? Can Visual Studio 2005 version still compile its C++ code natively or is everything tied CLI runtime now? (If not, I suppose I'll have to look into mingw's DLL capabilities...) The only info I've found about code mixing so far involves DLL's. I think I've got the C# part working (see end of the post - it's supposed to call a function from the DLL which as an experiment should do what the commented out C# code did) But I'm not sure how to create the DLL in the first place. The new project wizard seems to give options for 'MFC DLL's and 'COM DLL's and so on ... is it possible to compile a 'normal' DLL? Or even better, is there a way to do this without DLLs? csharp Syntax (Toggle Plain Text)
(places I've looked: http://www.adp-gmbh.ch/csharp/call_dll.html http://www.codersource.net/csharp_image_Processing.aspx for the bitmap stuff) |
|
|
|
|
|
#2 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 3
![]() |
You can mix and match .NET languages, so an assembly written in C++/CLI will mesh with an assembly written in C#. However, if you want to mix native C++ with a .NET assembly, you pretty much need to create a DLL in C++ and then uglify your C# to connect to it.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
Yes, it looks like things will get ugly...
It seems that to create a 'normal' DLL you have to create a C++ 'console project' and then select the DLL option, instead of being distracted by all the things that mention DLLs on the first step. All hail the great Microsoft for making things easy to find. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
Woah, that last link I found actually got it working
IT'S ALIVE!Thanks for the input. |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
You could use Managed C++ as well. Managed C++ uses the .NET framework, but can be integrated very easily with native C++ (even in the same source file), by using the #pragma unmanaged tags.
I use it myself now for a relatively big project, and it works like a charm: the ease of use of the .NET framework and the speed of native code. No ugly DLL function declarations or anything - it just works. |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
Thanks, I'll look into that.
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
Ok, I added and referenced a C++ project in a C# solution, and it compiles fine, but when it tries to access the C++ code during runtime it says:
System.IO.FileLoadException was unhandled Message="Attempt to load an unverifiable executable with fixups (IAT with more than 2 sections or a TLS section.) (Exception from HRESULT: 0x80131019)" I tried putting the C# call in an 'unsafe' block but it made no difference. Any ideas as to where I'm being stupid? :p The C# code is in a button click event: csharp Syntax (Toggle Plain Text)
C++ header: cpp Syntax (Toggle Plain Text)
C++ code: cpp Syntax (Toggle Plain Text)
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
I changed a compiler option for the C++ project to "Pure MSIL Common Language Runtime Support (/clr : pure)" and now it works. But now the assembly can't contain unmanaged code...
I assume this means that C# can't interface nicely with mixed C++ assemblies - you'd have to wrap the mixed C++ with another "pure MSIL" C++ class and only call the "pure MSIL" wrapper from C#? ---EDIT - seems to give the same error. Or ditch the C# and use a 'windows forms' managed C++ GUI? Last edited by Klipt; Dec 12th, 2006 at 1:22 PM. |
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Yes, I suggest dropping C# and doing everything in C++/CLI. Almost the same as it uses the same framework, and it makes things a lot easier.
|
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
Hmm ... ease of Delphi and power of C++ ... very tempting
![]() I finally found what I was doing wrong above: trying to link to a mixed .exe. I changed the C++ project to a DLL and it worked fine. So it seems C# can link easily to a mixed or pure .dll, but only to a pure .exe. Writing a managed wrapper for the unmanaged functions only saves you from [importing functions explicitly]. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Combining strings for os.system() | peaceofpi | Python | 12 | Sep 14th, 2006 7:35 AM |
| Combining languages | titaniumdecoy | Other Programming Languages | 12 | Jul 13th, 2006 2:03 PM |
| Combining Values in an array | bigmitch | C++ | 4 | Feb 10th, 2006 7:29 AM |