简体   繁体   English

我可以防止C ++ dll加载到Python Ctypes中吗?

[英]Can I prevent a C++ dll from loading in Python Ctypes?

I'd like to know if it's possible prevent or allow a dll to be loaded by python ctypes, based on whether a condition is true from within the dll. 我想根据条件是否来自dll ,确定是否可以阻止或允许python ctypes加载dll。

Some background: 一些背景:

My application uses various calculation algorithms, which I prototyped in Python, and then reimplemented in C++ for a speed boost. 我的应用程序使用各种计算算法,这些算法是我在Python中创建的原型,然后在C ++中重新实现以提高速度。 I still use Python for the application "glue" and GUI. 我仍将Python用于应用程序“胶水”和GUI。 I'm accessing the functions in the dll using a ctypes wrapper. 我正在使用ctypes包装器访问dll中的功能。

I now need to secure the software, so that it will only run if a security dongle is present. 现在,我需要保护软件安全,以便仅在存在安全加密狗的情况下才能运行该软件。 The open nature of Python makes this difficult, so I'd like to be able to stop a python script loading the dll unless a function which checks the dongle is present returns True. Python的开放性使这一点变得困难,因此,我希望能够停止加载dll的python脚本,除非存在检查加密狗的函数返回True。

Example Python wrapper: 示例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)

And the code for the my.dll: 以及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

Ideally the dll would fail to load if dongle_is_present returned false. 理想情况下,如果dongle_is_present返回false,则dll将无法加载。 Can anyone help? 有人可以帮忙吗? Please tell me if this question is unclear! 如果这个问题不清楚,请告诉我!

Many thanks 非常感谢

Add DllMain function to your library. DllMain函数添加到您的库。

An optional entry point into a dynamic-link library (DLL). 动态链接库(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. 当系统启动或终止进程或线程时,它将使用进程的第一个线程为每个加载的DLL调用入口点函数。 The system also calls the entry-point function for a DLL when it is loaded or unloaded using the LoadLibrary and FreeLibrary functions. 当使用LoadLibrary和FreeLibrary函数加载或卸载DLL时,系统还会调用DLL的入口点函数。

You could prevent dll load by returning FALSE on DLL_PROCESS_ATTACH : 您可以通过在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. 当系统使用DLL_PROCESS_ATTACH值调用DllMain函数时,如果成功,则该函数返回TRUE,如果初始化失败,则返回FALSE。 If the return value is FALSE when DllMain is called because the process uses the LoadLibrary function, LoadLibrary returns NULL. 如果由于进程使用LoadLibrary函数而在调用DllMain时返回值为FALSE,则LoadLibrary返回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 (系统立即使用DLL_PROCESS_DETACH调用您的入口点函数,并卸载DLL。)如果在进程初始化期间调用DllMain时返回值为FALSE,则该进程终止并出现错误

See DllMain MSDN entry for the additional information. 有关其他信息,请参见DllMain MSDN条目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM