Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode.
Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).
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
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
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
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.
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.
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()