?
Solved

Using unmanaged C++ from C#  with /clr

Posted on 2008-11-04
9
Medium Priority
?
662 Views
Last Modified: 2013-12-17
Hi Experts,

Just wondering if anyone has used the /clr option to convert unmanaged C++ to c#-ready code?  Any problems?  Any suggestions?  I've got a big project written in MFC and would like to use lots of its libraries in a new Winforms project (Visual Studio 2008)

Thank you!
Mike
0
Comment
Question by:thready
  • 4
  • 3
  • 2
9 Comments
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22883366
my experience was not good with unmanaged c++ and managed c++ both with c#
0
 
LVL 1

Author Comment

by:thready
ID: 22884898
Give me some details!  :-)  I'm all ears (well eyes), please...
0
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22885821
i have seen lot many memory leaks (attempted to write to a memory location.., corrupted memory... write-protected location, etc.) other than that..i was almost lost on multidimensional arrays in c++.. this is just to name a few and more complicated.
HTH
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 8

Expert Comment

by:mac-will
ID: 22886380
I don't agree.  If proper care is taken unmannaged code can be very efficient to use in a managed application.  (Interop)
What I have generally done is taken a little bit of time to define how my data will move from unmannaged memory to managed memory. (Marshaling)  Once this is well defined using native C++ libraries becomes relatively trivial.

As for using the /clr option to convert unmananaged code this does not make sense.  Code in C++ is either written in managed or unmannaged form and generally is not well converted.

This sounds like a prime candidate for Interop.

Compile a native C++ (unmanaged) dll and use it with well defined Marshalling from your C# winforms app.

Good Luck!

MAC
0
 
LVL 1

Author Comment

by:thready
ID: 22886509
Mac-will - this sounds pretty scary to me - marshaling....  There are a lot of libraries...  I'm tempted to tell the customer that this will take some time - we only have 6 months to get this done.  I'm probably better off sticking to MFC, rather than converting the project into .NET.  Although this is something we want to build on, MFC should be around for a long time...  Your thoughts on this?
0
 
LVL 8

Accepted Solution

by:
mac-will earned 2000 total points
ID: 22886621
MFC will definitely be around for a while.
If you have MFC expertise and an MFC base I would recommend staying with MFC (I actually prefer MFC to .NET)
As a matter of fact a new release of MFC has just added many new useful features:
http://msdn.microsoft.com/en-ca/visualc/bb892882.aspx

That said Interop is not that scary.  If you are sure you are going to migrate to .NET than I would go for it.

Marshalling is just the process of moving unmannaged memory data to managed memory and vise versa.
This can usually be left to the compiler.

Like I said take time to define how data will move to and from you library and this will save time in the end.

Here is a quick example from some of my code:

C++ DLL signature:
EXPORT void GetSignature(unsigned char * theRetSignature, int & theSampleRate, short * theDataBuffer, int & theNumSamples, int & theSilentLevel);

C# import siganture:

[DllImport("Wrapper.dll")]
internal static extern void GetSignature([Out] byte[] theRetSignature, ref int theSampleRate, [In] short[] theDataBuffer, ref int theNumSamples, ref int theSilentLevel);

With a setup like this automatic Marshalling is used and I didn't even have to worry about it.

Good Luck

MAC
0
 
LVL 1

Author Closing Comment

by:thready
ID: 31513397
This answers my deeper question - many thanks Mac-will.  
0
 
LVL 1

Author Comment

by:thready
ID: 22890037
Hi again Mac-will- the new MFC library you were talking about looks fantastic - and it is available to users of Visual Studio 2008 - do you know where to download it?
0
 
LVL 8

Expert Comment

by:mac-will
ID: 22894934
This version of MFC should be included with VS 2008.  VS Express does not include MFC but all the other versions should have it.
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question