雷赛运动控制卡H头文件内api函数转换到aardio函数声明转换器(源码)

By admin at 2019-09-18 • 0人收藏 • 1628人看过

话说,我们用到过多种运动控制卡, 一般都会提供 h头文件, 我不知道aardio里怎么直接调用这个H头文件当aar的api声明, 所以写了这个自动转换工具把dll里面的函数转换到aar里去.

注: aar里其实不赞成把dll内的所有函数都声明调用, 这样浪费时间和资源, aar本来就可以不声明就调用dll内的函数, 除极个别的一些参数类型需要声明之外.

这个工具,不局限于雷赛控制卡的h头文件, 我测试过,其他控制卡也可以


代码改自 aardio内置的api函数转换器, 感谢一鹤jacen提供.


blob.png

import win.ui;
/*DSG{{*/
mainForm = win.form(text="雷赛运动控制卡H头文件函数转换器";right=950;bottom=469;border="dialog frame")
mainForm.add(
button={cls="button";text='加载文件\u2191';left=5;top=208;right=184;bottom=268;z=3};
button2={cls="button";text='转换\u2193';left=561;top=199;right=740;bottom=259;z=4};
button3={cls="button";text="保存文件";left=768;top=199;right=947;bottom=259;z=5};
edit={cls="edit";left=0;top=0;right=951;bottom=155;edge=1;multiline=1;vscroll=1;z=1};
edit2={cls="edit";left=0;top=315;right=951;bottom=470;edge=1;multiline=1;vscroll=1;z=2};
rowStartStr={cls="edit";text="DMC2410_API";left=333;top=201;right=535;bottom=237;align="center";edge=1;font=LOGFONT(h=-20);multiline=1;z=6};
static={cls="static";text="函数行开始字符:";left=192;top=204;right=323;bottom=233;align="right";font=LOGFONT(h=-16;weight=700);notify=1;transparent=1;z=8};
static2={cls="static";text="调用约定字符:";left=210;top=248;right=324;bottom=277;align="right";font=LOGFONT(h=-16;weight=700);notify=1;transparent=1;z=9};
调用约定字符={cls="edit";text="__stdcall";left=332;top=246;right=534;bottom=282;align="center";edge=1;font=LOGFONT(h=-20);multiline=1;z=7}
)
/*}}*/

import fsys.dlg;
import string.list;
var filecontent,retcontent;

/*替换变量集{{*/
var byval=/*
CHAR=BYTE
CCHAR=BYTE
PSZ=string
PCHAR=string
PSTR=string
LPSTR=string
NPSTR=string
PCSTR=string
LPCSTR=string
WCHAR=WORD
PWCH=string
PWCHAR=ustring
PWSTR=ustring
LPWSTR=ustring
NWPSTR=ustring
PCWCH=ustring
LPCWCH=ustring
PCWSTR=ustring
LPCWSTR=ustring
TCHAR=word
PTCHAR=ustring
PTCH=string
LPTCH=string
PTSTR=ustring
LPTSTR=ustring
PCTSTR=ustring
LPCTSTR=ustring
UCHAR=BYTE
PUCHAR=string
BSTR=ptr
OLECHAR=word
LPOLESTR=ustring
LPCOLESTR=ustring
BYTE=BYTE
PBYTE=ptr
LPBYTE=ptr
TBYTE=word
PTBYTE=ustring
BOOL=bool
BOOLEAN=BYTE
SHORT=word
USHORT=WORD
INT32=int
INT64=long
INT_PTR=addr
UINT=INT
UINT32=INT
UINT64=LONG
UINT_PTR=INT
LONG=int
LONG32=int
LONG64=long
LONGLONG=long
__int64=long
LONG_PTR=addr
ULONG=INT
ULONG32=INT
ULONG64=LONG
ULONGLONG=LONG
ULONG_PTR=ADDR
DWORD32=INT
DWORD64=long
DWORD_PTR=INT
PDWORD=INT
FLOAT=float
GLOBALHANDLE=pointer
HDC=ptr
HBITMAP=ptr
HBRUSH=ptr
HICON=ptr
HCURSOR=ptr
HPALETTE=ptr
HPEN=ptr
HCONV=ptr
HACCEL=ptr
HCONVLIST=ptr
HDDEDATA=ptr
HDESK=ptr
HDLG=pointer
HDROP=ptr
HDWP=ptr
HENHMETAFILE=ptr
HFILE=int
HFONT=ptr
HGDIOBJ=ptr
HGLOBAL=ptr
HHOOK=ptr
HIMAGELIST=pointer
HIMC=pointer
HINSTANCE=ptr
HKEY=ptr
HKL=ptr
HLOCAL=ptr
HMEMU=pointer
HMETAFILE=ptr
HMIDIIN=pointer
HMIDIOUT=pointer
HMMIO=pointer
HMODULE=ptr
HMONITOR=ptr
HRGN=ptr
HRSRC=ptr
HSZ=ptr
HWAVEIN=pointer
HWAVEOUT=pointer
HWINSTA=ptr
HWND=HWND
LOCALHANDLE=pointer
SC_HANDLE=ptr
SC_LOCK=ptr
SERVICE_STATUS_HANDLE=ptr
MFENUMPROC=pointer
ENUMFENUMPROC=pointer
ENUMRESLANGPROC=pointer
ENUMRESNAMEPROC=pointer
ENUMRESTYPEPROC=pointer
FONTENUMPROC=pointer
GOBJENUMPROC=pointer
HOOKPROC=pointer
LPCCHOOKPROC=pointer
LPCFHOOKPROC=pointer
LPFRHOOKPROC=pointer
LPOFNHOOKPROC=pointer
LPPRINTHOOKPROC=pointer
LPSETUPHOOKPROC=pointer
FARPROC=pointer
DLGPROC=pointer
LINEDDAPROC=pointer
LOHANDLER_FUNCTION=pointer
PROPENUMPROC=pointer
PFNCALLBACK=pointer
PHANDLER_ROUTINE=pointer
TIMERPROC=pointer
WNDENUMPROC=pointer
WNDPRO=pointer
YIELDPROC=pointer
ATOM=WORD
LPRESULT=int
LRESULT=addr
COLORREF=INT
WPARAM=ADDR
LPARAM=addr
CRITICAL_SECTION=struct
LANGID=WORD
LCID=INT
LCTYPE=INT
MCIDEVICEID=INT
MMRESULT=INT
PCRITICAL_SECTION=pointer
PHKEY=pointer
POINTER_32=int
POINTER_64=long
REGSAM=INT
SIZE_T=ADDR
SSIZE_T=addr
UNSIGNED=INT
LUID=struct
VOID=void
PVOID=ptr
LPVOID=ptr
LPCVOID=pointer
DWORD=INT
DWORDLONG=INT
HALF_PTR=ptr
HANDLE=ptr
HGIDOBJ=ptr
HMENU=ptr
HRESULT=int
INT=int
LARGE_INTEGER=long
LGRPID=INT
POINTER_SIGNED=ptr
POINTER_UNSIGNED=ADDR
PUHALF_PTR=ptr
UHALF_PTR=ptr
ULARGE_INTEGER=LONG
USN=long
WORD=WORD
*/

var byref=/*
LPWORD=WORD
LPBOOL=bool
LPCOLORREF=INT
LPCRECT=struct
LPCRECTL=struct
PLUID=struct
PLCID=INT
LPCOLORREF=INT
LPCRITICAL_SECTION=struct
LPHWAVEIN=pointer
LPHWAVEOUT=pointer
PFLOAT=float
HANDLE=pointer
PHANDLE=ptr
SPHANDLE=pointer
LPHANDLE=ptr
PLONG=LONG
LPLONG=long
PULONG=INT
PULONGLONG=LONG
PWORD=WORD
LPWORD=WORD
LPDWORD=INT
PUINT=INT
PINT=int
LPINT=int
PLONGLONG=long
PBOOL=bool
LPBOOL=int
PSHORT=word
PUSHORT=WORD
PBOOLEAN=BYTE
PDWORD=INT
PDWORDLONG=LONG
PDWORD_PTR=INT
PDWORD32=INT
PDWORD64=long
PHALF_PTR=ptr
PHKEY=ptr
PINT_PTR=INT_PTR
PINT32=int
PINT64=long
PLONG_PTR=addr
PLONG32=long
PLONG64=long
PSIZE_T=ADDR
PSSIZE_T=addr
PUCHAR=BYTE
PUINT_PTR=INT
PUINT32=INT
PUINT64=LONG
PULARGE_INTEGER=LONG
PULONG_PTR=ADDR
PULONG32=INT
PULONG64=LONG
*/
	
	var tbyval = string.list(byval)
	var tbyref = string.list(byref)
	
	c2a = function(str){ 
		str = string.replace(str,"(\W)const\s*SIZE\s*\*", "\1struct ") 
		str = string.replace(str,"(\W)const\s*POINT\s*\*", "\1struct ") 
		str = string.replace(str,"(\W)const\s*RECT\s*\*", "\1struct ") 
		str = string.replace(str,"(\W)__in_opt\s*SIZE\s*\*", "\1struct ") 
		str = string.replace(str,"(\W)__in_opt\s*POINT\s*\*", "\1struct ") 
		str = string.replace(str,"(\W)__in_opt\s*RECT\s*\*", "\1struct ") 
		str = string.replace(str,"(\W)_In_opt_\s*SIZE\s*\*", "\1struct ") 
		str = string.replace(str,"(\W)_In_opt_\s*POINT\s*\*", "\1struct ") 
		str = string.replace(str,"(\W)_In_opt_\s*RECT\s*\*", "\1struct ") 
		str = string.replace(str,"(\W)_In_\s*SIZE\s*\*", "\1struct ") 
		str = string.replace(str,"(\W)_In_\s*POINT\s*\*", "\1struct ") 
		str = string.replace(str,"(\W)_In_\s*RECT\s*\*", "\1struct ") 
		str = string.replace(str,"(\W)__out_opt\s*SIZE\s*\*", "\1struct &") 
		str = string.replace(str,"(\W)__out_opt\s*POINT\s*\*", "\1struct &") 
		str = string.replace(str,"(\W)__out_opt\s*RECT\s*\*", "\1struct &") 
		str = string.replace(str,"(\W)_Out_opt_\s*SIZE\s*\*", "\1struct &") 
		str = string.replace(str,"(\W)_Out_opt_\s*POINT\s*\*", "\1struct &") 
		str = string.replace(str,"(\W)_Out_opt_\s*RECT\s*\*", "\1struct &") 
		str = string.replace(str,"(\W)__out\s*SIZE\s*\*", "\1struct &") 
		str = string.replace(str,"(\W)__out\s*POINT\s*\*", "\1struct &") 
		str = string.replace(str,"(\W)__out\s*RECT\s*\*", "\1struct &") 
		str = string.replace(str,"(\W)_Out_\s*SIZE\s*\*", "\1struct &") 
		str = string.replace(str,"(\W)_Out_\s*POINT\s*\*", "\1struct &") 
		str = string.replace(str,"(\W)_Out_\s*RECT\s*\*", "\1struct &") 
		str = string.replace(str,"(\W)__inout\s+", "\1")  
		str = string.replace(str,"(\W)__out_opt\s+", "\1") 
		str = string.replace(str,"(\W)__out\s+", "\1")  
		str = string.replace(str,"(\W)_Out_opt_\s+", "\1") 
		str = string.replace(str,"(\W)_Out_\s+", "\1")  
		str = string.replace(str,"(\W)__in_opt\s+", "\1") 
		str = string.replace(str,"(\W)__in\s+", "\1")  
		str = string.replace(str,"(\W)_In_opt_\s+", "\1") 
		str = string.replace(str,"(\W)_In_\s+", "\1")  
		str = string.replace(str,".+?(%\{\})\s*([\w_]+).+", "class \2 \1") 
		str = string.replace(str,"(\W)SIZE\s*\*", "\1struct &") 
		str = string.replace(str,"(\W)POINT\s*\*", "\1struct &") 
		str = string.replace(str,"(\W)RECT\s*\*", "\1struct &") 
		str = string.replace(str,"(\W)const\s*[A-Z_]{7,}\s*\*", "\1struct ") 
		str = string.replace(str,"(\W)[A-Z_]{7,}\s*\*", "\1struct&") 
 		str = string.replace(str,"(\W)unsigned\s*long\s*long(\W)", "\1LONG\2")
		str = string.replace(str,"(\W)long\s*long(\W)", "\1long\2")
		str = string.replace(str,"(\W)unsigned\s*__int64(\W)", "\1LONG\2")
		str = string.replace(str,"(\W)__int64(\W)", "\1long\2")
 
 		str = string.replace(str,"(\W)HDC(\W)", "\1pointer\2") 
 		str = string.replace(str,"(\W)POINT(\W)", "\1struct\2") 
 		str = string.replace(str,"(\W)SIZE(\W)", "\1struct\2") 
 		str = string.replace(str,"(\W)HDC(\W)", "\1pointer\2") 
 		str = string.replace(str,"(\W)HDC(\W)", "\1pointer\2") 
 		str = string.replace(str,"(\W)HDC(\W)", "\1pointer\2") 
 		str = string.replace(str,"(\W)HDC(\W)", "\1pointer\2") 
 		str = string.replace(str,"(\W)HMENU(\W)", "\1pointer\2") 
		str = string.replace(str,"(\W)INT(\W)", "\1int\2") 
		str = string.replace(str,"(\W)DWORD(\W)", "\1INT\2") 
		str = string.replace(str,"(\W)ULONG(\W)", "\1INT\2") 
		str = string.replace(str,"(\W)UINT(\W)", "\1INT\2") 
		str = string.replace(str,"(\W)unsigned\s*long(\W)", "\1INT\2")
		str = string.replace(str,"(\W)unsigned\s*int(\W)", "\1INT\2")
		
		str = string.replace(str,"(\W)FLOAT(\W)", "\1INT\2") 
		str = string.replace(str,"(\W)PVOID(\W)", "\1pointer\2") 
		str = string.replace(str,"(\W)LPVOID(\W)", "\1pointer\2") 
		str = string.replace(str,"(\W)LPCVOID(\W)", "\1pointer\2") 
		str = string.replace(str,"void\s*\*\s*", "pointer ") 
		
		
		str = string.replace(str,"(\W)BOOL(\W)", "\1int\2") 
		str = string.replace(str,"(\W)long(\W)", "\1int\2") 
		str = string.replace(str,"(\W)LRESULT(\W)", "\1addr\2") 
		str = string.replace(str,"(\W)LPARAM(\W)", "\1addr\2") 
		str = string.replace(str,"(\W)WPARAM(\W)", "\1ADDR\2") 
		str = string.replace(str,"(\W)HWND(\W)", "\1addr\2") 
		str = string.replace(str,"(\W)HINSTANCE(\W)", "\1pointer\2") 
		str = string.replace(str,"(\W)HKEY(\W)", "\1pointer\2") 
		str = string.replace(str,"(\W)HANDLE(\W)", "\1pointer\2") 
		str = string.replace(str,"\n\N*#\N+", "")  
		
		str = string.replace(str,"(\W)LPCSTR(\W)", "\1string\2")  
		str = string.replace(str,"(\W)LPCTSTR(\W)", "\1string\2")  
		str = string.replace(str,"(\W)LPSTR(\W)", "\1string\2") 
		str = string.replace(str,"(\W)LPTSTR(\W)", "\1string\2") 
		str = string.replace(str,"(\W)const\s*RECT\s*\*", "\1struct ") 
		str = string.replace(str,"(\W)const\s*char\s*\*", "\1string ")  
		str = string.replace(str,"(\W)const\s*unsigned\s*\char\s*\*(\W)", "\1string\2") 
		str = string.replace(str,"(\W)char\s*\*(\W)", "\1string\2")  
		str = string.replace(str,"(\W)unsigned\s*\char\s*\*(\W)", "\1string\2")  
		str = string.replace(str,"(\W)BYTE\s*\*(\W)", "\1string\2")  
		
		str = string.replace(str,"(\W)unsigned short(\W)", "\1WORD\2") 
		str = string.replace(str,"(\W)USHORT(\W)", "\1WORD\2")
		str = string.replace(str,"(\W)SHORT(\W)", "\1WORD\2") 
		str = string.replace(str,"(\W)short(\W)", "\1word\2")  
		
		str = string.replace(str,"(\W)char(\W)", "\1byte\2") 
		
		
		str = string.replace(str,"union[^\w_]+(%{}?)\s*([\w_]+)", "union \2=\1")  
		str = string.replace(str," +", " ")  
		
		
		str = string.replace(str,"(\W)LPCSTR(\W)", "\1string\2")  
		str = string.replace(str,"(\W)LPCSTR(\W)", "\1string\2")  
		str = string.replace(str,"(\W)LPCSTR(\W)", "\1string\2")  
	
    	//下面这句一定要放在最后
    	str = string.replace(str,"(\W)unsigned(\W)", "\1INT\2")
    	return str;
	}
	var dll = "dll"
	api2aau = function(code){
		var converApi = function(str){
			for(k,v in tbyval){
   				if(! tonumber(k) ){
   					str = ..string.replace(str,"(\W)"+k+"(\W)", "\1"+v+"\2")  
   				}
			}
			for(k,v in tbyref){
   				if(! tonumber(k) ){
   					str = ..string.replace(str,"(\W)"+k+"(\W)", "\1"+v+"&\2") 
   				}
			}
			str = ..string.replace(str,"\*","&")
			return str;
		}
	
		code = string.replace(code,"(\W)WINAPI(\W)", "\1\2") 
		code = string.replace(code,"(\W)__in(\W)", "\1\2") 
		code = ..string.replace(code,"([\w_]+)\s+([\w_]+)\s*(%())",function(c,c2,c3){
			if(::User32[c2]) dll = "::User32"
			if(::Kernel32[c2]) dll = "::Kernel32"
			return ..string.format('%s= %s.api("%s","%s")',  c2, dll, c2, converApi(c+c3) );
		} )
		return code;
	}
	
	struct2aau = function(str){
		str =" " + str; //简化(\W)\w+(\W)
		
		str = c2a(str);
		str = api2aau(str);
		str = string.replace(str,"(\W)\w+\s*\*", "\1pointer ") 
		
		for(k,v in tbyval){
   			if(! tonumber(k) ){
   				str = ..string.replace(str,"(\W)"+k+"(\W)", "\1"+v+"\2") 
   			}
		}
		for(k,v in tbyref){
   			if(! tonumber(k) ){
   				str = ..string.replace(str,"(\W)"+k+"(\W)", "\1pointer\2") 
   			}
		}
		
		str = string.trimleft(str);//去掉函数在左侧补偿的空格 
		return str;
	} 
	
/*}}*/

mainForm.button.oncommand = function(id,event){
	var path = fsys.dlg.open("所有文件|*.*|",,,mainForm.hwnd);
	if(path){
		//防止乱码
		filecontent = string.fromto(string.load(path),0,65001);
		mainForm.edit.text = filecontent;
	}
}

mainForm.button2.oncommand = function(id,event){
	retcontent = null;
	var temp;
	//以换行回车符进行分割
	var linestr = string.split(filecontent,'<\r\n>')
	for(i=1;#linestr;1){
		//判断需要转换的函数开头,只转换这些行
		if(string.startWith(linestr[i],mainForm.rowStartStr.text:"DMC2410_API",true)){
			//替换为空
			temp = string.replace(linestr[i],mainForm.rowStartStr.text:"DMC2410_API","");
			temp = string.replace(temp,mainForm.调用约定字符.text:"__stdcall","");
			temp = struct2aau(temp);
			retcontent = string.concat(retcontent,temp,'\r\n');
		}
	
	}
	mainForm.edit2.text = retcontent;
	
}

mainForm.button3.oncommand = function(id,event){
	var path = fsys.dlg.save("*.txt|*.txt|");
	if(path){
		var ret = string.save(path,retcontent);
   		if(ret){
   				mainForm.msgbox("保存成功!")
   		}else {
   				mainForm.msgbox("保存失败!")
   		}	
	}
}


mainForm.enableDpiScaling();
mainForm.show();

return win.loopMessage();


登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...