From b817e8d30ed8922c0ed70494febae85003c0bcc6 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Thu, 19 Sep 2019 21:28:59 -0500 Subject: [PATCH] Fixing Windows ffi python example * Added bat to build Windows i686 and x86_64 libraries * Added bat file to demonstrate calling the python ffi test * Updated the python ffi test to load the dll correctly --- FFI-tests/ffi_test.py | 62 +++++++++++++++---------------- FFI-tests/run_ffi_test_python.bat | 2 + build_windows_libs.bat | 3 ++ 3 files changed, 36 insertions(+), 31 deletions(-) create mode 100644 FFI-tests/run_ffi_test_python.bat create mode 100644 build_windows_libs.bat diff --git a/FFI-tests/ffi_test.py b/FFI-tests/ffi_test.py index 73aac08..8d288e8 100755 --- a/FFI-tests/ffi_test.py +++ b/FFI-tests/ffi_test.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 from ctypes import * -from sys import exit, platform +from sys import platform, exit large_image1_path = "../test_images/sample_01_large.jpg".encode(encoding="utf-8") medium_image1_path = "../test_images/sample_01_medium.jpg".encode(encoding="utf-8") @@ -19,39 +19,39 @@ large_image4_path = "../test_images/sample_04_large.jpg".encode(encoding="utf-8" medium_image4_path = "../test_images/sample_04_medium.jpg".encode(encoding="utf-8") small_image4_path = "../test_images/sample_04_small.jpg".encode(encoding="utf-8") -test_images=[ - large_image1_path, medium_image1_path, small_image1_path, - large_image2_path, medium_image2_path, small_image2_path, - large_image3_path, medium_image3_path, small_image3_path, - large_image4_path, medium_image4_path, small_image4_path +test_images = [ + large_image1_path, medium_image1_path, small_image1_path, + large_image2_path, medium_image2_path, small_image2_path, + large_image3_path, medium_image3_path, small_image3_path, + large_image4_path, medium_image4_path, small_image4_path ] + def unsigned64(number): - return (number & 0xfffffffffffffffff) + return (number & 0xfffffffffffffffff) + print("starting ffi test") # Load the shared library lib = None if platform == 'win32': - path = os.path.dirname(__file__) + "\\pihash.dll" - print(path) - lib = CDLL(path) + lib = cdll.pihash elif platform == 'darwin': - lib = cdll.LoadLibrary("libpihash.dylib") + lib = cdll.LoadLibrary("libpihash.dylib") elif platform == 'linux' or platform == 'linux2': - lib = cdll.LoadLibrary("libpihash.so") + lib = cdll.LoadLibrary("libpihash.so") else: - print('Unrecognized platform ', platform) - sys.exit(-1) + print('Unrecognized platform ', platform) + exit(-1) class PIHashes(Structure): - _fields_ = [ - ("ahash", c_ulonglong), - ("dhash", c_ulonglong), - ("phash", c_ulonglong)] - + _fields_ = [ + ("ahash", c_ulonglong), + ("dhash", c_ulonglong), + ("phash", c_ulonglong)] + # Setting the ctypes return type references for the foreign functions # returns a pointer to the library that we'll need to pass to all function calls @@ -70,26 +70,26 @@ lib.ext_free_pihashes.argtypes = [c_void_p] # Takes a pointer and frees the struct at that memory location lib.ext_free.argtypes = [c_void_p] -#initialize the library +# initialize the library lib_struct = lib.ext_init("./.hash_cache".encode('utf-8')) -#print("Pointer to lib_struct: ", lib_struct) +# print("Pointer to lib_struct: ", lib_struct) # Print the path and the bytes as hex for debugging -#print(large_image_path) -#print('\\x'+'\\x'.join('{:02x}'.format(x) for x in large_image_path)) +# print(large_image_path) +# print('\\x'+'\\x'.join('{:02x}'.format(x) for x in large_image_path)) for image in test_images: - print("Requesting hashes for: %s"% image) + print("Requesting hashes for: %s" % image) phashes = lib.ext_get_pihashes(lib_struct, image) - pihashes = PIHashes.from_address(phashes) + pihashes = PIHashes.from_address(phashes) lib.ext_free_pihashes(phashes) - print("ahash: %i"% unsigned64(pihashes.ahash)) - print("dhash: %i"% unsigned64(pihashes.dhash)) - print("phash: %i"% unsigned64(pihashes.phash)) - # print("ahash: %i"% unsigned64(lib.ext_get_ahash(lib_struct, image))) - # print("dhash: %i"% unsigned64(lib.ext_get_dhash(lib_struct, image))) - # print("phash: %i"% unsigned64(lib.ext_get_phash(lib_struct, image))) + print("ahash: %i" % unsigned64(pihashes.ahash)) + print("dhash: %i" % unsigned64(pihashes.dhash)) + print("phash: %i" % unsigned64(pihashes.phash)) +# print("ahash: %i"% unsigned64(lib.ext_get_ahash(lib_struct, image))) +# print("dhash: %i"% unsigned64(lib.ext_get_dhash(lib_struct, image))) +# print("phash: %i"% unsigned64(lib.ext_get_phash(lib_struct, image))) # Do cleanup # Makes sure that the heap is cleaned up diff --git a/FFI-tests/run_ffi_test_python.bat b/FFI-tests/run_ffi_test_python.bat new file mode 100644 index 0000000..ae0c5d3 --- /dev/null +++ b/FFI-tests/run_ffi_test_python.bat @@ -0,0 +1,2 @@ +SET PATH=%cd%;%PATH% +python ./ffi_test.py diff --git a/build_windows_libs.bat b/build_windows_libs.bat new file mode 100644 index 0000000..73516f0 --- /dev/null +++ b/build_windows_libs.bat @@ -0,0 +1,3 @@ +cargo clean +cargo build --target=i686-pc-windows-msvc --release --lib +cargo build --target=x86_64-pc-windows-msvc --release --lib \ No newline at end of file