I am very new to C++ programming...
I was assigned an assignment to do a SkipList in C++ that will do some functionalities..
Apart from these functionalities, there was also the following included:
Export the Skip List to a .DOT file that can be rendered in GraphViz
Any ideas how I can do this?
Also, I need to export the DLL so that I can use it in either Java or else in C#..
any help is greatly appreciated
Thanks
If you're looking to use C++ code in Java, check out JNI. It requires you to modify your C++ code with JNI wrappers (see the Wikipedia link for examples).
C#, on the other hand, can call DLL functions which are exported normally, i.e. with extern "C" __declspec(dllexport), via PInvoke.
The Java has got a JNI technology
In fact it's c/c++(with extern "c" calls) library with some specific types and rules
And yes you can use your dll in Java with some jni bridge
Related
In VS C/C++ you could use extern "C" __declspec(dllexport) -function declaration-.
How do I accomplish this in a C# dll? Is there C# code equivalent to the above code?
Edit: More info
I am trying to create an add in for Notepad++ and I want to use C#, but the common way I've seen so far is to use legacy C++ code with the above call to export a few of the functions that Notepad++ expects to import and call. There is an example app using C#, but this still requires a loader DLL, which I assume from the comments/answers below is the only way for C#.
Unmanaged Exports =>
https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports
DLLExport => https://github.com/3F/DllExport
How does it work?
Create a new classlibrary or proceed with an existing one.
Then add the UnmanagedExports Nuget package.
This is pretty much all setup that is required.
Now you can write any kind of static method, decorate it with [DllExport] and use it from native code.
It works just like DllImport, so you can customize the marshalling of parameters/result with MarshalAsAttribute.
During compilation, my task will modify the IL to add the required exports...
I've seen people do this before, but it required ildasm, adding the MSIL .export directive, and then reassembling. A program named dll_tool can do these steps for you.
If you want to build a mixed-mode DLL with both native and managed exports, you should be using C++/CLI, which is specially designed for this purpose.
Yes, it is possible to export functions from a C# dll in much the same way that C++ does it! You need a little help from an add-in Unmanaged Exports (DllExport for .Net) that facilitates this process, or from a similar method such as Exporting functions in C#/VB.NET to native code.
Please see Code to Export C# DLL to Metatrader Build 600+ for a working example using Robert Giesecke's C# Project Template for Unmanaged Exports to export a C# dll to a legacy application (Metatrader) that has a great deal of similarity to C++.
Additionally, you might find Native and .NET Interopability interesting though it is mostly geared toward accessing native code from within .NET rather than the other way around.
No, you cannot do that in the same sense as you do in C and C++.
But you can create COM API to achieve that which then you can use in C and C++ code.
See these articles
C# Classes as COM Objects
Calling Managed .NET C# COM Objects from Unmanaged C++ Code
COM Interop Part 1: C# Client Tutorial
I have a program written in C++ with a main function that calls a bunch of other C++ classes/functions.
I am new to C++ (been a python programmer), so I'm wondering - What are the steps I need to follow to be able to export this as a DLL that is importable from a C# program? Any suggestions?
If you want your C++ classes to be usable in a C# application you will need to use COM or target the CLR in your C++ program (i.e., use C++/CLI).
If you simply have some functions in the C++ DLL that you want to call from C# that take POD type arguments then declare each function as extern "C" to avoid name mangling and use the DLLImport attribute to import the function. PInvoke.net is a great resource here.
If you are only exporting plain functions, not classes, you can p/invoke them. if ypu really need the classes, you have to write your project as a C++/CLI and reference it in your c# project like any other .NET assembly.
A brokerage firm has an API built around C++ provided as a library and header files for users to consume(orders, prices, news..etc), long story short, I only have practical knowledge of C# and would like to make use of some wrapping technologies to make use of the .lib and .h files provided by converting them into managed DLLs. Is there a way of doing this without getting involved in much C++ coding/wrapping?
You can use C++/CLI. Try to starts from here.
Basically I would create a C++/CLI wrapper in order to expose a wise set of functionality to c#.
Anyway, it is not an easy task.
An alternative is to write in pure c++ some extern "C" entry point to the library and access that api using P/Invoke.
I have zero experience in C++, but have a few years C# experience.
Are there an examples out there showing how I can create a method in a C++ program which is then called from a C# program using the DLL?
Cheers
Using p/invoke, you can call C++ code from C#.
Read this: Calling Win32 DLLs in C# with P/Invoke
Another small yet good article : Using P/Invoke to Access Win32 APIs
--
EDIT:
This aritcle explains how to create a DLL library in C and then use it with C#
You can also try compiling the C++ code in Visual Studio to VC++, which is plain old .NET. Avoid the p/invoke, if the code is compatible.
Create some function in C++ with "extern "C"" to avoid the c++ name mangling and then use PInvoke as suggested by Nawaz is the better way.
Is it possible to call C++ code, possibly compiled as a code library file (.dll), from within a .NET language such as C#?
Specifically, C++ code such as the RakNet networking library.
One easy way to call into C++ is to create a wrapper assembly in C++/CLI. In C++/CLI you can call into unmanaged code as if you were writing native code, but you can call into C++/CLI code from C# as if it were written in C#. The language was basically designed with interop into existing libraries as its "killer app".
For example - compile this with the /clr switch
#include "NativeType.h"
public ref class ManagedType
{
NativeType* NativePtr;
public:
ManagedType() : NativePtr(new NativeType()) {}
~ManagedType() { delete NativePtr; }
void ManagedMethod()
{ NativePtr->NativeMethod(); }
};
Then in C#, add a reference to your ManagedType assembly, and use it like so:
ManagedType mt = new ManagedType();
mt.ManagedMethod();
Check out this blog post for a more explained example.
P/Invoke is a nice technology, and it works fairly well, except for issues in loading the target DLL file. We've found that the best way to do things is to create a static library of native functions and link that into a Managed C++ (or C++/CLI) project that depends upon it.
I'm not familiar with the library you mentioned, but in general there are a couple ways to do so:
P/Invoke to exported library functions
Adding a reference to the COM type library (in case you're dealing with COM objects).
Yes, it is called P/Invoke.
Here's a great resource site for using it with the Win32 API:
http://www.pinvoke.net/
Sure is. This article is a good example of something you can do to get started on this.
We do this from C# on our Windows Mobile devices using P/Invoke.
The technology used to do this is called P/Invoke; you can search for articles on the subject. Note that it is for calling C from C#, not C++ so much. So you'll need to wrap your C++ code in a C wrapper that your DLL exports.
Have you considered Apache Thrift?
http://thrift.apache.org/
It seems like a very very neat solution.