我可以阻止 C++ dll 在 Python Ctypes 中加载吗?
Can I prevent a C++ dll from loading in Python Ctypes?
我想知道是否有可能阻止或允许 python ctypes 加载 dll,基于 dll 中的条件是否为真。
一些背景:
我的应用程序使用各种计算算法,我在 Python 中制作了原型,然后用 C++ 重新实现以提高速度。对于应用程序 "glue" 和 GUI,我仍然使用 Python。我正在使用 ctypes 包装器访问 dll 中的函数。
我现在需要保护软件,这样只有在存在安全加密狗时它才会 运行。 Python 的开放性使这变得困难,所以我希望能够停止 python 脚本加载 dll,除非存在检查加密狗的函数 returns 是的。
示例 Python 包装器:
from ctypes import cdll, c_int , c_float, c_bool
lib = cdll.LoadLibrary('my.dll')
cpp_sum = lib.sum
cpp_sum.argtypes = [c_int,c_int]
cpp_sum.restype = c_int
def wrapped_sum(value_1,value_2):
return cpp_sum(value_1,value_2)
以及 my.dll 的代码:
#include "stdafx.h"
#include <cmath>
#define DLLEXPORT extern "C" __declspec(dllexport)
DLLEXPORT int sum(int a, int b)
{return a + b;}
//pseudo dongle code:
bool is_dongle_present(){
if dongle present return true
else return false
理想情况下,如果 dongle_is_present 返回 false,dll 将无法加载。谁能帮忙?
如果这个问题不清楚请告诉我!
非常感谢
将 DllMain
函数添加到您的库中。
An optional entry point into a dynamic-link library (DLL). When the
system starts or terminates a process or thread, it calls the
entry-point function for each loaded DLL using the first thread of the
process. The system also calls the entry-point function for a DLL when
it is loaded or unloaded using the LoadLibrary and FreeLibrary
functions.
您可以通过在 DLL_PROCESS_ATTACH
上返回 FALSE
来阻止 dll 加载:
When the system calls the DllMain function with
the DLL_PROCESS_ATTACH value, the function returns TRUE if it succeeds
or FALSE if initialization fails. If the return value is FALSE when
DllMain is called because the process uses the LoadLibrary function,
LoadLibrary returns NULL. (The system immediately calls your
entry-point function with DLL_PROCESS_DETACH and unloads the DLL.) If
the return value is FALSE when DllMain is called during process
initialization, the process terminates with an error
有关其他信息,请参阅 DllMain MSDN entry。
我想知道是否有可能阻止或允许 python ctypes 加载 dll,基于 dll 中的条件是否为真。
一些背景:
我的应用程序使用各种计算算法,我在 Python 中制作了原型,然后用 C++ 重新实现以提高速度。对于应用程序 "glue" 和 GUI,我仍然使用 Python。我正在使用 ctypes 包装器访问 dll 中的函数。
我现在需要保护软件,这样只有在存在安全加密狗时它才会 运行。 Python 的开放性使这变得困难,所以我希望能够停止 python 脚本加载 dll,除非存在检查加密狗的函数 returns 是的。
示例 Python 包装器:
from ctypes import cdll, c_int , c_float, c_bool
lib = cdll.LoadLibrary('my.dll')
cpp_sum = lib.sum
cpp_sum.argtypes = [c_int,c_int]
cpp_sum.restype = c_int
def wrapped_sum(value_1,value_2):
return cpp_sum(value_1,value_2)
以及 my.dll 的代码:
#include "stdafx.h"
#include <cmath>
#define DLLEXPORT extern "C" __declspec(dllexport)
DLLEXPORT int sum(int a, int b)
{return a + b;}
//pseudo dongle code:
bool is_dongle_present(){
if dongle present return true
else return false
理想情况下,如果 dongle_is_present 返回 false,dll 将无法加载。谁能帮忙? 如果这个问题不清楚请告诉我!
非常感谢
将 DllMain
函数添加到您的库中。
An optional entry point into a dynamic-link library (DLL). When the system starts or terminates a process or thread, it calls the entry-point function for each loaded DLL using the first thread of the process. The system also calls the entry-point function for a DLL when it is loaded or unloaded using the LoadLibrary and FreeLibrary functions.
您可以通过在 DLL_PROCESS_ATTACH
上返回 FALSE
来阻止 dll 加载:
When the system calls the DllMain function with the DLL_PROCESS_ATTACH value, the function returns TRUE if it succeeds or FALSE if initialization fails. If the return value is FALSE when DllMain is called because the process uses the LoadLibrary function, LoadLibrary returns NULL. (The system immediately calls your entry-point function with DLL_PROCESS_DETACH and unloads the DLL.) If the return value is FALSE when DllMain is called during process initialization, the process terminates with an error
有关其他信息,请参阅 DllMain MSDN entry。