As of right now I am building authentication library for let's say programing site(It needs to be included in the program).

The problem is that there several languages are used and I want to make authentication library that can be referenced in some of bigger languages like c#,java and python(all in one dll).

I heard that something like this can be done with c++ so I made library in c++,but I don't know how to refrence it in other languages.

  • 2
    I don't know how to refrence it in other languages. -- You would do it using that language's Foreign Function Interface (FFI) capabilities. For example, here. Many languages support a C FFI. – Robert Harvey May 3 at 18:58

The difficulty you will face in using your C++ DLL is that:

  • the symbols names will be mangled and are hence not easily usable from another language.
  • the C++ object model will impose constraints on usability of objects from other languages. For example, the return value of a C++ function returning a non trivially copiable object might not be usable in most language, since the other language doesn't know how to destruct the object when it's not longer used.
  • most languages will not be easily able to access class members, since the layout of a C++ object is implementation dependent and the C++ memory model does give very few guarantees on it.

However, you can overcome many of this limitations by defining the DLL interface through extern "C" functions, and limiting exchanges to the C compatible datatypes:

  • For Java, you can load the native DLL and use JNI to call the exposed functions. Alternatively you may consider JNR/FFI or other solutions proposed here.
  • For Python, you may consider using ctypes
  • For C# (and other managed languages), you could consider Robert's link.

Given that you specify a DLL (as opposed to, for example, a shared object library), you're apparently interested solely in Windows.

In this case, you basically have a couple of choices. The first (and usually simplest) is a least common denominator approach: use roughly the same interface Windows itself does: minimal mangling of names, and Microsoft's stdcall calling convention.

The other obvious choice is to create a COM object. For a fair number of languages, a COM object makes reuse quite a bit easier, because (among other things) the COM object can be almost entirely self-describing. That is, it can contain the information needed for the other language to make use of the COM object.

COM has another advantage if you want to support using it from object oriented languages: COM allows you to export objects, not just individual functions.

COM is something of a two-edged sword though: on one hand, exporting a COM object makes life much easier for some languages that support COM really well. On the other hand, it can make life much more difficult for languages that don't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy