Jump to content

call methods of classes And Interface


wolf9228
 Share

Recommended Posts

By the project you can call methods of classes And Interface through DllCallAddress function

I think the project is important for all ... Thanks

New All project files

InterfaceCall.zip

InterfaceCall.au3

Func InterfaceCall($Inface,$ReturnType,$MethodNum,$Type1 = 0,$Param1 = 0,$Type2 = 0,$Param2 = 0 _
,$Type3 = 0,$Param3 = 0, $Type4 = 0,$Param4 = 0 ,$Type5 = 0 ,$Param5 = 0,$Type6 = 0,$Param6 = 0 _
,$Type7 = 0,$Param7 = 0, $Type8 = 0,$Param8 = 0,$Type9 = 0,$Param9 = 0,$Type10 = 0,$Param10 = 0 _
,$Type11= 0,$Param11= 0,$Type12= 0,$Param12= 0,$Type13= 0,$Param13 = 0,$Type14 = 0,$Param14 = 0 _
,$Type15= 0,$Param15= 0,$Type16= 0,$Param16= 0,$Type17= 0,$Param17 = 0,$Type18 = 0,$Param18 = 0 _
,$Type19= 0,$Param19= 0,$Type20= 0,$Param20= 0,$Type21= 0,$Param21 = 0,$Type22 = 0,$Param22 = 0 _
,$Type23= 0,$Param23= 0,$Type24= 0,$Param24= 0,$Type25= 0,$Param25 = 0,$Type26 = 0,$Param26 = 0 _
,$Type27= 0,$Param27= 0,$Type28= 0,$Param28= 0,$Type29= 0,$Param29 = 0,$Type30 = 0,$Param30 = 0)
;;Return Array Of DllCallAddress
;Only Call Virtual Method Form any Class Or From any Interface
;$MethodNum ==> Virtual Method Number In (Virtual Methods Table) ;See the examples
;-------------------------------------------------------------------;C++ Example1
;C++ Example1
;class iClass
;{
;public:
;void MethodA()
;{
;MessageBox(0,"MethodA","MSG",0);
;}
;virtual void VirtualMethodB()
;{
;MessageBox(0,"VirtualMethodB","MSG",0);
;}
;virtual void VirtualMethodC()
;{
;MessageBox(0,"VirtualMethodC","MSG",0);
;}
;void MethodB()
;{
;MessageBox(0,"MethodB","MSG",0);
;}
;virtual void VirtualMethodA()
;{
;MessageBox(0,"VirtualMethodA","MSG",0);
;}
;};
;-----------------------------------------------------------;Virtual Methods Table
;Virtual Methods Table
;virtual void VirtualMethodB() ==> Virtual Method Number Is 1
;virtual void VirtualMethodC() ==> Virtual Method Number Is 2
;virtual void VirtualMethodA() ==> Virtual Method Number Is 3
;-----------------------------------------------------------;Virtual Methods Table
;-------------------------------------------------------------------;C++ Example1

;////////////////////////////////////////////////////////////////////////////////

;-------------------------------------------------------------------;C++ Example2
;C++ Example2
;class iClassA
;{
;public:
;void MethodA()
;{
;MessageBox(0,"MethodA","MSG",0);
;}
;virtual void VirtualMethodB()
;{
;MessageBox(0,"VirtualMethodB","MSG",0);
;}
;virtual void VirtualMethodC()
;{
;MessageBox(0,"VirtualMethodC","MSG",0);
;}
;};

;class iClassB : public iClassA
;{
;public:
;void MethodD()
;{
;MessageBox(0,"MethodD","MSG",0);
;}
;virtual void VirtualMethodF()
;{
;MessageBox(0,"VirtualMethodF","MSG",0);
;}
;virtual void VirtualMethodE()
;{
;MessageBox(0,"VirtualMethodE","MSG",0);
;}
;};
;////////////////////////////////////////////////////////////////////////////////
;////////////////////////////////////////////////////////////////////////////////
;------------------------------------------;Virtual Methods Table Of iClassA Class
;Virtual Methods Table Of iClassA Class
;virtual void VirtualMethodB() ==> Virtual Method Number Is 1
;virtual void VirtualMethodC() ==> Virtual Method Number Is 2
;------------------------------------------;Virtual Methods Table Of iClassA Class
;////////////////////////////////////////////////////////////////////////////////
;///////////////////////////////////////////////////////////////////////////////
;------------------------------------------;Virtual Methods Table Of iClassB Class
;class iClassB : public iClassA
;base class ==> iClassA
;derived class ==> iClassB
;http://msdn.microsoft.com/en-us/library/hzk8a7d3.aspx
;When preceding the name of a base class, the public keyword specifies that the public
;and protected members of the base class are public and protected members, respectively,
;of the derived class.
;-------------------------------------------------------------------------------------
;Virtual Methods Table Of iClassB Class
;virtual void VirtualMethodB() In (iClassA) ==> Virtual Method Number Is 1
;virtual void VirtualMethodC() In (iClassA) ==> Virtual Method Number Is 2
;virtual void VirtualMethodF() In (iClassB) ==> Virtual Method Number Is 3
;virtual void VirtualMethodE() In (iClassB) ==> Virtual Method Number Is 4
;------------------------------------------;Virtual Methods Table Of iClassB Class
;-------------------------------------------------------------------;C++ Example2
;////////////////////////////////////////////////////////////////////////////////
if Not IsPtr($Inface) Or ($MethodNum < 1) Then Return SetError(1,0,0)
if (@NumParams > 3) And (Mod((@NumParams - 3),2) <> 0) Then Return SetError(2,0,0)
Local $iMethAddress = GetMethodAddress($Inface,$MethodNum)
if Not ($iMethAddress) Then Return SetError(3,0,0)
Local $iDataType = "",$iFuncParam = "",$iCommand = "",$iReturn = 0
;Why use Inface Param In DllCallAddress Function Because the Function of the method
;starts from the (Interface Or class)
;See here
;int class::MethodFunction( int Param ){return 0;};
$iCommand = 'DllCallAddress("' & $ReturnType & '",Eval("iMethAddress"),"ptr",Eval("Inface"),'
For $i = 1 To ((@NumParams - 3) / 2)
$iDataType = Eval("Type" & $i)
$iCommand &= '"' & $iDataType & '",'
$iFuncParam = 'Eval("Param' & $i & '"),'
$iCommand &= $iFuncParam
Next
$iCommand = StringTrimRight($iCommand,1)
$iCommand &= ")"
$iReturn = Execute($iCommand)
if @error Then Return SetError(4,0,0)
Local $nReturn[UBound($iReturn) -1] , $j = 0
For $i = 0 To UBound($iReturn) - 1
if ($i = 1) Then ContinueLoop ;Skip $Inface Element
$nReturn[$j] = $iReturn[$i]
$j += 1
Next
Return SetError(0,0,$nReturn)
EndFunc

Func GetMethodAddress($Inface,$MethodNum)
;$MethodNum ==> Virtual Method Number In (Virtual Methods Table)
Local $SizeOfUlong_Ptr = 4,$iMethAddress = 0,$OutCastStruct1 = 0
Local $OutCast1 = 0 , $OutCastStruct2 = 0
if Not IsPtr($Inface) Or ($MethodNum < 1) Then Return SetError(1,0,0)
;-------------------------------------------------------
$OutCastStruct1 = DllStructCreate("ULONG_PTR",$Inface)
$OutCast1 = DllStructGetData($OutCastStruct1,1)
;In C++ ==> unsigned long** OutCast1 = *(unsigned long***)Inface;
;--------------------------------------------------------
;-------------------------------------------------------
$OutCastStruct2 = DllStructCreate("ULONG_PTR",$OutCast1 + ($SizeOfUlong_Ptr * ($MethodNum - 1)))
$iMethAddress = DllStructGetData($OutCastStruct2,1)
;$OutCast1 + ($SizeOfUlong_Ptr * ($MethodNum - 1)) ==> $OutCast1 Is PTR Array Of Virtual Methods Table // Method PTR = Array[MethodNum - 1]
;In C++ ==> unsigned long* iMethAddress = *(unsigned long**)((BYTE*)OutCast1 + (SizeOfUlong_Ptr * (MethodNum - 1)));
;Or In C++ ==> unsigned long* iMethAddress = OutCast1[MethodNum - 1];
;--------------------------------------------------------
if (IsBadCodePtr($iMethAddress)) Then Return SetError(2,0,0)
Return SetError(0,0,$iMethAddress)
EndFunc

Func GetCount_Of_VirtualMethods($Inface)
Local $SizeOfUlong_Ptr = 4,$iMethAddress = 0,$OutCastStruct1 = 0
Local $OutCast1 = 0 , $OutCastStruct2 = 0 , $MethodNum = 1
if Not IsPtr($Inface) Then Return SetError(1,0,0)
;-------------------------------------------------------
$OutCastStruct1 = DllStructCreate("ULONG_PTR",$Inface)
$OutCast1 = DllStructGetData($OutCastStruct1,1)
;In C++ ==> unsigned long** OutCast1 = *(unsigned long***)Inface;
;--------------------------------------------------------
While 1
;-------------------------------------------------------
$OutCastStruct2 = DllStructCreate("ULONG_PTR",DllStructGetData($OutCastStruct1,1) + ($SizeOfUlong_Ptr * ($MethodNum - 1)))
$iMethAddress = DllStructGetData($OutCastStruct2,1)
;$OutCast1 + ($SizeOfUlong_Ptr * ($MethodNum - 1)) ==> $OutCast1 Is PTR Array Of Virtual Methods Table // Method PTR = Array[MethodNum - 1]
;In C++ ==> unsigned long* iMethAddress = *(unsigned long**)((BYTE*)OutCast1 + (SizeOfUlong_Ptr * (MethodNum - 1)));
;Or In C++ ==> unsigned long* iMethAddress = OutCast1[MethodNum - 1];
;--------------------------------------------------------
if (IsBadCodePtr($iMethAddress)) Then
$MethodNum -= 1
ExitLoop
Else
$MethodNum += 1
EndIf
WEnd
Return SetError(0,0,$MethodNum)
EndFunc

Func IsBadCodePtr($lpfn)
Local $iReturn
$iReturn = DllCall("Kernel32.dll","BOOL","IsBadCodePtr","ptr",$lpfn)
if @error Then Return SetError(1,0,0)
Return SetError(0,0,$iReturn[0])
EndFunc

Func CLSIDFromString($psz)
$GUID = DllStructCreate("ulong Data1;ushort Data2;ushort Data3;byte Data4[8]")
$Oleerror = DllCall("Ole32.dll","int","CLSIDFromString","WSTR",$psz,"struct*",$GUID)
if @error Or $Oleerror[0] <> 0 Then Return SetError(1,0,0)
Return SetError(0,0,$Oleerror[2])
EndFunc

Func IIDFromString($psz)
$GUID = DllStructCreate("ulong Data1;ushort Data2;ushort Data3;byte Data4[8]")
$Oleerror = DllCall("Ole32.dll","int","IIDFromString","WSTR",$psz,"struct*",$GUID)
if @error Or $Oleerror[0] <> 0 Then Return SetError(1,0,0)
Return SetError(0,0,$Oleerror[2])
EndFunc

Example1

IPictureInterface.au3

#include "InterfaceCall.au3"
#include <WinAPI.au3>

;IPicture interface---------------------------------------------------------;IPicture interface
;EXTERN_C const IID IID_IPicture;
;
;
;
;#if defined(__cplusplus) && !defined(CINTERFACE)
;
;
;
; MIDL_INTERFACE("7BF80980-BF32-101A-8BBB-00AA00300CAB")
;
; IPicture : public IUnknown
;
; {
;
; public:
;
;    virtual HRESULT STDMETHODCALLTYPE get_Handle(
;
;        /* [out] */ OLE_HANDLE __RPC_FAR *pHandle) = 0;
;
;
;
;    virtual HRESULT STDMETHODCALLTYPE get_hPal(
;
;        /* [out] */ OLE_HANDLE __RPC_FAR *phPal) = 0;
;
;
;
;    virtual HRESULT STDMETHODCALLTYPE get_Type(
;
;        /* [out] */ SHORT __RPC_FAR *pType) = 0;
;
;
;
;    virtual HRESULT STDMETHODCALLTYPE get_Width(
;
;        /* [out] */ OLE_XSIZE_HIMETRIC __RPC_FAR *pWidth) = 0;
;
;
;
;    virtual HRESULT STDMETHODCALLTYPE get_Height(
;
;        /* [out] */ OLE_YSIZE_HIMETRIC __RPC_FAR *pHeight) = 0;
;
;
;
;    virtual HRESULT STDMETHODCALLTYPE Render(
;
;        /* [in] */ HDC hDC,
;
;        /* [in] */ LONG x,
;
;        /* [in] */ LONG y,
;
;        /* [in] */ LONG cx,
;
;        /* [in] */ LONG cy,
;
;        /* [in] */ OLE_XPOS_HIMETRIC xSrc,
;
;        /* [in] */ OLE_YPOS_HIMETRIC ySrc,
;
;        /* [in] */ OLE_XSIZE_HIMETRIC cxSrc,
;
;        /* [in] */ OLE_YSIZE_HIMETRIC cySrc,
;
;        /* [in] */ LPCRECT pRcWBounds) = 0;
;
;
;
;    virtual HRESULT STDMETHODCALLTYPE set_hPal(
;
;        /* [in] */ OLE_HANDLE hPal) = 0;
;
;
;
;    virtual HRESULT STDMETHODCALLTYPE get_CurDC(
;
;        /* [out] */ HDC __RPC_FAR *phDC) = 0;
;
;
;
;    virtual HRESULT STDMETHODCALLTYPE SelectPicture(
;
;        /* [in] */ HDC hDCIn,
;
;        /* [out] */ HDC __RPC_FAR *phDCOut,
;
;        /* [out] */ OLE_HANDLE __RPC_FAR *phBmpOut) = 0;
;
;
;
;    virtual HRESULT STDMETHODCALLTYPE get_KeepOriginalFormat(
;
;        /* [out] */ BOOL __RPC_FAR *pKeep) = 0;
;
;
;
;    virtual HRESULT STDMETHODCALLTYPE put_KeepOriginalFormat(
;
;        /* [in] */ BOOL keep) = 0;
;
;
;
;    virtual HRESULT STDMETHODCALLTYPE PictureChanged( void) = 0;
;
;
;
;    virtual HRESULT STDMETHODCALLTYPE SaveAsFile(
;
;        /* [in] */ LPSTREAM pStream,
;
;        /* [in] */ BOOL fSaveMemCopy,
;
;        /* [out] */ LONG __RPC_FAR *pCbSize) = 0;
;
;
;
;    virtual HRESULT STDMETHODCALLTYPE get_Attributes(
;
;        /* [out] */ DWORD __RPC_FAR *pDwAttr) = 0;
;
;
;
; };
;IPicture interface---------------------------------------------------------;IPicture interface

;-------------------------------
;IPicture : public IUnknown
;--------------------------------
;base class ==> IUnknown
;derived class ==> IPicture
;---------------------------------
;http://msdn.microsoft.com/en-us/library/hzk8a7d3.aspx
;When preceding the name of a base class, the public keyword specifies that the public
;and protected members of the base class are public and protected members, respectively,
;of the derived class.
;---------------------------------
;IUnknown interface---------------------------------------------------------;IUnknown interface
;#if (_MSC_VER >= 1100) && defined(__cplusplus) && !defined(CINTERFACE)
;
; EXTERN_C const IID IID_IUnknown;
;
; extern "C++"
;
; {
;
;    MIDL_INTERFACE("00000000-0000-0000-C000-000000000046")
;
;    IUnknown
;
;    {
;
;    public:
;
;        BEGIN_INTERFACE
;
;        virtual HRESULT STDMETHODCALLTYPE QueryInterface(
;
;            /* [in] */ REFIID riid,
;
;            /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject) = 0;
;
;
;
;        virtual ULONG STDMETHODCALLTYPE AddRef( void) = 0;
;
;
;
;        virtual ULONG STDMETHODCALLTYPE Release( void) = 0;
;
;
;
;        template<class Q>
;
;    HRESULT STDMETHODCALLTYPE QueryInterface(Q** pp)
;
;    {
;
;        return QueryInterface(__uuidof(Q), (void **)pp);
;
;    }
;
;
;
;        END_INTERFACE
;
;    };
;IUnknown interface---------------------------------------------------------;IUnknown interface

;------------------------------------------;Virtual Methods Table Of IPicture interface
;Virtual Methods Table Of IPicture interface
;virtual HRESULT QueryInterface() In (IUnknown interface) ==> Virtual Method Number Is 1
;virtual ULONG AddRef() In (IUnknown interface) ==> Virtual Method Number Is 2
;virtual ULONG Release() In (IUnknown interface) ==> Virtual Method Number Is 3
;virtual HRESULT get_Handle() In (IPicture interface) ==> Virtual Method Number Is 4
;virtual HRESULT get_hPal() In (IPicture interface) ==> Virtual Method Number Is 5
;virtual HRESULT get_Type() In (IPicture interface) ==> Virtual Method Number Is 6
;virtual HRESULT get_Width() In (IPicture interface) ==> Virtual Method Number Is 7
;virtual HRESULT get_Height() In (IPicture interface) ==> Virtual Method Number Is 8
;virtual HRESULT Render() In (IPicture interface) ==> Virtual Method Number Is 9
;virtual HRESULT set_hPal() In (IPicture interface) ==> Virtual Method Number Is 10
;virtual HRESULT get_CurDC() In (IPicture interface) ==> Virtual Method Number Is 11
;virtual HRESULT SelectPicture() In (IPicture interface) ==> Virtual Method Number Is 12
;virtual HRESULT get_KeepOriginalFormat() In (IPicture interface) ==> Virtual Method Number Is 13
;virtual HRESULT put_KeepOriginalFormat() In (IPicture interface) ==> Virtual Method Number Is 14
;virtual HRESULT PictureChanged() In (IPicture interface) ==> Virtual Method Number Is 15
;virtual HRESULT SaveAsFile() In (IPicture interface) ==> Virtual Method Number Is 16
;virtual HRESULT get_Attributes() In (IPicture interface) ==> Virtual Method Number Is 17
;The Count Of Virtual Methods Is 17
;------------------------------------------;Virtual Methods Table Of IPicture interface

$IPicture = GetIPictureInterface("Image.jpg")
;--------------------------------------------------
$Count = GetCount_Of_VirtualMethods($IPicture)
if Not @error Then
MsgBox(0,"Count Of Virtual Methods",$Count)
EndIf
;--------------------------------------------------
;//////////////////////////////////////////////////
;--------------------------------------------------
; C++ virtual HRESULT STDMETHODCALLTYPE get_Handle(OLE_HANDLE __RPC_FAR *pHandle);
;get_Handle() Virtual Method Number Is 4
;InterfaceCall($Inface,$ReturnType,$MethodNum,$Type1 = 0,$Param1 = 0)
;$Inface = $IPicture
;$ReturnType = "Long" Or HRESULT
;$MethodNum = 4 // Virtual Method Number
;$Type1 = "ptr*" Out Ptr
;$Param1 = 0
$Rt = InterfaceCall($IPicture,"long",4,"ptr*",0)
;Return Array Of DllCallAddress
if Not @error Then
$Handle = $Rt[1]
MsgBox(0,"get_Handle","Image Handle Is " & $Handle)
EndIf
;--------------------------------------------------
;//////////////////////////////////////////////////
;C ++ virtual HRESULT STDMETHODCALLTYPE get_Type(SHORT __RPC_FAR *pType);
;get_Type() Virtual Method Number Is 6
;InterfaceCall($Inface,$ReturnType,$MethodNum,$Type1 = 0,$Param1 = 0)
;$Inface = $IPicture
;$ReturnType = "Long" Or HRESULT
;$MethodNum = 6 // Virtual Method Number
;$Type1 = "SHORT*" Out SHORT
;$Param1 = 0
$Rt = InterfaceCall($IPicture,"long",6,"SHORT*",0)
;Return Array Of DllCallAddress
if Not @error Then
$Type = $Rt[1]
MsgBox(0,"get_Type()","Image Type Is " & $Type)
EndIf
;virtual HRESULT STDMETHODCALLTYPE get_Width(OLE_XSIZE_HIMETRIC __RPC_FAR *pWidth);
;get_Width() Virtual Method Number Is 7
;InterfaceCall($Inface,$ReturnType,$MethodNum,$Type1 = 0,$Param1 = 0)
;$Inface = $IPicture
;$ReturnType = "Long" Or HRESULT
;$MethodNum = 7 // Virtual Method Number
;$Type1 = "LONG*" Out LONG Or Out OLE_XSIZE_HIMETRIC
;$Param1 = 0
$Rt = InterfaceCall($IPicture,"long",7,"LONG*",0)
;Return Array Of DllCallAddress
if Not @error Then
$Width = $Rt[1]
$Width = MulDiv($Width,GetDeviceCaps(GetDC(0),88),2540); 88 => LOGPIXELSX
MsgBox(0,"get_Width()","Image Width Is " & $Width)
EndIf

;C++ virtual HRESULT STDMETHODCALLTYPE get_Height(OLE_YSIZE_HIMETRIC __RPC_FAR *pHeight)
;get_Height() Virtual Method Number Is 8
;InterfaceCall($Inface,$ReturnType,$MethodNum,$Type1 = 0,$Param1 = 0)
;$Inface = $IPicture
;$ReturnType = "Long" Or HRESULT
;$MethodNum = 8 // Virtual Method Number
;$Type1 = "LONG*" Out LONG Or Out OLE_YSIZE_HIMETRIC
;$Param1 = 0
$Rt = InterfaceCall($IPicture,"long",8,"LONG*",0)
;Return Array Of DllCallAddress
if Not @error Then
$Height = $Rt[1]
$Height = MulDiv($Height,GetDeviceCaps(GetDC(0),90),2540); 90 => LOGPIXELSY
MsgBox(0,"get_Height()","Image Height Is " & $Height)
EndIf

Func GetIPictureInterface($ImageFileName)
Local $nBytes
$FileSize = FileGetSize($ImageFileName) + 1
if @error Then Return SetError(1,0,0)
$tBuffer = DllStructCreate("byte[" & $FileSize & "]")
$hFile = _WinAPI_CreateFile($ImageFileName, 2, 2)
if @error Then Return SetError(2,0,0)
_WinAPI_ReadFile($hFile,DllStructGetPtr($tBuffer),$FileSize,$nBytes)
if @error Then Return SetError(3,0,0)
_WinAPI_CloseHandle($hFile)
$LPVOID = DllStructGetPtr($tBuffer)
$hMem = GlobalAlloc(0x0042,$FileSize) ;$GHND = 0x0042
if @error Then SetError(4,0,0)
$hLock = GlobalLock($hMem)
if @error Then
GlobalFree($hMem)
Return SetError(5,0,0)
EndIf
MoveMemory($LPVOID,$hLock,$FileSize)
$lpstream = CreateStreamOnHGlobal($hLock,True)
if @error Then SetError(6,0,0)
$riid = IIDFromString("{7BF80980-BF32-101A-8BBB-00AA00300CAB}")
if @error Then SetError(7,0,0)
$HRESULT = DllCall("OleAut32.dll","LONG","OleLoadPicture","PTR",$lpstream,"LONG", _
$FileSize,"BOOL",True,"ptr",DllStructGetPtr($riid),"PTR*",0)
if @error Or $HRESULT[0] <> 0 Then
GlobalUnlock($hMem)
GlobalFree($hMem)
Return SetError(8,0,0)
EndIf
GlobalUnlock($hMem)
GlobalFree($hMem)
$IPicture = $HRESULT[5]
Return SetError(0,0,$IPicture)
EndFunc

Func GlobalAlloc($uFlags,$dwBytes)
$HGLOBAL = DllCall("Kernel32.dll","ptr","GlobalAlloc","UINT",$uFlags,"ULONG_PTR",$dwBytes)
if @error Or $HGLOBAL[0] = 0 Then Return SetError(1,0,0)
Return SetError(0,0,$HGLOBAL[0])
EndFunc

Func GlobalLock($hMem)
$LPVOID = DllCall("Kernel32.dll","ptr","GlobalLock","ptr",$hMem)
if @error Or $LPVOID[0] = 0 Then Return SetError(1,0,0)
Return SetError(0,0,$LPVOID[0])
EndFunc

Func GlobalUnlock($hMem)
$BOOL = DllCall("Kernel32.dll","BOOL","GlobalUnlock","ptr",$hMem)
if @error Or $BOOL[0] = 0 Then Return SetError(1,0,0)
Return SetError(0,$BOOL[0])
EndFunc

Func GlobalFree($hMem)
$HGLOBAL = DllCall("Kernel32.dll","ULONG_PTR","GlobalFree","ptr",$hMem)
if (@error Or ($HGLOBAL[0])) Then Return SetError(1,0,$HGLOBAL[0])
Return SetError(0,0,0)
EndFunc

Func CreateStreamOnHGlobal($hGlobal,$fDeleteOnRelease)
$WINOLE = DllCall("Ole32.dll","PTR","CreateStreamOnHGlobal","ptr",$hGlobal,"BOOL",$fDeleteOnRelease _
,"PTR*",0)
if @error Or $WINOLE[0] <> 0 Then Return SetError(1,0,0)
Return SetError(0,0,$WINOLE[3])
EndFunc

Func MulDiv($nNumber,$nNumerator,$nDenominator)
$Rt = DllCall("Kernel32.dll","int","MulDiv","int",$nNumber,"int",$nNumerator,"int",$nDenominator)
Return $Rt[0]
EndFunc

Func GetDeviceCaps($hdc,$nIndex)
$Rt = DllCall("Gdi32.dll","int","GetDeviceCaps","ptr",$hdc,"int",$nIndex)
Return $Rt[0]
EndFunc

Func GetDC($hWnd)
$HDC = DllCall("User32.dll","ptr","GetDC","ptr",$hWnd)
if @error Or $HDC[0] = 0 Then Return SetError(1,0,0)
Return SetError(0,0,$HDC[0])
EndFunc

Func MoveMemory($Source,$Destination,$Length)
DllCall("Kernel32.dll","none","RtlMoveMemory","PTR",$Destination,"PTR",$Source,"INT",$Length)
EndFunc

Example2

http://msdn.microsoft.com/en-us/library/windows/desktop/dd387916%28v=vs.85%29.aspx

The following code example inserts a file object into a rich edit control. If a program

is associated with the file type on the user's machine (for example, Microsoft Excel for

an .xls file), the contents of the file display in the control; otherwise, an icon appears.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiRichEdit.au3>
#include <SendMessage.au3>
#include "InterfaceCall.au3"

$hGui = GUICreate("InsertObject Example",500,390)
$hRichEdit = _GUICtrlRichEdit_Create($hGui,"", 10, 10,480,300, _
BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
$Button1 = GUICtrlCreateButton("InsertObject",10,320,200,50)
GUICtrlSetFont(-1,18,600)
GUISetState()
InsertObject($hRichEdit,@ScriptDir & "\Image.bmp")
While True
     $iMsg = GUIGetMsg()
     Select
         Case $iMsg = $GUI_EVENT_CLOSE
             _GUICtrlRichEdit_Destroy($hRichEdit)
             Exit
Case $iMsg = $Button1
$var = FileOpenDialog("", @MyDocumentsDir & "\","All (*.*)", 1 + 4)
if Not @error Then
             InsertObject($hRichEdit,$var)
EndIf
     EndSelect
WEnd


;http://msdn.microsoft.com/en-us/library/windows/desktop/dd387916%28v=vs.85%29.aspx
;Use a Rich Edit Interface
;Rich edit controls expose some of their functionality through Component Object Model
;(COM) interfaces. By obtaining an interface from a control, you gain the ability to
;work with other objects within the control. You can obtain this interface by sending
;the EM_GETOLEINTERFACE message. From the IRichEditOle interface, you can then obtain
;interfaces used in the Text Object Model.
;Another interface, IRichEditOleCallback, is implemented by applications to define the
;behavior of the control when it interacts with objects.
;Insert an Object into a Rich Edit Control
;The following code example inserts a file object into a rich edit control. If a program
;is associated with the file type on the user's machine (for example, Microsoft Excel for
;an .xls file), the contents of the file display in the control; otherwise, an icon appears.
Func InsertObject($hRichEdit,$pszFileName)
Local $hr = 0, $Rt = 0 , $EM_GETOLEINTERFACE = 1084 , $pRichEditOle = 0

$pRichEditOle = _SendMessage($hRichEdit,$EM_GETOLEINTERFACE,0,0,4,"wparam","ptr*")
if ($pRichEditOle == 0) Then

     return FALSE;
EndIf

Local $pLockBytes = 0
$hr = DllCall("Ole32.dll","long","CreateILockBytesOnHGlobal","ptr",0,"BOOL",True,"Ptr*",0)

if (@error Or $hr[0] <> 0) Then

     return FALSE;
EndIf
$pLockBytes = $hr[3]

Local $pStorage = 0
$hr = DllCall("Ole32.dll","long","StgCreateDocfileOnILockBytes","ptr",$pLockBytes, _
     "DWORD",BitOR(0x00000010,0x00001000,0x00000002), _
"DWORD",0,"Ptr*",0)


if (@error Or $hr[0] <> 0) Then

     return FALSE;
EndIf
$pStorage = $hr[4]

$tagFORMATETC = "int cfFormat;ptr ptd;DWORD dwAspect;LONG lindex;DWORD tymed"
$formatEtc = DllStructCreate($tagFORMATETC)
DllStructSetData($formatEtc,"cfFormat",0)
DllStructSetData($formatEtc,"ptd",0)
DllStructSetData($formatEtc,"dwAspect",1)
DllStructSetData($formatEtc,"lindex",-1)
DllStructSetData($formatEtc,"tymed",0)

Local $pClientSite
;hr = pRichEditOle->GetClientSite(&pClientSite);
;GetClientSite Virtual Method Number Of GetClientSite Is 4 in (IRichEditOle interface)
$hr = InterfaceCall($pRichEditOle,"long",4,"ptr*",0)


if (@error Or $hr[0] <> 0) Then

     return FALSE;
EndIf
$pClientSite = $hr[1]

Local $pUnk,$clsid = CLSIDFromString("{00000000-0000-0000-0000-000000000000}")
Local $IID_IUnknown = IIDFromString("{00000000-0000-0000-C000-000000000046}")

$hr = DllCall("Ole32.dll","long","OleCreateFromFile","ptr",DllStructGetPtr($clsid),"wstr", _
$pszFileName,"ptr",DllStructGetPtr($IID_IUnknown),"DWORD",1,"ptr",DllStructGetPtr($formatEtc) _
,"ptr",$pClientSite,"ptr",$pStorage,"Ptr*",0)
;pClientSite->Release();
;IOleClientSite interface inherits from the IUnknown Or IOleClientSite : public IUnknown
;Release() Virtual Method Number Of Release() Is 3 In (IOleClientSite interface)
InterfaceCall($pClientSite,"long",3)

if ($hr[0] <> 0) Then

     return FALSE;
EndIf
$pUnk = $hr[8]

Local $pObject,$IID_IOleObject = IIDFromString("{00000112-0000-0000-C000-000000000046}")
;hr = pUnk->QueryInterface(IID_IOleObject, (void**)&pObject);
;New pUnk (IOleObject) interface inherits from the IUnknown Or IOleObject : public IUnknown
;QueryInterface() Virtual Method Number Of QueryInterface() Is 1 In (IOleObject)
$hr = InterfaceCall($pUnk,"long",1,"ptr",DllStructGetPtr($IID_IOleObject),"ptr*",0)
;Release() Virtual Method Number Of Release() Is 3 In (IOleObject interface)
;pUnk->Release();
InterfaceCall($pUnk,"long",3)

if ($hr[0] <> 0) Then

     return FALSE;
EndIf
$pObject = $hr[2]

DllCall("Ole32.dll","long","OleSetContainedObject","ptr",$pObject,"BOOL",True)
$tagREOBJECT = "DWORD cbStruct;LONG cp;ulong Data1;ushort Data2;ushort Data3;byte Data4[8]" & _
";ptr poleobj;ptr pstg;ptr polesite;long x;long y;DWORD dvaspect;DWORD dwFlags;DWORD dwUser"
$reobject = DllStructCreate($tagREOBJECT)
DllStructSetData($reobject,"cbStruct",DllStructGetSize($reobject))
;hr = pObject->GetUserClassID(&clsid);
;GetUserClassID Virtual Method Number Of GetUserClassID Is 16 in (IOleObject interface)
$hr = InterfaceCall($pObject,"long",16,"ptr",DllStructGetPtr($clsid))

if (@error Or $hr[0] <> 0) Then

     ;pObject->Release();
;Release() Virtual Method Number Of Release() Is 3 In (IOleObject interface)
InterfaceCall($pObject,"long",3)
     return FALSE
EndIf

MoveMemory(DllStructGetPtr($clsid),DllStructGetPtr($reobject) + 8,16)
;8 ==> SizeOf "DWORD cbStruct;LONG cp;"
;16 ==> sizeOf $clsid
DllStructSetData($reobject,"cp",-1)
DllStructSetData($reobject,"dvaspect",1)
DllStructSetData($reobject,"dwFlags",BitOR(0x00000001,0x00000002))
DllStructSetData($reobject,"dwUser",0)
DllStructSetData($reobject,"poleobj",$pObject)
DllStructSetData($reobject,"polesite",$pClientSite)
DllStructSetData($reobject,"pstg",$pStorage)
$SIZEL = DllStructCreate("long;long");
DllStructSetData($reobject,"SIZEL",DllStructGetPtr($SIZEL))


_SendMessage($hRichEdit, 0xB1, 0, -1);EM_SETSEL
$Rt = _SendMessage($hRichEdit, 0xB0,0,0,-1,"DWORD*","DWORD*");EM_GETSEL
Local $dwStart = $Rt[3], $dwEnd = $Rt[4]
_SendMessage($hRichEdit,0xB1, $dwEnd+1, $dwEnd+1);EM_SETSEL
_SendMessage($hRichEdit, 0xC2, TRUE,@CRLF,0,"BOOL","wstr");EM_REPLACESEL

;hr = pRichEditOle->InsertObject(&reobject);
;InsertObject Virtual Method Number Of InsertObject Is 8 in (IRichEditOle interface)
$hr = InterfaceCall($pRichEditOle,"long",8,"ptr",DllStructGetPtr($reobject))
;pObject->Release();
;Release() Virtual Method Number Of Release() Is 3 In (IOleObject interface)
InterfaceCall($pObject,"long",3)
;pRichEditOle->Release();
;Release() Virtual Method Number Of Release() Is 3 In (IRichEditOle interface)
InterfaceCall($pRichEditOle,"long",3)

if ($hr[0] <> 0) Then

     return FALSE;
EndIf

return TRUE;

EndFunc



Func MoveMemory($Source,$Destination,$Length)
DllCall("Kernel32.dll","none","RtlMoveMemory","PTR",$Destination,"PTR",$Source,"INT",$Length)
EndFunc
Edited by wolf9228

صرح السماء كان هنا

 

Link to comment
Share on other sites

Thanks for sharing it but first I've to understand what's is going on with the code you have provided. :huh2: And this will take a lot of time...

Preconditions are deep knowledge of the Windows API which I don't have.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thanks for sharing it but first I've to understand what's is going on with the code you have provided. :huh2: And this will take a lot of time...

Preconditions are deep knowledge of the Windows API which I don't have.

Br,

UEZ

Do I understand you are having difficulty in understanding the script ... A good thing.

Can you quote the part that you do not understand and will explain it.

The project requires the Knowledge of classes and interfaces in C + + language

A quick explanation

Classes and interfaces on the memory-like structure composed of several elements

The data type of the first element is a pointer to the value of one-dimensional array

Each element of the array is a pointer to the method address

Thanks

Edited by wolf9228

صرح السماء كان هنا

 

Link to comment
Share on other sites

It is not the code but rather than the concept of methods of classes and interfaces.

The project requires the Knowledge of classes and interfaces in C + + language

That's exactly the point.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thanks :)

Awesome, I see what's going on now.

You're embedding file objects into a rich edit control, this is actually pretty cool.

I was looking for something like this some time ago.

I noticed while playing with this that when I resize the embedded object, it's only a re-sizable rectangle that shows up with the object name.

As I was resizing several objects, I saw that one of them suddenly tried to display the unknown file type icon, are the embeded objects supposed to display their icon as well? Because they only show up randomly and then disappear.

Also, I believe it would benefit the thread if you used more laymanish terminology for describing this script, something like "RichEdit embedded objects".

I say this because I have seen some other examples you have made that I am currently using, but I only found them by chance due to the way you name your topics.

Edited by THAT1ANONYMOUSDUDE
Link to comment
Share on other sites

Awesome, I see what's going on now.

You're embedding file objects into a rich edit control, this is actually pretty cool.

I was looking for something like this some time ago.

I noticed while playing with this that when I resize the embedded object, it's only a re-sizable rectangle that shows up with the object name.

As I was resizing several objects, I saw that one of them suddenly tried to display the unknown file type icon, are the embeded objects supposed to display their icon as well? Because they only show up randomly and then disappear.

Also, I believe it would benefit the thread if you used more laymanish terminology for describing this script, something like "RichEdit embedded objects".

I say this because I have seen some other examples you have made that I am currently using, but I only found them by chance due to the way you name your topics.

Does not require the use of a file to a particular object because the

OleCreateFromFile function works with any file

These images WordPad application

Posted Image

Posted Image

Posted Image

Thanks

Edited by wolf9228

صرح السماء كان هنا

 

Link to comment
Share on other sites

  • 3 months later...
Link to comment
Share on other sites

Here are the examples in the first post implemented with ObjCreateInterface().

The first example shows how to get some picture properties with the IPicture interface. This is the main function:

Func GetPictureProps( $ImageFileName )

Local $iFileSize, $tBuffer, $pBuffer, $hFile, $nBytes
Local $hMem, $hLock, $lpStream, $aRet
Local $IPicture, $pIPicture
Local $props, $handle, $type, $width, $height

$iFileSize = FileGetSize( $ImageFileName ) + 1
$tBuffer = DllStructCreate( "byte[" & $iFileSize & "]" )
$pBuffer = DllStructGetPtr( $tBuffer )
$hFile = _WinAPI_CreateFile( $ImageFileName, 2, 2 )
_WinAPI_ReadFile( $hFile, $pBuffer, $iFileSize, $nBytes )
_WinAPI_CloseHandle( $hFile )

$hMem = GlobalAlloc( 0x0042, $iFileSize ) ; $GHND = 0x0042
$hLock = GlobalLock( $hMem )
MoveMemory( $pBuffer, $hLock, $iFileSize )
$lpStream = CreateStreamOnHGlobal( $hLock, True )

$aRet = DllCall( "OleAut32.dll", "long", "OleLoadPicture", "ptr", $lpStream, "long", _
$iFileSize, "bool", True, "ptr", DllStructGetPtr( $tRIID_IPicture ), "ptr*", 0 )
GlobalUnlock($hMem)
GlobalFree($hMem)

$pIPicture = $aRet[5]
$IPicture = ObjCreateInterface( $pIPicture, $sIID_IPicture, $dtagIPicture )

$IPicture.get_Handle( $handle )
$IPicture.get_Type( $type )
$IPicture.get_Width( $width )
$IPicture.get_Height( $height )
$props = "Image name = " & $ImageFileName & @CRLF & _
     "Image handle = " & Ptr( $handle ) & @CRLF & _
     "Image type = " & $type & @CRLF & _
     "Image width = " & MulDiv( $width, GetDeviceCaps( GetDC(0), 88 ), 2540 ) & @CRLF & _ ; 88 => LOGPIXELSX
     "Image height = " & MulDiv( $height, GetDeviceCaps( GetDC(0), 90 ), 2540 )  ; 90 => LOGPIXELSY
MsgBox( 0, "ObjCreateInterface", $props )

EndFunc

Get the full code here: IPictureInterface2.au3

You need the image files from the original zip.

The second example uses the methods of the IRichEditOle and IOleObject interfaces to insert a picture in a RichEdit control. It's a translation of a C++ example. There is a link to the C++ code in the first post. It's a really nice example. Here is the main AutoIt function:

Func InsertObject( $hRichEdit, $pszFileName )

; 1. Get the IRichEditOle interface
Local $pIRichEditOle = _SendMessage( $hRichEdit, $EM_GETOLEINTERFACE, 0, 0, 4, "wparam", "ptr*" )
Local $IRichEditOle = ObjCreateInterface( $pIRichEditOle, $sIID_IRichEditOle, $dtagIRichEditOle )

; 2. Create structured storage
Local $pILockBytes, $pIStorage
CreateILockBytesOnHGlobal( $NULL, True, $pILockBytes )
StgCreateDocfileOnILockBytes( $pILockBytes, BitOR($STGM_SHARE_EXCLUSIVE,$STGM_CREATE,$STGM_READWRITE), $pIStorage )

; 3. Set up the data format
Local $tFORMATETC = DllStructCreate( $tagFORMATETC )
DllStructSetData( $tFORMATETC, "cfFormat", 0 )
DllStructSetData( $tFORMATETC, "ptd", $NULL )
DllStructSetData( $tFORMATETC, "dvaspect", $DVASPECT_CONTENT )
DllStructSetData( $tFORMATETC, "lindex", -1 )
DllStructSetData( $tFORMATETC, "tymed", $TYMED_NULL )

; 4. Get a pointer to the display site
Local $pIOleClientSite
$IRichEditOle.GetClientSite( $pIOleClientSite )

; 5. Create the object and retrieve its IUnknown interface
Local $pIUnknown, $IUnknown
OleCreateFromFile( $pszFileName, $tRIID_IUnknown, $OLERENDER_DRAW, $tFORMATETC, $pIOleClientSite, $pIStorage, $pIUnknown )
$IUnknown = ObjCreateInterface( $pIUnknown, $sIID_IUnknown, $dtagIUnknown )

; 6. Get the IOleObject interface to the object
Local $pIOleObject, $IOleObject
$IUnknown.QueryInterface( $tRIID_IOleObject, $pIOleObject )
$IOleObject = ObjCreateInterface( $pIOleObject, $sIID_IOleObject, $dtagIOleObject )

; 7. To ensure that references are counted correctly, notify the object that it is contained
OleSetContainedObject( $pIOleObject, True )

; 8. Set up RichEdit object info
Local $tREOBJECT = DllStructCreate( $tagREOBJECT ), $clsid = $tRIID_INULL
DllStructSetData( $tREOBJECT, "cbStruct", DllStructGetSize( $tREOBJECT ) ) ; Structure size
DllStructSetData( $tREOBJECT, "cp", $REO_CP_SELECTION )      ; The selection is replaced with the specified object
$IOleObject.GetUserClassID( $clsid )
DllStructSetData( $tREOBJECT, "Data1", DllStructGetData( $clsid, "Data1" ) ) ; The REOBJECT structure contains a CLSID structure,
DllStructSetData( $tREOBJECT, "Data2", DllStructGetData( $clsid, "Data2" ) ) ; a class identifier of the object
DllStructSetData( $tREOBJECT, "Data3", DllStructGetData( $clsid, "Data3" ) )
DllStructSetData( $tREOBJECT, "Data4", DllStructGetData( $clsid, "Data4" ) )
DllStructSetData( $tREOBJECT, "pIOleObject", $pIOleObject )
DllStructSetData( $tREOBJECT, "pIStorage", $pIStorage )
DllStructSetData( $tREOBJECT, "pIOleClientSite", $pIOleClientSite )
DllStructSetData( $tREOBJECT, "x", 0 )               ; (x,y) = (0,0) on insertion indicates that
DllStructSetData( $tREOBJECT, "y", 0 )               ; an object is free to determine its size.
DllStructSetData( $tREOBJECT, "dvaspect", $DVASPECT_CONTENT )        ; Provides a representation of an object so it can be displayed as an embedded object inside of a container
DllStructSetData( $tREOBJECT, "dwFlags", BitOR($REO_RESIZABLE,$REO_BELOWBASELINE) )
DllStructSetData( $tREOBJECT, "dwUser", 0 )

; 9. Move the caret to the end of the text and add a carriage return
_SendMessage( $hRichEdit, $EM_SETSEL, 0, -1 )
Local $aRet = _SendMessage( $hRichEdit, $EM_GETSEL, 0, 0, -1, "dword*", "dword*" )
Local $dwStart = $aRet[3], $dwEnd = $aRet[4]
_SendMessage( $hRichEdit, $EM_SETSEL, $dwEnd+1, $dwEnd+1 )
_SendMessage( $hRichEdit, $EM_REPLACESEL, TRUE, @CRLF, 0, "bool", "wstr" )

; 10. Insert the object
$IRichEditOle.InsertObject( $tREOBJECT )

EndFunc

Get the full code here: IRichEditOleInterface2.au3

Half of the script is constants, enumerations, structures and interface definitions.

You need the image files from the original zip.

Lars.

Edited by LarsJ
Link to comment
Share on other sites

You need the image files from the original zip.

Very power step in AutoIt3 developing!

Thank you.

One note about IRichEditOleInterface2.au3.

It uses picture Image.bmp in line:

InsertObject($hRichEdit,"Image.bmp")

You can add any Image.bmp into @ScriptDir or update to

InsertObject($hRichEdit,"Image.jpg")

I've been getting error without Image.bmp

The point of world view

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...