I'm going to add a DLL which is made inside C# .net to a C++ project.
According to instructions inside some references like this, I should add its reference as below:
Properties --> Common Properties --> Framework and References -->Add new Reference
In windows which titled Add Reference I should add DLL to my C++ project.
However this windows does not show any DLL files and have no options to browse DLL files.
Does anyone know how can I bring my custom DLL into this window?
P.S. Whole this story is about the requirement that I want to connect SQL Server inside a C++ project and I don't want to use C++ libraries. Instead I want to implement SQL related stuff inside a C# class library project and import it inside C++ project using C# project DLL. If you have any ideas about this I would be grateful if you share your idea or solution.
You cannot call managed (i.e. c# methods) from a native (i.e. c++) project without jumping thru some hoops. You have a few options:
Make your project a c++/cli project, this will allow calling managed code directly. This would probably be the easiest, it should not be much more complex than turning on support for "common language runtime support" in the advanced project properties.
Use a c++/cli adapter project between your native and managed code.
Use some thirdparty software to generate native exported functions by some black magic.
Rewrite your application in c#
For a c++/cli and c# projects you should have a "References" entry in your project that you can use to add assemblies to. I'm not sure if nuget works however, so you might need to managed the references manually.
Related
I would like to share common code between a Xamarin.Forms project (C#) and a Qt project (C++).
Have you got a solution?
I try with a Visual C# Class Library which builds a DLL file. I succeeded to reference and use it in the Xamarin.Forms project, but I failed to use it in Qt. I supposed this is because the DLL is compiled from C# code.
Thanks.
If you can put the shared code in your C# dll, you should be able to use it from Xamarin (depending on what libraries it uses) and also call it from your C++ code.
As far as I know a C# project can be added to Unity3d in 2 forms. As DLL or uncompiled project.
What are the pros and cons of both? Will they work cross platform (Android/iOS/WinPhone)?
To compile as DLL - do I just use MonoDevelop or Visual Studio to new class library project and select .NET 3.5 and compile?
How do you add a uncompiled project to Unity3d?
Will they work Cross platform(Android/iOS/WinPhone)?
Yes. You can create a library project in Xamarin Studio/MonoDevelop/or Visual Studio. If you do not include platform-specific functions, it will be then cross platform.
To compile as DLL - Do I just use MonoDevelop or Visual Studio to new class library project and select .NET 3.5 and compile?
See this tutorial.
Well Well Well too many question in a single post. Try to answer one by one:
Unity Offical Docs provide much help:
Question
As far as I know a C# project can be added to Unity3d in 2 forms. As DLL or uncompiled project.
To compile as DLL - do I just use MonoDevelop or Visual Studio to new class library project and select .NET 3.5 and compile?
Usually, scripts are kept in a project as source files and compiled by
Unity whenever the source changes. However, it is also possible to
compile a script to a dynamically linked library (DLL) using an
external compiler. The resulting DLL can then be added to the project
and the classes it contains can be attached to objects just like
normal scripts.
It is generally much easier to work with scripts than DLLs in Unity.
However, you may have access to third party Mono code which is
supplied in the form of a DLL. When developing your own code, you may
be able to use compilers not supported by Unity (F#, for example) by
compiling the code to a DLL and adding it to your Unity project. Also,
you may want to supply Unity code without the source (for an Asset
Store product, say) and a DLL is an easy way to do this.
More
What are the pros and cons
About dll:
Pros
You can build DLLs separately.
It could be faster to re-build one DLL
Cons
Calling code from DLL is slower
It would be slower to re-build hole project with all DLLs
Function names are visible. It is easier to reverse code that uses
dynamic DLLs
You can find more on here, here and specially on Google, our best friend:)
How do you add a uncompiled project to Unity3d?
Ans: What do you mean by uncompiled project? You mean scripts? then it is usually Copy\Paste inside asset folder simply.
When you open your Unity project in an IDE (for example Visual Studio), it is a solution with two projects, one for game scripts and one for editor scripts. You can add more projects to the solution through your IDE of choice. In order for this to work, you need to set projects target framework to one that is compatible with Unity (which uses a subset of Mono), for example Unity 3.5 .net full Base Class Libraries. Unfortunately .NET PCL is not supported.
You can reference your (or third party) library in any of the projects as usual, just remember it also has to be compatible with Unity.
Wether your code will work on all the platforms actually depends on what you use. On platform specific limitations refer to http://docs.unity3d.com/Manual/PlatformSpecific.html.
I have a library called foo, which is written in C++/CX. I chose a Windows Runtime Component because I want it to be projected into C#, C++ and JavaScript. Also, I want to be able to distribute the library, and I don't want to require/allow the consumer to load my project, along with the source files, in the same solution as their project.
The instructions on MSDN only demonstrate how to include the Windows Runtime Component project in a solution with the consuming C# project. I know there is a way to only distribute the binary, but I don't know how.
This question has been asked a thousand times, but the answer always has the two projects in the same solution, is incomplete or a workaround.
I'm using Visual Studio 2013.4 on Windows 8.1.
One way to do it is to create a VSIX package of your component. See Walkthrough: Creating an SDK using C++ that shows exactly how to do this with a WinRT component consumed by a C# project.
I researched and found the answer... It is not documented well, it is not intuitive, but it's worth the trouble when you see how well a Windows Runtime Component works.
Compiling the C++/CX library:
Make sure you compile the library for all permutations of Debug and Release, in Win32, ARM, x86 and x64.
Instructions for consuming in C#:
Right-click on References in the Solution Explorer, and add a reference to the Microsoft Visual C++ Runtime Package v12.0. Then add a reference to the binary you created with the appropriate configuration for your project (i.e. Debug/ARM). This step is tricky, because the file filter prompts you for the .dll, but you need to set the filter to *.* and select the .winmd file. Then you unload the project, find the <Reference> tag for the library you just added. <Reference> will have a <HintPath> tag below it, and under <Reference> you will also need to add <IsWinMDFile>true</IsWinMDFile> and an <Implementation> tag pair loaded with the name of the .dll that was sitting in the same folder as the .winmd file.
For more detailed instructions and information, I highly recommend visiting Mike Taulty's Blog
I have a rather large legacy nmake (Win32) project that creates a static library from native C++ code. I need to use this library in a C#/.Net application. In the past after much effort I had been successful at wrapping the static library in a managed C++ library, which I am then able to reference in a C#/.Net application. However, after receiving updates from the developers of the nmake project, and having gone through an many upgrades on my own build machine in the meantime, it is no longer working.
I am however able to import the cpp and header files of the nmake project and build it to a Win32 static library in VS 2010, by setting all of the preprocessor constants in the build properties. I set the build configuration type to DLL, and then try to add a reference to the Win32 output in my C#/.Net application hoping to use P/Invoke down the road, and it fails with a message "A reference to MyLibrary could not be added."
Is there a way to build the Win32 library so that it can be referenced by the C#/.Net project and so that I can use P/Invoke?
Is there a way to build the Win32 library so that it can be referenced by the C#/.Net project and so that I can use P/Invoke?
If you want to directly reference the library, you'll need to build a C++/CLI project using your library, and make managed wrappers.
If you want to use P/Invoke (which is a separate concept), you can make exports with a C API, and call those directly via P/Invoke.
The two approaches are both valid, but completely different in terms of implementation (C++/CLI vs. C API wrappers) on the native side, as well as used differently (directly referenced for C++/CLI vs. P/Invoke via [DllImport]).
You can use SWIG to generate wrappers for your code. SWIG is a very powerful tool and worth taking the time learn. It creates wrappers for a number of languages including Python, C#, and Java so if you get it working with one language it is fairly easy to use in other languages as well. It will generate all the wrapper code for you, although you will probably need to do some work with type. You use swig to create a special DLL that SWIG generates code for and then used supplied C# code to access the DLL without needing to deal with managed C++ assemblies which can be a nightmare to deal with.
http://www.swig.org/Doc2.0/SWIGDocumentation.html
Edit: my explanation may not be that clear and the docs are pretty overwhelming, take a look at the "What is swig?" section here to get started:
http://www.swig.org/Doc2.0/SWIGDocumentation.html#Introduction_nn2
I have the source code of a C# program. I want to create a DLL out of it which I want to use in C++.
Is it possible to create a native DLL in Visual Studio 2008 which can be used in C++?
native <-> .Net interop is one of my pet disciplines, which is why I needed this as straightforward and reliable as possible.
The result was that I made me an MSBuild task which I just need to drag into a project to allow me to export static methods from pretty much any .Net language. And since the whole marshalling infrastructure works for exports as well, you can do pretty much anything with it that you want (like passing native/managed objects as IUnknown).
The resulting assembly will look to the consuming process like a real DLL, which means you can't have it to scale up to 64bit automatically anymore.
However, since you do have native bits in your application, you already have this issue anyways. ;-)
When you do not specifiy the CPU target in your C# project, my task will emit a warning that it created a folder for all targets (x86,x64 and Itanium), and there you'll have *.dll and *.pdb for each platform.
If you want the program to be native, and not managed, you'll need to port it to C++, instead of using C#.
That being said, you can compile it in C# into a library, and use it from C++ by using C++/CLI. This just requires that you compile the files that use the C# library with the /clr flag. This provides C++ access to the .NET framework, and lets you use libraries made in C# directly from C++.
Alternatively, you can use .NET's COM interop to expose the C# class(es) as COM objects, and then use those from native C++.
It is possible in Visual Studio 2008, but you're not going to be able to write it using C#.
To create a native DLL, you'll have to write your code using one of the unmanaged C++ project types.
You can expose the DLL to COM. Have a look here for some examples.
yes you can.
you need to create second project.
the project must be unmanaged (like "visual c++"->class library).
the name of this procedure is "calling from unmanaged code to managed code".
good to read unmanaged to managed (codeproject)
you must be aware, that any computer that using your dll must have preinstalled DotNet and Visual C++ Redistributable Package