Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Recent
    • Tags
    • Users
    • Login

    calling C++ from Python

    Scheduled Pinned Locked Moved PYTHON Development
    6 Posts 0 Posters 451 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      Helper
      last edited by

      On 06/10/2013 at 07:25, xxxxxxxx wrote:

      We want to make cracking our python plugin a bit more difficult, by putting the hash creation in a C++ library.
      Is there a simple but complete tutorial how to call C++ functions from python?

      Pim

      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        On 06/10/2013 at 07:53, xxxxxxxx wrote:

        not sure if you can read german, but i think one of the best python tutorials come from
        a german IT publishing company called galileo computing.

        http://openbook.galileocomputing.de/python/python_kapitel_26_001.htm

        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          On 06/10/2013 at 11:04, xxxxxxxx wrote:

          A link to an english tutorial on how to use the ctypes module is here:
          http://python.net/crew/theller/ctypes/tutorial.html

          Remember that dynamic libraries can be replaced easily. It should be relatively
          safe if you perform a proper check, eg. by comparing via an MD5 hash.

          Best,
          -Niklas

          1 Reply Last reply Reply Quote 0
          • H Offline
            Helper
            last edited by

            On 07/10/2013 at 23:59, xxxxxxxx wrote:

            Thanks for the links littledevil and Niklas,

            And indeed, checking the dynamic library using a MD5 hash is a good idea.
            The more levels of protection, the better.

            1 Reply Last reply Reply Quote 0
            • H Offline
              Helper
              last edited by

              On 09/10/2013 at 00:57, xxxxxxxx wrote:

              sorry, I'm running into errors.
              "AttributeError: function 'Add' not found"

              I guess my C++ is not correct.
              I do believe the lib is oke:

              1>------ Build started: Project: MathFuncsDll, Configuration: Debug x64 ------
              1>     Creating library d:\users\pgrooff\documents\visual studio 2010\Projects\Dynamic library\x64\Debug\MathFuncsDll.lib and object d:\users\pgrooff\documents\visual studio 2010\Projects\Dynamic library\x64\Debug\MathFuncsDll.exp
              1>  MathFuncsDll.vcxproj -> d:\users\pgrooff\documents\visual studio 2010\Projects\Dynamic library\x64\Debug\MathFuncsDll.dll
              1>  MathFuncsDll.vcxproj -> d:\users\pgrooff\documents\visual studio 2010\Projects\Dynamic library\x64\Debug\MathFuncsDll.dll
              ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

              Here the c++ h and cpp file and the python code

              // MathFuncsDll.h
                
              #ifdef MATHFUNCSDLL_EXPORTS
              #define MATHFUNCSDLL_API __declspec(dllexport) 
              #else
              #define MATHFUNCSDLL_API __declspec(dllimport) 
              #endif
                
              namespace MathFuncs
              {
                  // This class is exported from the MathFuncsDll.dll
                  class MyMathFuncs
                  {
                  public: 
                      // Returns a + b
                      static MATHFUNCSDLL_API double Add(double a, double b); 
                
                      // Returns a - b
                      static MATHFUNCSDLL_API int Subtract(int a, int b); 
                
                      // Returns a * b
                      static MATHFUNCSDLL_API double Multiply(double a, double b); 
                
                      // Returns a / b
                      // Throws const std::invalid_argument& if b is 0
                      static MATHFUNCSDLL_API double Divide(double a, double b); 
                  };
              }
                
              -----------------------
                
              // MathFuncsDll.cpp : Defines the exported functions for the DLL application.
              //
                
              #include "stdafx.h"
                
                
              // MathFuncsDll.cpp : Defines the exported functions for the DLL application. 
              //
                
              #include "stdafx.h"
              #include "MathFuncsDll.h"
              #include <stdexcept>
                
              using namespace std;
                
              namespace MathFuncs
              {
                  double MyMathFuncs::Add(double a, double b)
                  {
                      return a + b;
                  }
                
                  int MyMathFuncs::Subtract(int a, int b)
                  {
                      return a - b;
                  }
                
                  double MyMathFuncs::Multiply(double a, double b)
                  {
                      return a * b;
                  }
                
                  double MyMathFuncs::Divide(double a, double b)
                  {
                      if (b == 0)
                      {
                          throw invalid_argument("b cannot be zero!");
                      }
                
                      return a / b;
                  }
              }
                
              -------------------
                
              import c4d
              import ctypes 
                
                
              def main() :
                  path = 'D:/Users/pgrooff/Documents/Visual Studio 2010/Projects/Dynamic library/x64/Debug/MathFuncsDll.dll'
                
                  bibliothek = ctypes.CDLL(path) 
                  #bib = ctypes.CDLL('msvcrt')
                  #print bib.printf('2.33aaaa')
                  #print bib.time(None)
                  print bibliothek.Add(c_double(1.5),c_double(1.5))
                
                  
              if __name__=='__main__':
                  main()
              

              Note: Using another lib 'msvcrt' seem to work.

              1 Reply Last reply Reply Quote 0
              • H Offline
                Helper
                last edited by

                On 09/10/2013 at 06:13, xxxxxxxx wrote:

                Ok, I made it much more simpler and it works now.

                Just a simple C++ file, no h file.

                #define DLLEXPORT extern "C" __declspec(dllexport)
                  
                  
                DLLEXPORT int add()
                {
                	return 11;
                }
                  
                

                And the python script code:

                import c4d
                import sys
                import ctypes 
                  
                def main() :
                  
                    lib_path = 'D:/Users/pgrooff/Documents/Visual Studio 2010/Projects/SimpleDLL/x64/Release/simpleDLL.dll'
                    mydll = ctypes.CDLL(lib_path) 
                    print mydll.add()
                  
                  
                    
                if __name__=='__main__':
                    main()
                  
                
                
                1 Reply Last reply Reply Quote 0
                • First post
                  Last post