autoit之ObjCreateInterface使用方法

先看au3帮助文档的介绍:

ObjCreateInterface

Creates a reference to an object from the given classname/object pointer, interface identifier and description string.

ObjCreateInterface ( "CLSID" , "IID" [,"interface_description", ["flag"]] )

参数

CLSIDClass identifier or object pointer. If this is a class identifier it can be in either ProgID or the string representation of the CLSID.
IIDString representation of interface identifier.
interface_description[可选参数] String describing v-table of the object. Use keyword Default to access IDispatch for dual interfaces.
flag[可选参数] Default value is True meaning the object interface inherits from IUnknown.

返回值

Success:Returns an object.
Failure:Returns 0 and sets @error to non-zero.

注意/说明

ObjCreateInterface creates objects with methods that are listed in inerface-description string.
For COM objects (IUnknown based) first three methods are always QueryInterface, AddRef and Release. Don't specify them inside the description strings.

Methods listed inside description string must be in v-table order of the interface. If you provide an invalid or incorrect description AutoIt may become unstable or crash.

Format of the description string is:
$sTagInterface = "MethodName1 RetType(ParamType1;ParamType2;...); MethodName2 RetType(..."

By default, AutoIt uses the 'stdcall' calling convention for COM. To use the 'cdecl' place ':cdecl' after the return type.

Valid Types are:

TypeDetails
noneno value (only valid for return type - equivalent to void in C)
bytean unsigned 8 bit integer
booleanan unsigned 8 bit integer
shorta 16 bit integer
word, ushortan unsigned 16 bit integer
int, longa 32 bit integer
boola 32 bit integer
dword, ulong, uintan unsigned 32 bit integer
hresulta 32 bit integer
int64a 64 bit integer
uint64an unsigned 64 bit integer
ptra general pointer (void *)
hwnda window handle (pointer)
handlean handle (pointer)
floata single precision floating point number
doublea double precision floating point number
int_ptr, long_ptr, lresult, lparaman integer big enough to hold a pointer when running on x86 or x64 versions of AutoIt.
uint_ptr, ulong_ptr, dword_ptr, wparaman unsigned integer big enough to hold a pointer when running on x86 or x64 versions of AutoIt.
stran ANSI string (a minimum of 65536 chars is allocated).
wstra UNICODE wide character string (a minimum of 65536 chars is allocated).
bstra composite data type that consists of a length prefix, a data string and a terminator
varianta tagged union that can be used to represent any other data type
idispatch, objecta composite data type that represents object with IDispatch interface
clsid128-bit integer in form of GUID string
structstructure created with DllStructCreate()
*Add * to the end of another type to pass it by reference. For example "int*" passes a pointer to an "int" type.

根据以上说明,我们知道ObjCreateInterface是创建com接口的,创建com接口还有个命令是ObjCreate。

我们以防火墙操作为例:

以下代码可以获取防火墙信息,ObjCreate创建的com接口,可以直接调用接口方法,无须声明

Local $oFireWall = ObjCreate("HNetCfg.FwPolicy2")
ConsoleWrite($oFireWall.CurrentProfileTypes & @CRLF)
ConsoleWrite($oFireWall.FirewallEnabled($oFireWall.CurrentProfileTypes) & @CRLF)

如果使用 ObjCreateInterface ,则需要注意:

1.如果接口是继承自IUnknown,IUnknown有3个方法接口:

virtual HRESULT STDMETHODCALLTYPE QueryInterface( REFIID riid, _COM_Outptr_ void __RPC_FAR *__RPC_FAR *ppvObject);
virtual ULONG STDMETHODCALLTYPE AddRef( void);
virtual ULONG STDMETHODCALLTYPE Release( void);

说明中明确提到,不用声明:For COM objects (IUnknown based) first three methods are always QueryInterface, AddRef and Release. Don't specify them inside the description strings.

例如下面这个接口:

Local Const $sCLSID_TaskbarList = "{56FDF344-FD6D-11D0-958A-006097C9A090}"
Local Const $sIID_ITaskbarList = "{56FDF342-FD6D-11D0-958A-006097C9A090}"
Local Const $sTagITaskbarList = "HrInit hresult(); AddTab hresult(hwnd); DeleteTab hresult(hwnd); ActivateTab hresult(hwnd); SetActiveAlt hresult(hwnd);"
Local $oTaskbarList = ObjCreateInterface($sCLSID_TaskbarList, $sIID_ITaskbarList, $sTagITaskbarList)
$oTaskbarList.HrInit()
Run("notepad.exe")
Local $hNotepad = WinWait("[CLASS:Notepad]")
MsgBox($MB_SYSTEMMODAL, "", "Look in the Taskbar and you should see an entry for Notepad." & @CRLF & @CRLF & "Press OK to continue.")
$oTaskbarList.DeleteTab($hNotepad)
MsgBox($MB_SYSTEMMODAL, "", "Look in the Taskbar.  There should no longer be a Notepad entry but Notepad is still running." & @CRLF & @CRLF & "Press OK to continue.")
WinClose($hNotepad)

2.如果接口是继承自IDispatchIDispatch有4个方法接口:

virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount( __RPC__out UINT *pctinfo) ;
virtual HRESULT STDMETHODCALLTYPE GetTypeInfo( UINT iTInfo,LCID lcid,__RPC__deref_out_opt ITypeInfo **ppTInfo) ;    
virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames( __RPC__in REFIID riid,__RPC__in_ecount_full(cNames) LPOLESTR *rgszNames, __RPC__in_range(0,16384) UINT cNames, LCID lcid, __RPC__out_ecount_full(cNames) DISPID *rgDispId) ;
virtual HRESULT STDMETHODCALLTYPE Invoke(_In_  DISPID dispIdMember,_In_  REFIID riid, _In_  LCID lcid,_In_  WORD wFlags,_In_  DISPPARAMS *pDispParams,_Out_opt_  VARIANT *pVarResult,_Out_opt_  EXCEPINFO *pExcepInfo,_Out_opt_  UINT *puArgErr);

则需要声明这4个方法。或者直接把第三个参数设置为Default,就可以直接使用接口方法。

例如防火墙接口:

Local Const $CLSID_NetFwPolicy2 = "{E2B3C97F-6AE1-41AC-817A-F6F92166D7DD}"
Local Const $IID_INetFwPolicy2 = "{98325047-C671-4174-8D81-DEFCD3F03186}"
;第三个参数设置为Default
Local $pNetFwPolicy2 = ObjCreateInterface($CLSID_NetFwPolicy2, $IID_INetFwPolicy2, Default)
ConsoleWrite($pNetFwPolicy2.CurrentProfileTypes & @CRLF)
ConsoleWrite($pNetFwPolicy2.FirewallEnabled($pNetFwPolicy2.CurrentProfileTypes) & @CRLF)

;声明4个方法接口:
Local Const $TaskList = 'GetTypeInfoCount HRESULT(UINT*);GetTypeInfo HRESULT(UINT,DWORD,ptr);GetIDsOfNames HRESULT(ptr,ptr,UINT,DWORD,ptr);Invoke HRESULT(LONG,ptr,DWORD,WORD,ptr,ptr,ptr,ptr);' & _ 
'get_CurrentProfileTypes HRESULT(long*);' & _
'get_FirewallEnabled HRESULT(long;BOOL*);'
Local $pNetFwPolicy2 = ObjCreateInterface($CLSID_NetFwPolicy2, $IID_INetFwPolicy2, $TaskList)
Local $profileType = 0
Local $Ret = $pNetFwPolicy2.get_CurrentProfileTypes($profileType)
ConsoleWrite('$profileType=' & $profileType & @CRLF)
Local $enabled = 0
Local $Ret = $pNetFwPolicy2.get_FirewallEnabled($profileType, $enabled)
ConsoleWrite('$enabled=' & $enabled & @CRLF)

有关方法接口的定义,在介绍中有说明:$sTagInterface = "MethodName1 RetType(ParamType1;ParamType2;...); MethodName2 RetType(..."

方法名 返回值类型(参数1类型;参数2类型;...;参数n类型)

注意C++原型中的self指针无须定义。

打赏

本篇文章链接 地址:https://www.wmzos.com/?id=163

相关阅读

添加新评论