Jump to content

How can registration the class


Recommended Posts

How can registration the class and called from another program
CoRegisterClassObject and CoGetClassObject functions returns a success value
On this page of the Microsoft website explain how to register class
http://msdn.microsoft.com/en-us/library/windows/desktop/ms221186%28v=vs.85%29.aspx

#include <WinAPI.au3>
#include <Memory.au3>
#include <GUIConstantsEx.au3>
$Ole32 = DllOpen("Ole32.dll")
DllCall($Ole32,"long","CoInitialize","ptr",0)


$rclsidSt = _WinAPI_GUIDFromString("{6DE7CAC2-1D1C-43B8-A91C-D0B3495007CD}")
Global $tagClassObjA = "ULONG_PTR QueryInterface;ULONG_PTR AddRef;ULONG_PTR Release;ULONG_PTR MethodA;ULONG_PTR MethodB;long Addend"
$RegQInterface = DllCallbackRegister("QueryInterface","int","ptr;ptr;ptr")
$RegAddRef = DllCallbackRegister("AddRef","int","ptr")
$RegRelease = DllCallbackRegister("Release","int","ptr")
$RegMethodA = DllCallbackRegister("MethodA","int","ptr;int;int")
$RegMethodB = DllCallbackRegister("MethodB","int","ptr;int;int")
$ClassObjStA = DllStructCreate($tagClassObjA)
DllStructSetData($ClassObjStA,"QueryInterface",DllCallbackGetPtr($RegQInterface))
DllStructSetData($ClassObjStA,"AddRef",DllCallbackGetPtr($RegAddRef))
DllStructSetData($ClassObjStA,"Release",DllCallbackGetPtr($RegRelease))
DllStructSetData($ClassObjStA,"MethodA",DllCallbackGetPtr($RegMethodA))
DllStructSetData($ClassObjStA,"MethodB",DllCallbackGetPtr($RegMethodB))
Global $tagClassObjB = "ULONG_PTR Class"
$ClassObjStB = DllStructCreate($tagClassObjB)
DllStructSetData($ClassObjStB,"Class",DllStructGetPtr($ClassObjStA))
$ClassObj = DllStructGetPtr($ClassObjStB)
$rclsid = DllStructGetPtr($rclsidSt)
$CLSCTX_INPROC_SERVER = 0x1
$dwClsContext = $CLSCTX_INPROC_SERVER
$REGCLS_MULTIPLEUSE = 1
$flags = $REGCLS_MULTIPLEUSE
$dwRegister = CoRegisterClassObject($rclsid,$ClassObj,$dwClsContext,$flags)
MsgBox(0,$dwRegister,@error)
$Return = CoRegisterPSClsid($rclsid,$rclsid)
MsgBox(0,"kkkkk",$Return)
$Return = RegisterActiveObject($ClassObj,$rclsid,0x1)
MsgBox(0,"nnnn",$Return)


$Tag_COSERVERINFO = "DWORD dwReserved1;ptr pwszName;ptr pAuthInfo;DWORD dwReserved2"
$COSERVERINFOSt = DllStructCreate($Tag_COSERVERINFO)




$Return = CoGetClassObject($rclsid,$dwClsContext,0,$rclsid)
MsgBox(0,@error,$Return)

Func QueryInterface($Interface,$riid,$ppvObject)
_MemMoveMemory($Interface,$ppvObject,DllStructGetSize(DllStructCreate("ULONG_PTR")))
InterfaceCall($Interface,"long",2)
Return 0
EndFunc

Func AddRef($Interface)
$StCast1 = DllStructCreate($tagClassObjB,$Interface)
$Cast1 = DllStructGetData($StCast1,1)
$StCast2 = DllStructCreate($tagClassObjA,$Cast1)
$Addend = DllStructGetData($StCast2,"Addend") + 1
DllStructSetData($StCast2,"Addend",$Addend)
Return $Addend
EndFunc

Func Release($Interface)
$StCast1 = DllStructCreate($tagClassObjB,$Interface)
$Cast1 = DllStructGetData($StCast1,1)
$StCast2 = DllStructCreate($tagClassObjA,$Cast1)
$Addend = DllStructGetData($StCast2,"Addend") - 1
if ($Addend = 0) Then
Return 0
EndIf
Return $Addend
EndFunc


Func MethodA($Interface,$Nu1,$Nu2)
Return ($Nu1 * $Nu2)
EndFunc

Func MethodB($Interface,$Nu1,$Nu2)
Return ($Nu1 + $Nu2)
EndFunc


Func CoRegisterPSClsid($riid,$rclsid)
$HRESULT = DllCall($Ole32,"long","CoRegisterPSClsid","ptr",$riid,"ptr",$rclsid)
if @error Or $HRESULT[0] <> 0 Then Return SetError(1,0,0)
Return SetError(0,0,$HRESULT[0])
EndFunc

Func CoGetPSClsid($riid)
$HRESULT = DllCall($Ole32,"long","CoGetPSClsid","ptr",$riid,"ptr*",0)
if @error Or $HRESULT[0] <> 0 Then Return SetError(1,0,0)
Return SetError(0,0,$HRESULT[2])
EndFunc

Func CoGetClassObject($rclsid,$dwClsContext,$pServerInfo,$riid)
$HRESULT = DllCall($Ole32,"long","CoGetClassObject","ptr",$rclsid, _
"DWORD",$dwClsContext,"ptr",$pServerInfo,"ptr",$riid,"ptr*",0)
if @error Or $HRESULT[0] <> 0 Then Return SetError(1,0,$HRESULT[0])
Return SetError(0,0,$HRESULT[5])
EndFunc

Func CoRegisterClassObject($rclsid,$pUnk,$dwClsContext,$flags)
$HRESULT = DllCall($Ole32,"long","CoRegisterClassObject","ptr",$rclsid _
,"ptr",$pUnk,"DWORD",$dwClsContext,"DWORD",$flags,"DWORD*",0)
if @error Or $HRESULT[0] <> 0 Then Return SetError(1,0,0)
Return SetError(0,0,$HRESULT[5])
EndFunc


Func RegisterActiveObject($punk,$rclsid,$dwFlags)
$HRESULT = DllCall($Ole32,"long","RegisterActiveObject","ptr",$punk _
,"ptr",$rclsid,"DWORD",$dwFlags,"DWORD",$flags,"DWORD*",0)
if @error Or $HRESULT[0] <> 0 Then Return SetError(1,0,0)
Return SetError(0,0,$HRESULT[5])
EndFunc

Func CoRevokeClassObject($dwRegister)
$HRESULT = DllCall($Ole32,"long","CoRevokeClassObject","DWORD",$dwRegister)
if @error Or $HRESULT[0] <> 0 Then Return SetError(1,0,False)
Return SetError(0,0,True)
EndFunc

Func GetIUnknown()
$StrIID_IUnknown = "{00000000-0000-0000-C000-000000000046}"
$StrCLSID_IUnknown = "{7CE551EA-F85C-11CE-9059-080036F12502}"
$IUnknown = GetObjFromGuid($StrIID_IUnknown,$StrCLSID_IUnknown)
if @error Then Return SetError(1,0,0)
Return $IUnknown
EndFunc

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 GetObjFromGuid($StrIID,$StrCLSID)
Local $LSCTX_ALL = BitOR(0x1,0x4,0x16),$Rt = 0
$clsid = CLSIDFromString($StrCLSID)
if @error Then Return SetError(2,0,0)
$riid = IIDFromString($StrIID)
if @error Then Return SetError(3,0,0)
$Rt = DllCall($Ole32,"long","CoCreateInstance","ptr",DllStructGetPtr($clsid), _
"ptr",0,"DWORD",$LSCTX_ALL,"ptr",DllStructGetPtr($riid),"ptr*",0)
if @error Or $Rt[0] <> 0 Then Return SetError(4,0,0)
Return SetError(0,0,$Rt[5])
EndFunc

Func CLSIDFromString($psz)
$GUID = DllStructCreate("ulong Data1;ushort Data2;ushort Data3;byte Data4[8]")
$Oleerror = DllCall($Ole32,"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,"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

Func MIDL_DEFINE_GUID($Elm1,$Elm2,$Elm3,$iElm1,$iElm2,$iElm3,$iElm4,$iElm5,$iElm6,$iElm7,$iElm8)
$vGUID = DllStructCreate("ulong Data1;ushort Data2;ushort Data3;byte Data4[8]")
DllStructSetData($vGUID,1,$Elm1)
DllStructSetData($vGUID,2,$Elm2)
DllStructSetData($vGUID,3,$Elm3)
DllStructSetData($vGUID,4,$iElm1,1)
DllStructSetData($vGUID,4,$iElm2,2)
DllStructSetData($vGUID,4,$iElm3,3)
DllStructSetData($vGUID,4,$iElm4,4)
DllStructSetData($vGUID,4,$iElm5,5)
DllStructSetData($vGUID,4,$iElm6,6)
DllStructSetData($vGUID,4,$iElm7,7)
DllStructSetData($vGUID,4,$iElm8,8)
Return $vGUID
EndFunc


 
Edited by wolf9228

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

 

Link to comment
Share on other sites

@wolf9228 - If this is a question you are asking, then you are doing so in the wrong part of the forum. Example scripts and programs here only.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

@wolf9228 - If this is a question you are asking, then you are doing so in the wrong part of the forum. Example scripts and programs here only.

 

Sorry I did not realize I was browsing through the examples section :)

Edited by wolf9228

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

 

Link to comment
Share on other sites

If you click report, and give an explanation, then a Mod will relocate the topic to General Help & Support for you.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

If you click report, and give an explanation, then a Mod will relocate the topic to General Help & Support for you.

I know that thanks

If you can move the topic to the help section do so

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

 

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...