How do I call windows form c# functions from c++ project? - c#

I was working on a windows form project lately and I made some functions in that project that I want to use from another c++ project of mine. My windows form project is creating a .dll already, so I was wondering if I could call my functions thanks to it somehow.
I tried creating a CLR empty project as I know those are supposed to link C++ and C# possible but when I try to use the namespace of my C# classes in the CLR project, visual doesn't find it and tells me the name I'm trying to use must be a namespace, am I missing something?
Here are some images of what I've done:
Encrypter.cs (the C# class I'm trying to use)
The C++ file in the CLR project trying to use it (I tried several ways of getting my class, I left them all, none worked)
Both projects are next to each other in the same solution.
To sum up my questions:
Do CLR projects work both ways with C++ and C#? I find very few mentions of C++ projects using C# functions, but much more of the other way around.
Is it possible to call windows from C# functions from a C++ or CLR project, or do I have to make another C# project that creates a .dll ?
If I'm going the right way with what I've already done, how do I use my C# class in my CLR project?
EDIT: For anyone maybe coming through here, I found the solution. Turns out I did miss something in my setup, when selecting the CLR project and going in the "Project" menu on top of visual studio, you can then go to "Add Reference" and select your C# dll. Once I did this, the CLR project saw all my C# classes and functions.

Related

Build and deliver a C/C++ DLL into a C# app using Visual Studio 2013

I am a Visual Studio 2013 Newbie who is trying to figure out how to build a C DLL in Visual Studio and then invoke the functions exported by the C lib from within a C# app. I've got everything building, but I cannot figure out the proper way of getting the DLL included with the app build so that it will actually load properly when using [DLLImport()]. I currently get a System.DllNotFoundException when attempting to invoke a function from the library from within my C# class.
I have setup a VS 2013 solution which contains the following projects:
HelloWorldLib.Shared :
Shared project that contains the .cpp and .h files for the DLL
HelloWorldLib.Windows :
Win 8.1 C++ project that references the shared project and builds the DLL for Win 8.1
MyApp-Win.Windows :
C# project for Win 8.1 that wants to make use of the HelloWorldLib.Windows.dll produced by the HelloWorldLib.windows build
I have been looking over many SO questions and answers. For instance, I think my question is similar to this one, which doesn't appear to have been answered in any useful way:
Interop, Native DLL and Visual Studio build process
Some of the other answers suggested adding a file reference to the DLL, but how would I do that and keep the proper Debug/Release versions? Given that all of these projects are being built within the same solution, and there are build dependencies between them, it feels like there should be a straightforward way to set this up.
BTW, I am using the shared project setups, as my ultimate goal is to figure out how to get this all working with Xamarin to target iOS, Win, Win Phone, Mac and Android. So that we can use a common C/C++ layer for our high performance requirement code, and the reference it from the UI layers on the various platforms.
This is a pretty straight-forward problem, easy to identify if you look where the built files end up. The C++ build system has a different strategy than the managed build system. It by default writes the build output to the solution directory's Debug directory. Your C# project however builds to its bin\Debug directory and requires all dependent DLLs to be present there.
So, by default, your C# program can never find the DLL, it is in the wrong directory and Windows doesn't look there. Nor is MSBuild smart enough to discover this by itself, like it can for managed DLLs, it cannot see that you program depends on the C++ DLL.
Two basic ways to work around this:
Change the C++ project's Output Directory setting
Write a post-build event for your C# project that uses xcopy.exe to copy the DLL.
Unfortunately things aren't that easy, at least not yet. I was going to have a meeting with Microsoft C++ team but that got postponed, similar functionality you are after was one of the things on my wishlist (building managed C++/CLI wrappers for Xamarin.iOS and Android).
Currently, if you can, the easiest option is to wrap C++ code inside a DLL that exposes C interface. Then build it with XCode and Android NDK. If you can't then the next option is to create Objective-C binding project that wraps the C++ code inside Objective C classes for iOS. For Android either create Java wrapper or use SWIG. Both of these options are cumbersome which is why C API should be the first choice you investigate.
I know it's not the answer you were hoping for but reusing C++ code is way more complicated than reusing C# or even C.

C# Code in C++ Project

I've read many solutions to the issue of using C# code in a C++ project, including this one: How to use c# code in C++ project
I've also read this, and one answerer says Compile your C++ code with the /clr flag. With that, you can call into any .NET code with relative ease.
Does this mean that I can use C++ & C# code together, within the same project if I append the /clr flag?
I'm a little confused by this, and it'll just waste time if I went the long route of converting the C# project that I'm trying to use parts of to C++, if doing so isn't actually needed.
Basically, I answered my own question on a different topic a couple of days ago without realising that the actual code of the sample project is C#:
Obtaining Current Network Rate
Can anybody give me a firm clarification to whether I understand this correctly (that I can use C# code in a C++ project with no issues, apart from the /clr switch being required)?
I'm not sure if SO is the right place to ask a question like this, so please tell me if it isn't instead of downvoting with no explanation.
Thanks.
Edit
Forgot to mention that this is a C++/CLI gameserver application DLL on Windows. If it matters, it is used on only Windows Server 2008 R2 and Windows 7.
If you use C++/CLI, then you can add references to other .net assemblies, but you do not mix C++ and C# code in the same project. What you would do is to create a new project (or add an existing one) in your solution using C#, and then add a reference to it in the C++/CLI project.
The drawback is that you need to marshal between C++ and .Net types (std::string vs System::String^), and you also need to learn the additional syntax used by C++/CLI (^, gcnew, etc.).
Further reading: Pure C++: Hello, C++/CLI
It means, that with the /clr switch, you can access assemblies (DLL or EXE) written for the .Net framework. Because such assemblies can be created with any .net language (including C#), you can use "code" written in C# from within C++, but it doesn't mean that you would be able to write C# in C++ project. Unfortunately - you can't.
You need to make a C++/CLI project in visual studio.
C++/CLI is a bit different to C++, because there is the managed stack, so memory is managed and you can avoid memory leaks (garbage collection).
Simply create a C++/CLI project and click on propietys and add a reference to your C# project, it should work :D
http://www.codeproject.com/KB/cs/ManagedCOM.aspx

interacting between a C# project and C++ project in same solution

I have a windows forms app written in C++/cli. I want to extend this app with some new forms and I'd like to create them in C# in a separate project.
Is it possible to simply add a C# project to a solution that has the C++ project and the two will interact? By interaction, I mean that, say, a button clicked on a form written in the c# project will be able to call methods in the c++ project. Asked perhaps in a different way, can an object in the C# project reference an object in the c++ project? if so, do you know of an example code to get me started?
Thanks.
Yes. A C++/CLI application will be able to interface with a C# application, in one of two ways:
If you are using CLI extensions (which from your post it sounds like it), you will be able to write code using the new object references:
Managed objects: System::String^ myString (in C++) is the same as string myString in C#
Managed refs: System::String% myString is equivalent to ref string myString.
If you want to use C++ native types, then you will have to use P/Invoke, but that's an entirely different category. For what you want to do, just add the C++ project as a reference to your C# project, write a publicly-visible class in C++ using managed types, and then compile. Your project should then be visible to your C# class in whatever namespace you chose for the C++ class.
EDIT: oh, and if you need to allocate managed objects through C++, you will need to use gcnew instead of new.
This can be done by compiling a dll with the C++ project, then having the C# app load that dll and then it will be able to call its exported functions. This method allows your C++ code to be unmanaged code.
As far as finding a sample that is already set up, I can only find a Visual Studio 2008 project:Microsoft All-In-One Code Framework
For visual studio 2010, here's how to do the C++ side: How to make a dll with C++
Using Explicit PInvoke in C++

Adding dlls in VS 2008

I would like to add some external .dll libraries e.g. glut32.dll (but it's only example) in Visual Studio 2008, using C#.
Can you please tell me what should I do step by step?
I am a little bit confused cause I found a lot of solutions to add dll files, but they significantly differ.
Some of them add dll's only using code, some using properties in vs, add references and in other tutorials there is about registering dlls in system.
But how to put it all together?
There are different kinds of DLLs, you'll need to treat them differently when you use them in a C# project. The 3 main kinds are:
DLLs that contain unmanaged code and were designed to be used by a program written in unmanaged code. You cannot use such a DLL directly, there is no way to add a reference to them in a C# project. You must use P/Invoke, the [DllImport] attribute is required to declare the exported functions in the DLL. Glut32.dll is such a DLL. A very basic test you can use to see if you've got such a DLL is to run Dumpbin.exe /exports on that DLL. It lists the names of the functions that are exported.
DLLs that implement an in-process COM server. They are written in unmanaged code as well. .NET has very good support for using such servers, as long as you have a type library for them. The type library is usually embedded in the DLL, Visual Studio expects to find it when you add a project reference, either through the COM tab or the Browse tab. A very basic test is Dumpbin.exe /exports again, an in-process COM server has 4 exported functions. The DllGetClassObject function is the important one. You can view the type library embedded in the DLL with OleView.exe, File + View Typelibrary. A good example is c:\windows\system32\shell32.dll
DLLs that were created by a managed compiler. They don't contain machine code like the other types, they contain IL code and metadata. It is the native kind of DLL for managed code, you simply use Project + Add Reference to add a reference, the compiler automatically knows the types that are available in the DLL.
The first kind is the one you'll encounter a lot for DLLs in the wild. There's a lot of code written for Windows in an unmanaged language. It isn't a kind of DLL that's particularly easy to use from managed code. Glut32.dll for example has a lot of exported functions, writing a P/Invoke declaration for every single one of them is painful.
Tools you can use to help with this are SWIG and PInvoke Interop Assistant. The former is required when the DLL was written in the C++ language. C++ classes are not directly usable from a C# program, they need a wrapper written in the C++/CLI language. The latter tool is useful for DLLs written in C, including the Windows API.
Beware that those tools don't usually give you a clean and guaranteed-to-work interop solution. Declarations in unmanaged code are ambiguous, you'll need to know the exact semantics of the arguments of an unmanaged function to pick the right one. Getting the wrong one can be hard to diagnose, the best place to get help is a forum or Q+A site. Like stackoverflow.com
You can use the add reference dialog under the project menu to do that. Just go to the COM tab and add your library. Now you can verify that the classes appear in teh object browser
Right click on project in solution explorer and select add reference and then browse and select the dll.
If you wish to reference the dlls you
Right Click on References, and select add reference.
Or have a look at
How to: Add and Remove References in Visual Studio (C#)
Adding a Reference to a C# or Visual Basic .NET Project
In Solution Explorer, right-click the project node and click Add Reference.
In the Add Reference dialog box, select the tab indicating the type of component you want to reference.
Select the components you want to reference, and then click OK. Tip.

VB .frm files, can they be imported into a C# project?

I am supposed to be updating a piece of software for someone. Originally written in VB 6 I want to import it into a .net c# project. My question is, I don't want to have to lay out all the forms again and I am wondering if, or how, I could import them into c# project. They originally come with the .frm extension and c# forms are .cs. I tried 'add existing item' to the project but obviously that didn't work, and I tried a couple of other things. Is there a way of importing them that would work? I just need the layout, not worried about the underlying code or anything as this will all be re-written.
Thanks, R.
You can utilize the VB upgrade wizard, but that will only get you to VB.NET (potentially as there as some things that cannot be upgraded). Or you can compile your VB6 app to a COM dll... then call the COM dll via interop.
As already mentioned, you can migrate to VB.NET first. You can copy and paste controls from one project's form to another's.

Categories

Resources