Using unmanaged C++ from C# with /clr

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
LVL 1
threadyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SameerJagdaleCommented:
my experience was not good with unmanaged c++ and managed c++ both with c#
0
threadyAuthor Commented:
Give me some details!  :-)  I'm all ears (well eyes), please...
0
SameerJagdaleCommented:
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

mac-willCommented:
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
threadyAuthor Commented:
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
mac-willCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
threadyAuthor Commented:
This answers my deeper question - many thanks Mac-will.  
0
threadyAuthor Commented:
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
mac-willCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.