Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2004

Tip: Looking for answers? Try searching our database.

C++ and C# interoperability

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bruce Schechter - 01 Jun 2004 01:43 GMT
Background:  I have nearly two years of experience with C# and the .NET
framework, as well as a fair amount of C experience from many years ago, but
no C++ background (until about two months ago.)  Now, I am helping a team
develop an application with a UI based on C# and business logic coded in
C++.  The choice of C++ is because the team has several key elements of the
business logic already coded in VC++ (from pre- .NET tools.)

Ideally I'd like to compile the final project altogether within vs.net 2003,
in which I'd combine the C# UI project with the C++ business layer project
resulting in one .NET executable.  But after reading up on these topics in
MSDN, I've run into some issues about which I need advice regarding these
two points:

1.) It looks like the only way to combine the C++ code into the .NET project
is to convert it into "managed code" (which involves quite a bit of
modifications to the C++ source, like tagging each class with the __gc
modifier, etc.)  Is that a correct understanding/assessment?   Sounds like a
tedious  job on the C++ side, and would totally "break" our ability to
re-use the C++ in future non-.NET projects, correct?  Is there a mechanism
to write C++ code that can be conditionally compiled as either managed or
not managed?

2.) I gather that we could compile the C++ modules into one DLL and then
access it from the .NET project using some kind of wrapper??  I frankly don'
t fully understand what this means.  Could someone recommend a link to a
good article on this topic?

Which of these paths makes the most sense, if we want to keep the C++ code
in standard (as close as possible to ANSI standard) format for potential
portability?  Am I missing any other good approaches to the overall
strategy?

Thanks,

Bruce
Jesse McGrew - 01 Jun 2004 03:04 GMT
[...]
> 1.) It looks like the only way to combine the C++ code into the .NET project
> is to convert it into "managed code" (which involves quite a bit of
[quoted text clipped - 4 lines]
> to write C++ code that can be conditionally compiled as either managed or
> not managed?

To access the same classes directly from C#, yes, you would have to do
that. There is a #define that you can check to see if you're compiling
managed code, but I can't think of it offhand... and the number of
differences between what unmanaged and managed classes are allowed to do
would make this a nightmare, IMO.

> 2.) I gather that we could compile the C++ modules into one DLL and then
> access it from the .NET project using some kind of wrapper??  I frankly don'
> t fully understand what this means.  Could someone recommend a link to a
> good article on this topic?

This is what I'd suggest. Here's an article about it:
http://www.ondotnet.com/pub/a/dotnet/2004/03/29/mcpp_part3.html

Jesse
Gary Chang - 01 Jun 2004 04:05 GMT
Hi Bruce,

Additionally, you can make your C++ business logic code into COM
components, them use COM Interop to access them from C# applications:

COM Interop Tutorials
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/
vcoricominteroptutorial.asp

COM Interop Part 1 Sample
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cssample/ht
ml/vcsamCOMInterOpSample.asp  

Advanced COM Interop
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconadvancedcominterop.asp

Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Bruce Schechter - 01 Jun 2004 17:20 GMT
Gary and Jesse,

Thanks very much for your answers and links.  Although I have more analysis
to do, it tentitively looks to me like the best solution in my case is to
keep our C++ code independent of the .NET framework, compile it into a COM
component, and then let vs.net take care of building the wrapper around it
for interop with my C# UI.

Thanks again,
-- Bruce

> Hi Bruce,
>
> Additionally, you can make your C++ business logic code into COM
> components, them use COM Interop to access them from C# applications:
>
> COM Interop Tutorials

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/
> vcoricominteroptutorial.asp
>
> COM Interop Part 1 Sample

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cssample/ht
> ml/vcsamCOMInterOpSample.asp
>
> Advanced COM Interop

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
> l/cpconadvancedcominterop.asp
>
[quoted text clipped - 8 lines]
> This posting is provided "AS IS" with no warranties, and confers no rights.
> --------------------
Gary Chang - 03 Jun 2004 04:38 GMT
Hi Bruce,

After re-considering your problem carefully, I recognized that the best
approach for your case is used the IJW to interop between the unmanaged and
managed code, I agree with the Jesse and Nat's opinion.

The COM interop is for use of calling OLE Automation compatible COM
components(such as some Office automation components), which it appears not
your scenario.

However, the IJW(It Just Works) is a managed C++-specific feature which
enables flat APIs and COM APIs to be used directly, it is for use of  
wrapping complex unmanaged flat APIs(or wrapping unmanaged flat APIs which
changing frequently during developing) and calling IDL based COM
components, so I think it is the proper approach to your case.

Please refer to the following detail document:

An Overview of Managed/Unmanaged Code Interoperability
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/ht
ml/manunmancode.asp

Thanks for your understanding!

Best regards,

Gary Chang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Bruce Schechter - 02 Jun 2004 09:29 GMT
Gary,

I would like to try the COM interop approach described in this article.
But I need help with one more element of that approach...  Can you point me
to an article to explain how I can take my set of (unmanaged, legacy) C++
classes and create a COM object from them?  (All my experience so far with
Microsoft tools has been with the .NET framework.  I have no comprehension
of what kind of effort is required to create a COM object.)

Thanks, Bruce

> Hi Bruce,
>
> Additionally, you can make your C++ business logic code into COM
> components, them use COM Interop to access them from C# applications:
>
> COM Interop Tutorials

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/
> vcoricominteroptutorial.asp
>
> COM Interop Part 1 Sample

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cssample/ht
> ml/vcsamCOMInterOpSample.asp
>
> Advanced COM Interop

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
> l/cpconadvancedcominterop.asp
>
[quoted text clipped - 8 lines]
> This posting is provided "AS IS" with no warranties, and confers no rights.
> --------------------
Nat Wells - 03 Jun 2004 03:44 GMT
The easiest solution might be to write the UI in managed c++ (instead of
c#).
Then you can combine everything into a single project. Compile the legacy
c++ code using /clr (no source changes), and "it just works".
nwells

> Background:  I have nearly two years of experience with C# and the .NET
> framework, as well as a fair amount of C experience from many years ago, but
[quoted text clipped - 31 lines]
>
> Bruce
Bruce Schechter - 08 Jun 2004 01:30 GMT
Nat,

Thanks for the message.  That is a very interesting prospect.

I will give that a try.

cheers, Bruce

> The easiest solution might be to write the UI in managed c++ (instead of
> c#).
[quoted text clipped - 43 lines]
> >
> > Bruce

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.