Smarter dllcall()

Discuss the future of the AutoHotkey language
arcticir
Posts: 647
Joined: 17 Nov 2013, 11:32

Smarter dllcall()

18 Jul 2020, 23:41

The recent progress of V2 is surprising :bravo:
I found that PY calls DLL without specifying the parameter type. This is very convenient, using DLL becomes very simple. So can we achieve the same effect in V2?
lexikos
Posts: 7057
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Smarter dllcall()

26 Jul 2020, 01:44

What is PY? :roll:

There are two ways to avoid specifying the parameter type:
  1. Use only predefined functions, for which the parameter types are known.
  2. Make assumptions about the parameter type.
In neither case is the actual function call smarter, but one might be more or less efficient, (un)safe, etc.

As far as I can tell,
Python's CFFI uses #1. It takes a full C definition of a function and produces a function object. In some cases it invokes a C compiler.
Python's ctypes uses #1 or #2. If you don't tell it what arg types the function has, it makes assumptions based on what values you pass.

For #1, you can just write wrapper functions, or use AutoHotkey_H and #DllImport.

For #2, if we assume that the parameter should be "str" for any string, "double" for any floating-point number and "ptr" otherwise, you won't have to specify the type, but:
  • Functions that do not accept a UTF-16 string would require you to manually convert the string before passing it (or call a function that we add for that purpose; e.g. AStr(value)).
  • There would be no way to specify that the parameter is for output. Instead, you will have to pass a buffer and read from it afterward.
  • You would need to manually reinterpret the return value as the appropriate data type, or find a new way to specify the return type that isn't ambiguous with a parameter value.
  • You would not be able to specify other types without the use of additional functions (i.e. converting the value to a "ptr").
It does not make using a dll simpler. If the parameter types aren't predefined, you must be even more careful to pass the appropriate type of value, or do even more work to convert values to a type appropriate for the function.

It can be done in script, but not very efficiently.
User avatar
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Smarter dllcall()

30 Jul 2020, 05:31

What is PY?
Its definitely .py Python :)
arcticir wrote:
18 Jul 2020, 23:41
I found that PY calls DLL without specifying the parameter type. This is very convenient, using DLL becomes very simple. So can we achieve the same effect in V2?
It was partially tackled in AHK_H V2 branch (by @HotKeyIt) with DllCall's equivalent named DynaCall.

Code: Select all

DllCall	DynaCall equivalent
Int	i
Str	s
AStr	a
WStr	w
Short	h
Char	c
Float	f
Double	d
PTR	t
Int64	i6
HotKeyIt happened to generate many brilliant ideas, but I prefer Lexikos's mainstreamed AHK V2 branch, yet I would like to have abbreviated types for dllcall. Oh, and some abbr. (like i6) look awkward: int64 -> i64, ptr -> p, ptrp (ptr*) -> pp
DllCall could actually support both notations - regular and abbreviated at the same time for compatibility sake.
User avatar
nnnik
Posts: 4470
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Smarter dllcall()

10 Aug 2020, 08:21

Instead of i64 maybe l for long?
Recommends AHK Studio

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 7 guests