Detecting Calling Python Script from Windows DLL When Using CTypes -
i seeking add functionality within windows dll detect name of calling python script.
i calling dll via python using ctypes described in answers how can call dll scripting language?
within dll i'm able determine calling process using winapi getmodulefilename() http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197(v=vs.85).aspx. however, since python script it's being run via python executable returned module file name "c:/python33/python.exe". i'm needing name of actual script file doing call. possible?
a bit of background on why: dll used authentication. it's generating hash using shared secret key script use authenticate http request. it's embedded in dll people using script won't see key. want make sure python file calling script signed not can use dll generate signature, getting filepath of calling script first step.
generically, without using python c-api, can process command line , parse argv
array using win32 getcommandline , commandlinetoargvw. check if argv[1]
.py file.
python demo, using 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 ] if __name__ == '__main__': cmdline = getcommandline() argc = ctypes.c_int() argv = commandlinetoargvw(cmdline, ctypes.byref(argc)) argc = argc.value argv = argv[:argc] print(argv)
Comments
Post a Comment