Wrapping C++ class API for C consumption -


I have a set of related C ++ sections that should be wrapped and exported so that it's easy to do with DLL Consumed by C / FFI libraries I am looking for some "best practices" to do this, for example, how to create and handle free objects, how to base classes, alternative solutions, etc. ... < / P>

Some of the basic guideline methods so far Not have to return to an extra zero to convert simple with the * argument constructor to any representation of this' pointer with devastating retain their original argument list, but an indicator representing the object.

Any advice is appreciated

/ div>

Prerequisite public mode requires a C function. In C code you also need an opaque indicator to represent your class.
It is easy to use just a zero *, although you can construct such a structure that contains zero * and other information (for example if you want to support arrays?).

  Fred .h -------------------------------- #ifdef __cplusplus Category Fred {Public: Fred (int x, int y); Int doStuff (int p); }; #endif / / / c interface Typedef zero * CFred; // // Extern "C" CFredreded (int x, int y) CFred; requires a clear manufacturer and destroyer; Extern "C" Zero DelCFred (CFred); // // Every public law takes opaque reference to the object that was returned from the above constructor and also the methods parameters. Extra "C" Int Dustaffeed (CFread, Int P);  

The implementation is trivial.
Convert an opaque indicator to Fred, and then call the method.

  CFred.cpp ---- ---------------------------- // A CPP file Implemented in // But keep in mind that they were declared above as "C" above, it gets them // link and thus they are available from C-Bid. CFred newCFred (int x, int y) {return reinterpret_cast & lt; Void * & gt; (New Fred (X, Y)); } Zero Delfred (Seferred Fred) {remove reinterpret_cast & lt; Fred * & gt; (Fred); } Int Dustfraud (Seafried Fred, Int P) {Return Returns & lt; Fred * & gt; (Fred) - & gt; Dustst (p); }  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -