Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[AHK_H+Hv2] - WinApi()


  • Please log in to reply
21 replies to this topic
HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

WinApi() wraps a lot of Windows dlls and allows to use DllCall much easier.
Additionally it saves a lot of code :!:

Requires AutoHotkey_H.exe or dll due to DynaCall!

WinApi.ahk - or - WinApi.ahk (v2 version)
WinApi List of Functions.txt ( 6433 functions in 29 dlls ).
user.ahk.api - Save in ..\SciTE\user\user.ahk.api to have CallTips and Autocompletition for all functions in SciTe and a lot of windows constants.



mapping:
WinApi supports 4 modes:
1. Enable all functions
_:=WinApi("") ;example how to load all available dlls and their functions not using any mappingMsgBox % _.GetProcAddress(_.LoadLibrary.("AutoHotkey.dll"),"ahkReady")
2. Enable only certain dll(s)
_:=WinApi("kernel32.dll") ;example how to load only required dll(s) not using any mappingMsgBox % _.GetProcAddress(_.LoadLibrary.("AutoHotkey.dll"),"ahkReady")
3. Enable only certain functions
_:=WinApi("LoadLibrary GetProcAddress") ;example how to load only required functions not using any mappingMsgBox % _.GetProcAddress(_.LoadLibrary.("AutoHotkey.dll"),"ahkReady")
4. Enable only certain functions and use abbreviation mapping.
_:=WinApi("LoadLibrary=ll GetProcAddress=gpa") ;add/load only some functions and use abbreviation mappingMsgBox % _.gpa(_.ll("AutoHotkey.dll"),"ahkdll")
You can also have a mix of 2/3/4 Add functions to existing object:
You can pass an object to WinApi so the functions will be added to your existing object.
A:={} ;here an existing object will be passed to WinApi and functions will be added.WinApi("AnimateWindow=WinAni sleep",A) ;enable only 2 function and rename AnimateWindow to WinAniGui,+LastFoundhwnd:=WinExist()Gui,Show,Hide w400 h600A.WinAni(hwnd,1000,0x20000|0x10) ;show WindowA.sleep(1000) ;sleep 1000 msA.WinAni(hwnd,1000,0x10000|0x10) ;hide WindowExitApp
Add Functions to default base:
Gui,Show,Hide w400 h600Gui,+LastFoundDoAnimation(WinExist())ExitAppDoAnimation(hwnd){ static init:=WinApi(true,"AnimateWindow=WinAni sleep") ;enable only 2 function and rename AnimateWindow to WinAni "".WinAni(hwnd,1000,0x20000|0x10) ;show Window "".sleep(1000) ;sleep 1000 ms "".WinAni(hwnd,1000,0x10000|0x10) ;hide Window}


I have not tested all functions, so please report any bugs.

Enjoy wink.png

wiseley
  • Members
  • 29 posts
  • Last active: Sep 30 2011 11:12 PM
  • Joined: 22 Apr 2009
wow, this is great :shock:
Thanks HotKeyIt

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Looks great, thx.

I just wish DynaCall or some form of it is included in AHK_L.
Posted Image

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
Thanks guys ;)

I've fixed some and added even more functions, meanwhile 7103 functions :D

Also fixed to load only required dlls and added additional parameter so you can add functions to existing object. (see examples above)

Carcophan
  • Members
  • 1578 posts
  • Last active: Nov 27 2013 06:46 PM
  • Joined: 24 Dec 2008
306 GDI32 calls. The gaming sector has some work to do!

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
Update

- Recreated all functions and corrected all parameters and return types. (6160 functions in 28 dlls)
- All functions available as Unicode and Ansi (...W ...A) are now also available without W/A suffix.
- - For example CreateFileA and CreateFileW are also available as CreateFile
- - - CreateFile will be CreateFileA in ANSI builds and CreateFileW in UNICODE builds.

Enjoy ;)

EDIT:
Added missing functions
EDIT:
Removed duplicates and fixed some incorrect parameters

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

- Renamed to WinApi()
- Fixed for 64bit
- Now available vor AutoHotkey v2 as well.
- user.ahk.api now contains a lot of windows constants as well so very simple to use in SciTe
NEW:
Default Base mode.
This way you can call the functions in any other function, regardless if it is in local static or global mode.

WinApi(true) ; will add all functions of default ahk dlls like kernel32/user32... to default base
"".LoadLibrary("AutoHotkey.dll") ; same as DllCall("LoadLibrary","Str","AutoHotkey.dll")

; Also all other modes/mapping can be used here
WinApi(true,"LoadLibrary=ll")
MsgBox % func()
func(){
   return "".ll("AutoHotkey.dll")
}



magusneo
  • Members
  • 51 posts
  • Last active: May 10 2014 03:15 PM
  • Joined: 30 May 2012
Useful,Thank you!

<!-- m -->https://ahknet.autoh... ... naCall.htm<!-- m -->
seems broken.

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
Thanks, link fixed ;)

nepter
  • Members
  • 53 posts
  • Last active: Jun 05 2019 07:59 AM
  • Joined: 29 Jun 2012

Hi, HotkeyIt. Can you add exported function of Shlwapi.dll to winapi(), or offer a method how to get paras and types of every function in dlls quickly. Thanks.



HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

Hi, HotkeyIt. Can you add exported function of Shlwapi.dll to winapi(), or offer a method how to get paras and types of every function in dlls quickly. Thanks.

Done ;)



nepter
  • Members
  • 53 posts
  • Last active: Jun 05 2019 07:59 AM
  • Joined: 29 Jun 2012

I wanna use the kind of  winap techniques to wrap COM api. I try to make a function to return an obj that wrap a interface by specified ptr and interface name.

_CreateObj(ptr,intfc){
	static _t:=["t",2,3,4,5,6,7,8,9]
	static __:={IUnknown:"QueryInterface:0:tt*|AddRef:1:|Release:2:"
	,ISequentialStream:"IUnknown\Read:3:t*uiui*|Write:4:t*uiui*"
	,IStream:"ISequentialStream\Seek:5:6ui6*|SetSize:6:6|CopyTo:7:t66*6*|Commit:8:ui|Revert:9:|LockRegion:10:66ui|UnlockRegion:11:66ui|Stat:12:tui|Clone:13:t*"}
	if !__[intfc]
		return
	if p:=InStr(__[intfc],"\")
		inh:=SubStr(__[intfc],1,p-1),obj:=_CreateObj(ptr,inh),fl:=SubStr(__[intfc],p+1)
	else
		fl:=__[intfc],obj:={}
	loop,parse,fl,|
	{
		if A_LoopField {
			v3:=""
			StringSplit,v,A_LoopField,:    ; v1:func name, v2:index, v3:def type
			_t[1]:="t" v3
			obj[v1]:=Dynacall(NumGet(NumGet(ptr+0)+v2*A_PtrSize),_t,ptr)
		}
	}
	return obj
}

example:

tagSTATSTG:="
(
  LPWSTR         pwcsName;
  DWORD          type;
  int64     cbSize;
  int64       mtime;
  int64       ctime;
  int64      atime;
  DWORD          grfMode;
  DWORD          grfLocksSupported;
  byte[4]          clsid;
  DWORD          grfStateBits;
  DWORD          reserved;
)"
STAT:=struct(tagSTATSTG)
file:="test.xml"
DllCall("Shlwapi\SHCreateStreamOnFile","str",file,"uint",0,"ptr*",pStream)
fs:=_CreateObj(pStream,"IStream")
fs.stat(stat[],0)
msgbox % "File Size: " stat.cbSize " byte"

Now, I have wrapped some interfaces, but it is a tough thing and a lot of unseen error occurs , sometimes ahk_h crashed, sometimes it run in very low speed. So I wanna know whether it is meaningful to develop it.



HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

You have to have correct amount of parameters, you can't use _t with 9 parameters for every function!

 

Here is also an example how to use the SHCreateStreamOnFile in WinApi:

WinApi(true,"shlwapi.dll")
tagSTATSTG:="LPWSTR pwcsName;DWORD type;int64 cbSize;int64 mtime;int64 ctime;int64 atime;DWORD grfMode;DWORD grfLocksSupported;byte[4] clsid;DWORD grfStateBits;DWORD reserved"
file:="test.xml"
"".SHCreateStreamOnFile(file,0,getvar(pStream:=0))
fs:=CreateObject(pStream,"IStream")
fs.stat((stat:=struct(tagSTATSTG))[],0)
msgbox % "File Size: " stat.cbSize " byte"

For AutoHotkey_H 1.1:

CreateObject(ptr,intfc){
	static __:={IUnknown:"QueryInterface:0:tt*|AddRef:1:|Release:2:"
	,ISequentialStream:"IUnknown\Read:3:t*uiui*|Write:4:t*uiui*"
	,IStream:"ISequentialStream\Seek:5:6ui6*|SetSize:6:6|CopyTo:7:t66*6*|Commit:8:ui|Revert:9:|LockRegion:10:66ui|UnlockRegion:11:66ui|Stat:12:tui|Clone:13:t*"}
	if !__[intfc]
		return
	if p:=InStr(__[intfc],"\")
		inh:=SubStr(__[intfc],1,p-1),obj:=CreateObject(ptr,inh),fl:=SubStr(__[intfc],p+1)
	else
		fl:=__[intfc],obj:={}
	loop,parse,fl,|
	{
		if A_LoopField {
			StringSplit,v,A_LoopField,:    ; v1:func name, v2:index, v3:def type
			_t:=["t" v3]
			loop % StrLen(RegExReplace(v3,"\*|u"))
				_t[A_Index+1]:=A_Index+1
			obj[v1]:=Dynacall(NumGet(NumGet(ptr+0)+v2*A_PtrSize),_t,ptr)
		}
	}
	return obj
} 

For AutoHotkey_H v2 alpha:

CreateObject(ptr,intfc){
	static __:={IUnknown:"QueryInterface:0:tt*|AddRef:1:|Release:2:"
	,ISequentialStream:"IUnknown\Read:3:t*uiui*|Write:4:t*uiui*"
	,IStream:"ISequentialStream\Seek:5:6ui6*|SetSize:6:6|CopyTo:7:t66*6*|Commit:8:ui|Revert:9:|LockRegion:10:66ui|UnlockRegion:11:66ui|Stat:12:tui|Clone:13:t*"}
	if !__[intfc]
		return
	if p:=InStr(__[intfc],"\")
		inh:=SubStr(__[intfc],1,p-1),obj:=CreateObject(ptr,inh),fl:=SubStr(__[intfc],p+1)
	else
		fl:=__[intfc],obj:={}
	loopparse,%fl%,|
	{
		if A_LoopField {
			StrSplit,v,%A_LoopField%,:    ; v1:func name, v2:index, v3:def type
			_t:=["t" v.3]
			loop % StrLen(RegExReplace(v.3,"\*|u"))
				_t[A_Index+1]:=A_Index+1
			obj[v.1]:=Dynacall(NumGet(NumGet(ptr+0)+v.2*A_PtrSize),_t,ptr)
		}
	}
	return obj
}


nepter
  • Members
  • 53 posts
  • Last active: Jun 05 2019 07:59 AM
  • Joined: 29 Jun 2012

Thanks. happy.png

 

I found 

_t:=["t",2,3,4,5,6,7,8,9]

is ok in some occasion, and sometimes not. I have not read source code of ahk_h. Maybe some error occurs because of uncertain parameters.

 

This is excellent.

loop % StrLen(RegExReplace(v3,"\*|u"))
	_t[A_Index+1]:=A_Index+1

I learn it. Thanks again.



Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Sorry to ask this, HKI, but, this syntax is quite foreign to me. Would you mind posting an example of an actual function call? Bonus points would include an example of calling ChooseFontA/W as that is what I hope to be able to do. Thanks!


Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.