使用CType检测从Windows DLL调用Python脚本

我正在寻求在Windows DLL中添加function来检测调用Python脚本的名称。

我正在通过Python使用ctypes来调用dll,正如我如何从脚本语言调用DLL所描述的那样?

在DLL中,我能够成功地确定调用过程使用WINAPI GetModuleFileName() http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197 (v=vs.85) .aspx 。 但是,由于这是一个Python脚本,因此它正在通过Python可执行文件运行,因此返回的模块文件名是“C:/Python33/Python.exe”。 我需要实际的脚本文件的名字进行呼叫。 这可能吗?

有一点关于为什么:这个DLL用于身份validation的背景。 它使用共享密钥生成一个散列,脚本用于validationHTTP请求。 它embedded在DLL中,以便使用脚本的人不会看到密钥。 我们要确保调用脚本的python文件是经过签名的,所以不是任何人都可以使用这个dll来生成签名,因此获取调用脚本的文件path是第一步。

通常,不使用Python C-API,可以使用Win32 GetCommandLine和CommandLineToArgvW获取进程命令行并将其解析为argv数组。 然后检查argv[1]是否是.py文件。

Python演示,使用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)