Creating libraries



  • On 30/05/2016 at 06:39, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:    
    Platform:      
    Language(s) :     C++  ;

    ---------
    Hi!
    I have a project where I may want to expose a function in my plugin to other plugins via a C4DLibrary, as in

    https://developers.maxon.net/docs/Cinema4DCPPSDK/html/struct_c4_d_library.html

    In older versions of the SDK, I seem to recall that there was an article describing how to do this, with example. Indeed, the current SDK (page linked above) says "See also: Creating Libraries" but there is no link. Has that article been removed, or can I find it somewhere else?

    Thanks
    /Filip



  • On 30/05/2016 at 10:02, xxxxxxxx wrote:

    Doesn't seem to be in the docs anymore but here is it from an older SDK version:

    Function libraries are the easiest to create. They only let the user call functions in your library and do not provide any classes. The first step is to create internal function. Just create a global function in your library module, for example:
    Bool iMyLibraryFunction(Int32 v, String s) {   GePrint("v: " + String::IntToString(v) + ", s: " + s);   return v != 42; }
    Note the naming convention with an i in front. Then you need to create the library structure. This should be a structure derived from _<_span title="struct c4dlibrary"_>_C4DLibrary with a function pointer to your kind of function. The struct should be placed in the public header file of your library and #included into the library module:
    struct MyFunctionLib : public [\_ry">C4DLibrary](../c4d_library/struct_C4DLibrary251.html) {   Bool (\*MyLibraryFunction)(Int32 v, String s); };
    Creating a function pointer from a function is really only adding two parentheses and a \* like above. Next you should create a global instance of this struct in your library module and write the registration function like this: (With the _IORITY">C4DMSG_PRIORITY message you should tell CINEMA 4D that your library needs to be registered before other plugins.)
    `  
    MyFunctionLib flib;

    Bool RegisterMyFunctionLib()
    {
      // Clear the structure
      ClearMem(&flib, sizeof(flib));

    // Fill in all function pointers
      flib.MyLibraryFunction = iMyLibraryFunction;

    // Install the library
      return _Int32 size">InstallLibrary(MY_UNIQUE_FUNCTION_LIBRARY_ID, &flib, 1000, sizeof(flib));
    }
    Note that you do not need a&to take the function pointer address of theiMyLibraryFunctionfunction. Also note thatMY_UNIQUE_FUNCTION_LIBRARY_IDshould be a unique ID from [Plugin Cafe](http://www.plugincafe.com). It should be placed in the public header:  
    const Int32 MY_UNIQUE_FUNCTION_LIBRARY_ID = ...;
    Now we only need to create the public glue to this library. In the public header file, add in a function with the same signature as your internal one: (This is why we appended anito the internal function, because otherwise they would have collided when we included the public header into the library module.)  
    Bool MyLibraryFunction(Int32 v, String s);
    Then you create a public _.cpp_ module for your library. In it you first create this helper function:  
    MyFunctionLib* flib_cache = nullptr;

    MyFunctionLib* CheckMyFunctionLib(Int32 offset)
    {
      return (MyFunctionLib* ) _DLibrary store">CheckLib(MY_UNIQUE_FUNCTION_LIBRARY_ID, offset, (C4DLibrary** ) &flib_cache);
    }
    Finally you implement the glue function like this:  
    Bool MyLibraryFunction(Int32 v, String s)
    {
      MyFunctionLib* flib = CheckMyFunctionLib([_rary::LIBOFFSETs,m">LIBOFFSET(MyFunctionLib, MyLibraryFunction)](../c4d_library/global_c4d_library213.html#liboffse_<_span title=));
      if (!lfib || !flib->MyLibraryFunction) return false ;

    return flib->MyLibraryFunction(v, s);
    }
    Now anyone can import your public _myfunctionlibrary.h_ and _myfunctionlibrary.cpp_ files into their project, and as long as the library is installed the functionMyLibraryFunction()` will work just as an ordinary function.

    This is the complete listing for each file:
    `  
    // myfunctionlibrary.h
    #include "c4d.h"

    Bool MyLibraryFunction(Int32 v, String s);

    // INTERNAL
    const Int32 MY_UNIQUE_FUNCTION_LIBRARY_ID = ...;

    struct MyFunctionLib : public [_tle="struct C4DLibrary">C4DLibrary](../c4d_library/struct_C4DLibrary25_<_span title=)
    {
      Bool (*MyLibraryFunction)(Int32 v, String s);
    };
    // INTERNAL

    // myfunctionlibrary.cpp
    #include "myfunctionlibrary.h"

    MyFunctionLib* flib_cache = nullptr;

    MyFunctionLib* CheckMyFunctionLib(Int32 offset)
    {
      return (MyFunctionLib* ) [_t offset, C4DLibrary store">CheckLib(MY_UNIQUE_FUNCTION_LIBRARY_ID, offset, (C4DLibrary** ) &flib_cache)](../c4d_library/global_c4d_library213.html_<_span title=);
    }

    Bool MyLibraryFunction(Int32 v, String s)
    {
      MyFunctionLib* flib = CheckMyFunctionLib([_le=" c4d_library::LIBOFFSETs,m">LIBOFFSET(MyFunctionLib, MyLibraryFunction)](../c4d_library/global_c4d_library213.h_<_span title=));
      if (!lfib || !flib->MyLibraryFunction) return false ;

    return flib->MyLibraryFunction(v, s);
    }

    // myfunctionlibrarymodule.cpp
    #include "myfunctionlibrary.h"

    Bool iMyLibraryFunction(Int32 v, String s)
    {
      GePrint("v: " + String::IntToString(v) + ", s: " + s);
      return v != 42;
    }

    MyFunctionLib flib;

    Bool RegisterMyFunctionLib()
    {
      // Clear the structure
      ClearMem(&flib, sizeof(flib));

    // Fill in all function pointers
      flib.MyLibraryFunction = iMyLibraryFunction;

    // Install the library
      return [_lib,Int32 version, Int32 size">InstallLibrary(MY_UNIQUE_FUNCTION_LIBRARY_ID, &flib, 1000, sizeof(flib))](../c4d_library/global_c4d_library213.ht_<_span title=);
    }

    `



  • On 30/05/2016 at 11:04, xxxxxxxx wrote:

    Thanks a lot Samir! That is indeed the text I was looking for.



  • On 30/05/2016 at 23:35, xxxxxxxx wrote:

    SDK team: Would it be possible to put this valuable information back in the SDK documentation, or was it removed for a particular reason?



  • On 31/05/2016 at 03:03, xxxxxxxx wrote:

    Hi,

    shame on us! The C4DLibrary howto got lost while transitioning to the new documentation system. Sorry, and thanks for making us aware!
    As a workaround (besides that Samir posted it already here, thanks Samir!) you can still download R14 or R15 documentation from our development blog.
    And of course we will add the information back in asap.



  • On 31/05/2016 at 03:33, xxxxxxxx wrote:

    Thanks Andreas! You can mark this as solved.



  • On 22/09/2017 at 12:02, xxxxxxxx wrote:

    Hello SDK team!

    Just a small note that the link in the SDK is still not  linking...

    Fortunately, I found the info here:)

    Regards,

    Hermen



  • On 23/09/2017 at 08:51, xxxxxxxx wrote:

    In case you missed it here is the link to the actual SDK manual about Libraries.
    https://developers.maxon.net/docs/Cinema4DCPPSDK/html/page_creating_libraries.html



  • On 24/09/2017 at 12:54, xxxxxxxx wrote:

    @ gr4ph0s:
    Thank you! That is even more elaborate than the text here.

    regards,

    Hermen


Log in to reply