minxomat

(Solved) Temporary COM object instances

4 posts in this topic

#1 ·  Posted (edited)

Please note: I'm not talking about anything .NET here, nor about .NET assemblies. When I say something about "classes" or "VB", I mean native, unmanaged code.

Registration free COM instances

COM is great, but it usually requires a modification to the system registry to make a COM server available for all users. This registration process is simple, but requires elevated privileges, so that a "normal" user can't register objects. There were certain workarounds (like RegSvrEx), but they stopped working when Vista introduced a new UAC.

Instances from file

At some point in '08, a very popular VB programmer, "Schmidt", created DirectCOM, a native DLL that when called, can spawn an instance of a COM class from the file, without ever touching the registry. I've uploaded a copy here. There is no official source for DirectCOM, but internally, it does something like this:

Private Declare Function LoadLibrary& Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName$)
Private Declare Function FreeLibrary& Lib "kernel32" (ByVal hLibModule&)
Private Declare Function GetProcAddress& Lib "kernel32" (ByVal hModule&, ByVal lpProcName$)
Private Declare Function CallWindowProc& Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc&, ByVal hWnd&, ByVal MSG&, ByVal wParam&, ByVal lParam&)
Private Declare Sub RtlMoveMemory Lib "kernel32" (Dst As Any, Src As Any, ByVal bLength&)

Private IIDCF As UUID, IIU As UUID, pIUNull As IUnknown
Private Libs As Collection, CFs As Collection, ASM() As Byte

Private Sub Class_Initialize()
  'Get IID for IClassFactory
  CLSIDFromString "{00000001-0000-0000-C000-000000000046}", IIDCF
  'same for IUnknown
  CLSIDFromString "{00000000-0000-0000-C000-000000000046}", IIU
  Set Libs = New Collection
  Set CFs = New Collection
  'Initialize ASM-Code for StdCall (principle found on VBDotCom)
  ASM = StrConv("XYYYYPh    h    h    è    ÃÌ", vbFromUnicode)
End Sub

Public Function GetInstance(sFile$, sClass$) As Object
Dim Result&, Lib&, pGetClass&, RefIU As IUnknown
Dim TLI As ITypeLib, CID As UUID, pICF As IClassFactory, pp&
  On Error Resume Next
  Lib = Libs(sFile)
  If Lib = 0 Then
    Lib = LoadLibrary(sFile)
  Else
    Set pICF = CFs(sFile & sClass)
    If Not pICF Is Nothing Then
      pICF.CreateInstance pIUNull, IIU, RefIU
      Set GetInstance = RefIU
      Exit Function
    End If
  End If
  If Lib = 0 Then Err.Clear: Exit Function
  Libs.Add Lib, sFile 'Cache the Library-Handle
  Set TLI = LoadTypeLibEx(sFile, REGKIND_NONE)
  If TLI Is Nothing Then Err.Clear: Exit Function

  If Not ClassExists(TLI, sClass, CID) Then Err.Clear: Exit Function

  pGetClass = GetProcAddress(Lib, "DllGetClassObject")
  If pGetClass = 0 Then Err.Clear: Exit Function

  RtlMoveMemory ASM(7), VarPtr(pICF), 4 'Param3
  RtlMoveMemory ASM(12), VarPtr(IIDCF), 4 'Param2
  RtlMoveMemory ASM(17), VarPtr(CID), 4 'Param1
  RtlMoveMemory ASM(22), pGetClass - VarPtr(ASM(22)) - 4, 4
  Result = CallWindowProc(VarPtr(ASM(0)), 0, 0, 0, 0)
  If Result = &H80040111 Then Err.Clear: Exit Function 'Bad ClassID
  If pICF Is Nothing Then Err.Clear: Exit Function 'couldn't get IClassFactory
  CFs.Add pICF, sFile & sClass
  pICF.CreateInstance pIUNull, IIU, RefIU
  Set GetInstance = RefIU
  Err.Clear
End Function

Private Function ClassExists(TLI As ITypeLib, sClass$, CID As UUID) As Boolean
Dim i&, sName$, Obj As Object, pAttr&
Dim TI As ITypeInfo, TA As TYPEATTR
  On Error Resume Next
  For i = 0 To TLI.GetTypeInfoCount - 1
    If TLI.GetTypeInfoType(i) <> TKIND_COCLASS Then GoTo nxt
    Set TI = TLI.GetTypeInfo(i)
    TI.GetDocumentation DISPID_UNKNOWN, sName, "", 0, ""
    If UCase(sName) <> UCase(sClass) Then GoTo nxt
    pAttr = TI.GetTypeAttr
    MoveMemory TA, ByVal pAttr, Len(TA)
    TI.ReleaseTypeAttr pAttr
    If TA.wTypeFlags Then CID = TA.iid: ClassExists = True: Exit For
nxt: Next i
  Err.Clear
End Function

Private Sub Class_Terminate()
Dim Lib
  Set CFs = Nothing
  For Each Lib In Libs: FreeLibrary Lib: Next
End Sub

Anyway, DC has been used by hundreds for years now without any problem and it is still fully supported in Windows 10. The problem is the unusual way DC returns an instance. While DC is a native DLL, it returns an Object instance:

Private Declare Function GetInstanceEx Lib "DirectCOM" (spFName As Long, spClassName As Long, Optional ByVal UseAlteredSearchPath As Boolean = True) As Object

Notice how the return type is "Object".

DirectCOM and AutoIt

My frustration with this is that I cannot call it in AutoIt, because AutoIt doesn't accept Objects returned by DllCalls. But DC works with AutoIt. To check that it actually works, we create an "airbag" class that calls DC internally at wraps the instance:

Wrapper Module:

Option Explicit

Private Declare Function GetInstanceEx Lib "DirectCOM" (spFName As Long, spClassName As Long, Optional ByVal UseAlteredSearchPath As Boolean = True) As Object
Private Declare Function GETINSTANCELASTERROR Lib "DirectCOM" () As String

Private Declare Function LoadLibraryW& Lib "kernel32" (ByVal lpLibFileName&)
Private Declare Function GetModuleFileNameW& Lib "kernel32" (ByVal hMod&, ByVal lpFileName&, ByVal nSize&)

Public Function GetInstance(FullDllPathFileName As String, ClassName As String) As Object
    LoadLibraryW (StrPtr("C:\rc5\DirectCOM.dll"))
    Set GetInstance = GetInstanceEx(StrPtr(FullDllPathFileName), StrPtr(ClassName), True)
  
    If Err Then
        On Error GoTo 0: Err.Raise vbObjectError, Err.Source & ".GetInstance", Err.Description
    ElseIf GetInstance Is Nothing Then
        On Error GoTo 0: Err.Raise vbObjectError, Err.Source & ".GetInstance", GETINSTANCELASTERROR()
    End If
End Function

And the very simple Class (note that the SET keyword casts the variable to be Object):

Function ExposeInstance(sPath As String, sCls As String) As Object
    Set ExposeInstance = GetInstance(sPath, sCls)
End Function

Compile the above code as ActiveX and register. Then we can test this in AutoIt:

;                        name of wrapper                 server file                        class to instantiate
$cConstructor = ObjCreate("ECOM.Runtime").ExposeInstance(@ScriptDir & "\SomeCOMObject.dll", "SomeClass")

and voila, we have created a new object from a file. And it actually works.

 

The problem

If we call DC from AutoIt without any wrapper, it receives 4 "long" values, because that's the internal structure of an Object. But AutoIt is unable to use the object. So, my question is: How the hell can I call DC from AutoIt and receive the actual object? I know that it works in theory (see above), but that requires another wrapper class, which has to be registered! If it is impossible to use DC, is there any other way I can instantiate a class from a file without any registration in AutoIt?

 

Trivia

BTW, you can download my wrapper class here. If you register this, you can from now on use it (as any user) to create an instance from a file :) .

Edited by minxomat

I will answer every single PM, and you are free to ask anything anytime.

Share this post


Link to post
Share on other sites



I''m not sure but maybe this following link can be handy for you:


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
Share on other sites

God dammit, I was about to tag trancexx because I was sure she'd knew some hack for this. :) . I used my wrapper to get the IID and CLSID and it works as expected. This should be documented somewhere. Thanks @mLipok .


I will answer every single PM, and you are free to ask anything anytime.

Share this post


Link to post
Share on other sites

As this is experimental, this is not documented.


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
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