编写DLL供AHK调用

学习了群主的dll教程后,感觉只有写一些测试例程才能理解dll的调用流程

测试环境

autohotkey和c必须都是32位或者64位

c代码

#include <stdlib.h>
#include <stdio.h>

_declspec(dllexport) int dlltest(int *a,char arr[20])
{
	FILE *fp;
	fp = fopen("dlldate.txt","a+");
	fprintf(fp,"test\n");
	fprintf(fp, "input a: %d\n", *a);
	fprintf(fp,"input str is %s\n",arr);
	
	strcpy(arr,"i have accept\n");

	if (NULL == fp)
	{
		fclose(fp);
	}

	return 10;
}

autohotkey代码

va1 := 10
va2 := "autohotkey"

va3 := "autohotkey"

va4 := "autohotkey"

;第一种调用方法
result := DllCall("C:\source\repos\Dll4\Release\Dll4.dll\dlltest","Int *",va1,"AStr",va2,"Cdecl ")

MsgBox,% ErrorLevel

if(ErrorLevel != 0)
{
    return
}
MsgBox,% "va2 Return is" va2
;返回值会截断,ahk是动态分配内存的,dll处理后,ahk并不知道


VarSetCapacity(va31,100)
;转化为系统码
StrPut(va3,&va31,"CP0")


result := DllCall("C:\source\repos\Dll4\Release\Dll4.dll\dlltest","Int *",va1,"Str",va31,"Cdecl ")


MsgBox,% ErrorLevel

if(ErrorLevel != 0)
{
    return
}

MsgBox,% "va31 Return is" StrGet(&va31,20,"CP0")


VarSetCapacity(va41,100)
;转化为系统码
StrPut(va4,&va41,"CP0")


result := DllCall("C:\source\repos\Dll4\Release\Dll4.dll\dlltest","Int *",va1,"Ptr",&va41,"Cdecl ")


MsgBox,% ErrorLevel

if(ErrorLevel != 0)
{
    return
}

MsgBox,% "va41 Return is" StrGet(&va41,20,"CP0")

输出结果

test
input a: 10
input str is autohotkey
test
input a: 10
input str is autohotkey
test
input a: 10
input str is autohotkey

 

给TA捐赠
共{{data.count}}人
人已捐赠
其他

c语言调用大漠插件例子

2019-11-11 0:46:24

其他

截取屏幕指定区域的图像到指定的文件demo

2019-11-13 22:37:49

2 条回复 A文章作者 M管理员
  1. AHK中文社区

    牛逼,你理解的比我深刻

个人中心
购物车
优惠劵
有新私信 私信列表
搜索