Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

IClassFactory (Interfaces)
 
.

This interface is used to instantiate a COM Object with a licence key

C# Definition:

[ComImport]
[Guid("B196B28F-BAB4-101A-B69C-00AA00341D07")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IClassFactory2
{
   /// <summary>
   /// the standard create instance (without licence)
   /// </summary>
   /// <param name="unused">must be set to null</param>
   /// <param name="iid">the Guid of the COM class to create</param>
   /// <returns>an instance of the COM class</returns>
   [return: MarshalAs(UnmanagedType.Interface)]
   Object CreateInstance(
     [In, MarshalAs(UnmanagedType.Interface)] Object unused,
     [In, MarshalAs(UnmanagedType.LPStruct)] Guid iid);

   void LockServer(Int32 fLock);

   IntPtr GetLicInfo(); // TODO : an enum called LICINFO

   [return: MarshalAs(UnmanagedType.BStr)]
   String RequestLicKey(
     [In, MarshalAs(UnmanagedType.U4)] int reserved);

   /// <summary>
   /// create an instance of the COM class thanks to a licence key
   /// </summary>
   /// <param name="pUnkOuter">don't know what it is, set to null</param>
   /// <param name="pUnkReserved">must be set to null</param>
   /// <param name="iid">the Guid of the COM class to create</param>
   /// <param name="bstrKey">the licence key</param>
   /// <returns>an instance of the COM class</returns>
   [return: MarshalAs(UnmanagedType.Interface)]
   Object CreateInstanceLic(
     [In, MarshalAs(UnmanagedType.Interface)] object pUnkOuter,
     [In, MarshalAs(UnmanagedType.Interface)] object pUnkReserved,
     [In, MarshalAs(UnmanagedType.LPStruct)] Guid iid,
     [In, MarshalAs(UnmanagedType.BStr)] string bstrKey);
}

VB Definition:

    <ComImport>
    <Guid("B196B28F-BAB4-101A-B69C-00AA00341D07")>
    <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
    Interface IClassFactory2
    Function CreateInstance(<MarshalAs(UnmanagedType.[Interface])> unused As Object,
                <MarshalAs(UnmanagedType.LPStruct)> iid As Guid) As <MarshalAs(UnmanagedType.[Interface])> Object
    Sub LockServer(ByVal fLock As Int32)
    Function GetLicInfo() As IntPtr
    Function RequestLicKey(<MarshalAs(UnmanagedType.U4)> reserved As Integer) As <MarshalAs(UnmanagedType.BStr)> String
    Function CreateInstanceLic(<MarshalAs(UnmanagedType.[Interface])> pUnkOuter As Object,
                   <MarshalAs(UnmanagedType.[Interface])> pUnkReserved As Object,
                   <MarshalAs(UnmanagedType.LPStruct)> iid As Guid,
                   <MarshalAs(UnmanagedType.BStr)> bstrKey As String) As <MarshalAs(UnmanagedType.[Interface])> Object
    End Interface

    <Flags>
    Enum CLSCTX As UInteger
    CLSCTX_INPROC_SERVER = &H1
    CLSCTX_INPROC_HANDLER = &H2
    CLSCTX_LOCAL_SERVER = &H4
    CLSCTX_INPROC_SERVER16 = &H8
    CLSCTX_REMOTE_SERVER = &H10
    CLSCTX_INPROC_HANDLER16 = &H20
    CLSCTX_RESERVED1 = &H40
    CLSCTX_RESERVED2 = &H80
    CLSCTX_RESERVED3 = &H100
    CLSCTX_RESERVED4 = &H200
    CLSCTX_NO_CODE_DOWNLOAD = &H400
    CLSCTX_RESERVED5 = &H800
    CLSCTX_NO_CUSTOM_MARSHAL = &H1000
    CLSCTX_ENABLE_CODE_DOWNLOAD = &H2000
    CLSCTX_NO_FAILURE_LOG = &H4000
    CLSCTX_DISABLE_AAA = &H8000
    CLSCTX_ENABLE_AAA = &H10000
    CLSCTX_FROM_DEFAULT_CONTEXT = &H20000
    CLSCTX_INPROC = CLSCTX_INPROC_SERVER Or CLSCTX_INPROC_HANDLER
    CLSCTX_SERVER = CLSCTX_INPROC_SERVER Or CLSCTX_LOCAL_SERVER Or CLSCTX_REMOTE_SERVER
    CLSCTX_ALL = CLSCTX_SERVER Or CLSCTX_INPROC_HANDLER
    End Enum

    <DllImport("ole32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True, PreserveSig:=False)>
    Function CoGetClassObject(<MarshalAs(UnmanagedType.LPStruct)> rclsid As Guid,
                     dwClsContext As CLSCTX,
                     pServerInfo As IntPtr,
                     <MarshalAs(UnmanagedType.LPStruct)> riid As Guid) As <MarshalAs(UnmanagedType.[Interface])> Object
    End Function

User-Defined Types:

TODO : specify struct to replace all IntPtr

Notes:

You can instantiate a IClassFactory2 thanks to the CoGetClassObject (from OLE32.DLL)

The "Object" return by methods can be cast to the specified type.

Exemple :

    String LicenceKey = "haha";
    IClassFactory2 icf2 = Ole32.CoGetClassObject(
        typeof(Interop.MyComLibrary.ComMyLibraryClass).GUID,
        CLSCTX.CLSCTX_LOCAL_SERVER, new System.IntPtr(),
        typeof(IClassFactory2).GUID) as IClassFactory2;
    Interop.MyComLibrary.IMyComClass instance = icf2.CreateInstanceLic(
        null, null, typeof(Interop.MyComLibrary.IMyComClass).GUID, LicenceKey) as Interop.MyComLibrary.IMyComClass;

Example vb.net :

    Dim strLicense As String = "haha"
    Dim classFactory As IClassFactory2 = CoGetClassObject(GetType(ComMyLibraryClass).GUID,CLSCTX.CLSCTX_LOCAL_SERVER,
                                  IntPtr.Zero, GetType(IClassFactory2).GUID)
    Dim instance As IMyComClass = classFactory.CreateInstanceLic(Nothing, Nothing, GetType(IMyComClass).GUID, strLicense)

Documentation

Please edit this page!

Do you have...

  • helpful tips?
  • corrections to the existing content?
  • alternate definitions?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing any supporting types needed.

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions