0

I am trying to load a DLL using ctypes in order to run some functions from the ASWHook.dll file that comes with the old AutoHotKey AutoScriptWriter. I am able to use a program to view the function names, but I cannot figure out why ctypes is crashing when I try to call a function. I'm guessing it's because the functions need parameters, but the program I'm using doesn't tell you any of that information. The program I'm using is called "DLL Export Viewer"

The function I'm trying to call is called GetKeyInfo, and this is what I'm doing in my script.

import ctypes
d = ctypes.WinDLL ("C:\\Users\\Username\\Desktop\\AHK\\AutoScriptWriter\\ASWHook.dll")
f = d['GetKeyInfo']
v = f()

This causes the python interpreter to freeze and then restart. I'm not sure if GetKeyInfo takes any parameters, but either way, it's crashing when I try to use it. Is there a better way to inspect the DLL in order to get more information about what parameters the functions take?

Sorry if this is simple stuff, I'm new to using DLLs in general, so I'm not exactly sure.

I was looking at this answer on how to use the DLLs, but it was really confusing.

I'm working off this example now.

Community
  • 1
  • 1
RattleyCooper
  • 4,997
  • 5
  • 27
  • 43
  • 4
    You can't. For an exported C function (then without name mangling) you have no idea about its parameters, calling convention and return value. Yes, with some reverse engineering it _may_ be done but... – Adriano Repetti Apr 01 '16 at 15:10
  • @AdrianoRepetti, Can you provide more information as to why this isn't possible? – RattleyCooper Apr 01 '16 at 15:14
  • 2
    Simply because it's not saved anywhere. Even function name is optional, that's why to call a C function from a DLL you need C header files. Plain Windows DLLs don't contain any metadata (unless you're exporting .NET classes, .COM objects or similar). What you have is just a function name (or its ordinal) and an address to call. – Adriano Repetti Apr 01 '16 at 15:17
  • @AdrianoRepetti, so there are 2 cases where I may be able to recover the parameters, or am I misunderstanding what you are saying? – RattleyCooper Apr 01 '16 at 15:18
  • 1
    No, there are not. If, instead of a C function, your DLL exports COM or NET objects then yes, metadata are embedded inside DLL then you can know. If it's exporting C functions then that data is not available. Even C++ name mangling doesn't include calling convention and return value (and it doesn't help you with structures)... – Adriano Repetti Apr 01 '16 at 15:20
  • @AdrianoRepetti, Ok, now I'm getting it. Thanks for the clarification Adriano. – RattleyCooper Apr 01 '16 at 15:20
  • @AdrianoRepetti, Actually, it looks like this site can recover some stuff. Was able to find the code along with parameters and return values :D https://retdec.com Granted, it's really ugly and I'm not sure it's going to do me a ton of good, but it worked! – RattleyCooper Apr 01 '16 at 15:34

0 Answers0