.NET Forum / .NET Framework / Interop / August 2006
Licensed COM Object
|
|
Thread rating:  |
juan@msdnCompany.com - 03 Aug 2006 23:00 GMT Is there a way to create a COM object that is licensed using the .NET COM wrapper? I want to supply the COM object with a run time key and do not understand how I go about doing it after adding the COM library as reference in my C# project. I see a lot of discussion when using an ActiveX Control, but I don't see how it is done when the COM object is not part of a control. Is there a way to use IClassFactory2 to instantate the COM Object?
MyCOMObject obj = new MyCOMObject(); -- Generates an error saying it is not licensed
Like to do: MyCOMObject obj = (MyCOMObject)xxxx.CreateInstanceLic(runtimeStr);
I searched and did not see anyone with a solution. If there is a link that would head me in the right direction, I would appreciate it.
 Signature Thanks, Juan
"Peter Huang" [MSFT] - 04 Aug 2006 07:57 GMT Hi Juan,
From your description, I understand that you wanted to use a COM Object without UI in .NET which need license information. Have I fully understood you? If there is anything I misunderstood, please feel free to let me know.
Here is some code snippet for your reference. If a control does not need to be sited on a form (like mscomm control) can use the following:
Imports System.Runtime.InteropServices Public Class Form1 Inherits System.Windows.Forms.Form <DllImport("Ole32.dll", ExactSpelling:=True, PreserveSig:=False)> _ Public Shared Function CoGetClassObject(ByRef clsid As Guid, ByVal dwContext As Integer, ByVal serverInfo As Integer, ByRef reiid As Guid) As IClassFactory2 End Function Private Const INPROC_SERVER As Integer = 1 Private IID_IUnknown As Guid = New Guid("{00000000-0000-0000-C000-000000000046}") <ComImport(), Guid("B196B28F-BAB4-101A-B69C-00AA00341D07"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _ Public Interface IClassFactory2 Sub CreateInstance( _ <MarshalAs(UnmanagedType.Interface)> ByVal unused As Object, _ ByRef refiid As Guid, <MarshalAs(UnmanagedType.LPArray)> ByRef ppunk As Object) Sub LockServer(ByVal fLock As Integer) Sub GetLicInfo(ByVal licInfo As IntPtr) Sub RequestLicKey(<MarshalAs(UnmanagedType.U4)> ByVal dwReserved As Integer, <MarshalAs(UnmanagedType.LPArray)> ByVal pBstrKey As String()) Sub CreateInstanceLic(<MarshalAs(UnmanagedType.Interface)> ByVal _ pUnkOuter As Object, _ <MarshalAs(UnmanagedType.Interface)> ByVal pUnkReserved As Object, _ ByRef riid As Guid, _ <MarshalAs(UnmanagedType.BStr)> ByVal _ bstrKey As String, _ <MarshalAs(UnmanagedType.Interface)> ByRef _ ppVal As Object) End Interface Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim icf2_Guid As Guid = New Guid("B196B28F-BAB4-101A-B69C-00AA00341D07") Dim ppval As Object Dim gd As Guid = New Guid("6262D3A0-531B-11CF-91F6-C2863C385E30") 'Clsid Dim te As String = "72E67120-5959-11cf-91F6-C2863C385E30" 'License Key Dim icf2 As IClassFactory2 icf2 = CoGetClassObject(gd, 1, 0, icf2_Guid) 'Sets license icf2.CreateInstanceLic(0, 0, IID_IUnknown, te, ppval) 'This code will test about box in flex grid Dim msflex As MSFlexGridLib.MSFlexGridClass msflex = CType(ppval, MSFlexGridLib.MSFlexGridClass) msflex.AboutBox() End Sub End Class
Please Apply My Suggestion Above And Let Me Know If It Helps Resolve Your Problem.
However, if you have concern, please feel free to let me know.
Best regards,
Peter Huang
Microsoft Online Community Support ================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights.
juan@msdnCompany.com - 04 Aug 2006 19:24 GMT You were doing what I was trying to do, but I had done a couple of things wrong. 1) I had inherited IClassFactory2 from IClassFactory and once I did factory2 by itself the createInstanceLic actually returned me something. 2) Then in the CreateInstanceLic call I was using the clsId of my object, not IUnknown. That caused the app and debugger to "go crazy" when trying to use the object.
Below Is my support class and implementation if people get stuck like me doing something I would have thought should be simple.
I really appreciated your help, because I would not have had a working program without your sample code.
 Signature Thanks, Juan
namespace COMInterop { class Utils { public enum CLSCTX { INPROC_SERVER = 0x1, INPROC_HANDLER = 0x2, LOCAL_SERVER = 0x4, INPROC_SERVER16 = 0x8, REMOTE_SERVER = 0x10, INPROC_HANDLER16 = 0x20, RESERVED1 = 0x40, RESERVED2 = 0x80, RESERVED3 = 0x100, RESERVED4 = 0x200, NO_CODE_DOWNLOAD = 0x400, RESERVED5 = 0x800, NO_CUSTOM_MARSHAL = 0x1000, ENABLE_CODE_DOWNLOAD = 0x2000, NO_FAILURE_LOG = 0x4000, DISABLE_AAA = 0x8000, ENABLE_AAA = 0x10000, FROM_DEFAULT_CONTEXT = 0x20000, ACTIVATE_32_BIT_SERVER = 0x40000, ACTIVATE_64_BIT_SERVER = 0x80000 };
[DllImport("ole32.dll", ExactSpelling = true, CharSet = CharSet.Unicode)] public static extern int CoGetClassObject( ref Guid rclsid, CLSCTX dwClsContext, IntPtr pServerInfo, ref Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppv);
public static readonly Guid IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046"); public static readonly Guid IID_IClassFactory = new Guid("00000001-0000-0000-C000-000000000046"); public static readonly Guid IID_IClassFactory2 = new Guid("B196B28F-BAB4-101A-B69C-00AA00341D07");
/// IClassFactory definitions /// </summary> [ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000001-0000-0000-C000-000000000046"),] public interface IClassFactory { int CreateInstance([MarshalAs(UnmanagedType.Interface)] object pUnkOuter, [In] ref Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppvObject);
void LockServer([In] int fLock); }
/// <summary> /// IClassFactory2 definitions /// </summary> [ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("B196B28F-BAB4-101A-B69C-00AA00341D07"),] public interface IClassFactory2 { int CreateInstance([MarshalAs(UnmanagedType.Interface)] object pUnkOuter, [In] ref Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppvObject);
void LockServer([In] int fLock);
int GetLicInfo(IntPtr licInfo); int RequestLicKey([In] UInt32 dwReserved, out string pbstrKey);
int CreateInstanceLic([MarshalAs(UnmanagedType.Interface)] object pUnkOuter, [MarshalAs(UnmanagedType.Interface)] object pUnkReserved, [In] ref Guid riid, [MarshalAs(UnmanagedType.BStr)] string bstrKey, [Out, MarshalAs(UnmanagedType.Interface)] out object ppvObject); } } }
Using the above classes:
Guid iClassFactoryIID = COMInterop.Utils.IID_IClassFactory2; int result = COMInterop.Utils.CoGetClassObject(ref myObjCLSID,COMInterop.Utils.CLSCTX.INPROC_SERVER, IntPtr.Zero, ref iClassFactoryIID, out factory); Marshal.ThrowExceptionForHR(result);
object voidObj = null; Guid unknownCLSID = COMInterop.Utils.IID_IUnknown; COMInterop.Utils.IClassFactory2 factory2 = (COMInterop.Utils.IClassFactory2)factory; result = factory2.CreateInstanceLic(null, null, ref unknownCLSID, keyStr, out voidObj); Marshal.ThrowExceptionForHR(result);
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|