检测从Windows DLL中调用Python脚本时使用CTypes [英] Detecting Calling Python Script from Windows DLL When Using CTypes
问题描述
我通过Python使用ctypes调用dll,如如何从脚本调用DLL的答案语言?
在dll内,我可以使用WINAPI GetModuleFileName() http://msdn.microsoft.com/en-us/library/windows/桌面/ ms683197(v = vs.85)的.aspx 。然而,由于这是一个Python脚本,它是通过Python可执行文件运行的,所以返回的模块文件名是C:/Python33/Python.exe。我需要执行该调用的实际脚本文件的名称。这是可能的吗?
有一些背景为什么:这个dll用于身份验证。它使用共享秘密密钥生成哈希,用于脚本验证HTTP请求。它嵌入在dll中,以便使用脚本的人不会看到密钥。我们要确保调用脚本的python文件是签名的,所以不只是任何人都可以使用这个dll来生成签名,所以获取调用脚本的文件路径是第一步。
一般来说,没有使用Python C-API,您可以获取进程命令行并将其解析为 argv
数组使用Win32 GetCommandLine 和 CommandLineToArgvW 。然后检查 argv [1]
是否为.py文件。
Python演示,使用ctypes:
import ctypes
从ctypes import wintypes
GetCommandLine = ctypes.windll.kernel32.GetCommandLineW
GetCommandLine.restype = wintypes.LPWSTR
GetCommandLine.argtypes = []
CommandLineToArgvW = ctypes.windll.shell32.CommandLineToArgvW
CommandLineToArgvW.restype = ctypes.POINTER(wintypes.LPWSTR)
CommandLineToArgvW.argtypes = [
wintypes.LPCWSTR,#lpCmdLine,
ctypes.POINTER(ctypes.c_int),#pNumArgs
]
如果__name__ =='__main__':
cmdline = GetCommandLine()
argc = ctypes.c_int ()
argv = CommandLineToArgvW(cmdline,ctypes.byref(argc))
argc = argc.value
argv = argv [:argc]
print(argv)
I am seeking to add functionality within a windows dll to detect the name of a calling Python script.
I am calling the dll via Python using ctypes as described in the answers to How can I call a DLL from a scripting language?
Within the dll I'm able to successfully determine the calling process using WINAPI GetModuleFileName() http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197(v=vs.85).aspx. However, since this is a Python script it's being run via the Python executable thus the returned module file name is "C:/Python33/Python.exe". I'm needing the name of the actual script file doing the call. Is this possible?
A bit of background on why: this dll is used for authentication. It's generating a hash using a shared secret key for the script to use to authenticate an HTTP request. It's embedded in the dll so that people using the script won't see the key. We want to make sure the python file calling the script is signed so not just anyone can use this dll to generate a signature, so getting the filepath of the calling script is the first step.
Generically, without using the Python C-API, you can get the process command line and parse it into an argv
array using Win32 GetCommandLine and CommandLineToArgvW. Then check if argv[1]
is a .py file.
Python demo, using ctypes:
import ctypes
from ctypes import wintypes
GetCommandLine = ctypes.windll.kernel32.GetCommandLineW
GetCommandLine.restype = wintypes.LPWSTR
GetCommandLine.argtypes = []
CommandLineToArgvW = ctypes.windll.shell32.CommandLineToArgvW
CommandLineToArgvW.restype = ctypes.POINTER(wintypes.LPWSTR)
CommandLineToArgvW.argtypes = [
wintypes.LPCWSTR, # lpCmdLine,
ctypes.POINTER(ctypes.c_int), # pNumArgs
]
if __name__ == '__main__':
cmdline = GetCommandLine()
argc = ctypes.c_int()
argv = CommandLineToArgvW(cmdline, ctypes.byref(argc))
argc = argc.value
argv = argv[:argc]
print(argv)
这篇关于检测从Windows DLL中调用Python脚本时使用CTypes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!