Link to home
Create AccountLog in
Avatar of johneanderson
johneandersonFlag for United States of America

asked on

Configuring a C# windows project with managed/unmanaged C++ code

I am trying to create a windows form application in C# that calls managed C++, and subsequently unmanaged C++.  I created three projects within a single solution:

1) Visual C++, empty project.  Changed it to a "Dynamic Library (.dll)" type configuration and added .h and .cpp modules.  This builds correctly, although no .dll file is produced.  (Maybe that's my problem)
2) Managed C++ (CLR) project.  Added .cs files and made calls into unmanged project code (above).  This builds correctly.
3) C# windows form project.  Added reference to unmanaged C++ project.  Added code to create a managed C++ object.

This entire solution builds fine.  When I try to run, I get a FileNotFoundException in Program.cs when executing the line:

    Application.Run(new Form1());

If I remove the construction of the managed C++ object from the Form1 constructor, it runs OK.  In the windows C# project, I tried added a reference to the unmanaged C++ project.  I get the error 'A reference to unmanagedIFC could not be added.  I tried copying the .obj files from the unmanaged project to the application's execution directory.  Still get the FileNotFound exception.

How do you do this?  How do I get my unmanged project to produce a plain old dll?  I can find no help anywhere in the MSDN help or online.  MS seems to think everyone is writing web apps and that C++ is dead.  Well, I've got to use a driver's DLL in my project, and yes, I've tried the platform invoke route.  Platform invoke works, but the performance is terrible when trying to marshal large amounts of data across this interface.  I am trying to speed things up by writing my own managed/unmanaged C++ wrapper around the driver DLL.  Am I crazy?

Any help or hints would be much appreciated.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

May I ask what your bigger plans are?  Why do you have this confusing array of projects and mixed mode processing?  Can we stream-line this process a bit?

Bob
The dll is probably not in the directory you expect it to be in - have a look in the other directories in the complete solution.
Avatar of johneanderson

ASKER

Bob:

We are developing an application that acquires data from a custom instrument.  The vendor supplies a driver and a DLL to access the device and acquire data.  The DLL is unmanaged C code.  Originally, I was using PlatformInvoke (see question: https://www.experts-exchange.com/questions/23212402/Performance-of-platform-invoke.html ).  But, performance became an issue.  

Yes, I would love to keep everything in C# but how do I build a bridge to the driver DLL?  If you have a simpler answer that doesn't involve managed/unmanged C++ I would love to hear about it.  Maybe I'm doing something wrong in platform invoke (see question referenced above).
I was only trying to say that you need a managed CLR DLL project in VC++.NET that you can reference in your C# project.

Bob
Andy:

Can't find the DLL for my unmanaged code anywhere.  The project is named 'UnmanagedIFC', so I would expect to see a UnmanagedIFC.dll somewhere.  Like I said, it doesn't appear my unmanaged project is producing a DLL.  In the 'Debug' folder under the unmanaged project, I see a .obj corresponding to each of my three C++ source files.  I also see a mt.dep, UnmangedIFC.dll.intermediate.manifect, vc80.idb and vc80.pdb files.  But no DLL.

To simplify my question: How do I build an old-fashioned unmanged C++ library that results in a .dll file?
Bob:

I am doing precisely what you say.  I have a C# windows form application that references a managed C++.NET project, called 'ManagedIFC'.  It's getting the UnmanagedIFC project (see above) that is the problem.  Can I call C code in an external DLL from managed C++.NET, that doesn't involve using platform invoke?
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
OK.  I found my UnmangedIFC.dll file.  It was under a debug folder in my solution directory.  I put solutions in a completely different folder.  I will fix this and see if I can add a reference now.  Thanks.
Thanks for you help.