DllCall throws an error

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
T1ran1403
Posts: 44
Joined: 06 Sep 2020, 21:36

DllCall throws an error

Post by T1ran1403 » 03 Dec 2022, 12:18

Hello everyone I decided to convert the popular MCode() function to AHK v2, but as usual everything went wrong) I can't understand why the error occurs, I'm 95% sure that I rewrote everything as needed. Here is my converted code:

Code: Select all

MyFunction := MCode("2,x86:uCoAAADD,x64:uCoAAADD")
Msgbox(DllCall(MyFunction, "Cdecl"))

MCode(mcode)
{
	e := [[0x00000004], [0x00000001]]
	c := (A_PtrSize == 8) ? "x64" : "x86"

	if (!RegExMatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", &m))
	{
		return
	}
	if (!DllCall("crypt32\CryptStringToBinary", "Str", m[3], "UInt", 0, "UInt", e[m[1]][1], "Ptr", 0, "UInt*", &s := 0, "Ptr", 0, "Ptr", 0))
	{
		return
	}
	p := DllCall("GlobalAlloc", "UInt", 0, "Ptr", s, "Ptr")
	if (c == "x64")
	{
		DllCall("VirtualProtect", "Ptr", p, "Ptr", s, "UInt", 0x40, "UInt*", &op := 0)
	}
	if (DllCall("crypt32\CryptStringToBinary", "Str", m[3], "UInt", 0, "UInt", e[m[1]][1], "Ptr", 0, "UInt*", &s := 0, "Ptr", 0, "Ptr", 0))
	{
		return p
	}
	DllCall("GlobalFree", "Ptr", p)
}
And here is a working example of a function on AHK v1:

Code: Select all

MyFunction := MCode("2,x86:uCoAAADD,x64:uCoAAADD")
Msgbox % DllCall(MyFunction, "cdecl")


MCode(mcode)
{
	static e := {1:4, 2:1}, c := (A_PtrSize=8) ? "x64" : "x86"
	if (!regexmatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))
		return
	if (!DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", 0, "uint*", s, "ptr", 0, "ptr", 0))
		return
	p := DllCall("GlobalAlloc", "uint", 0, "ptr", s, "ptr")
	if (c="x64")
		DllCall("VirtualProtect", "ptr", p, "ptr", s, "uint", 0x40, "uint*", op)
	if (DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", p, "uint*", s, "ptr", 0, "ptr", 0))
		return p
	DllCall("GlobalFree", "ptr", p)
}

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: DllCall throws an error

Post by jNizM » 03 Dec 2022, 13:46

Code: Select all

if (DllCall("crypt32\CryptStringToBinary", "Str", m[3], "UInt", 0, "UInt", e[m[1]][1], "Ptr", p, "UInt*", s, "Ptr", 0, "Ptr", 0))
{
	return p
}
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

T1ran1403
Posts: 44
Joined: 06 Sep 2020, 21:36

Re: DllCall throws an error

Post by T1ran1403 » 03 Dec 2022, 14:20

@jNizM
Thank you! Have a good day!

Post Reply

Return to “Ask for Help (v2)”