calling COM using CreateObject ( late bind )
-
hi i have a COM definitions as interface IMyInterface : IDispatch { [id(1), helpstring("method SetUserTicket")] HRESULT SetUserTicket([in] BSTR bstrTicket, [out,retval] VARIANT *varRetVal); }; [ uuid(), version(1.0), helpstring("My Type Library") ] library MYLib { uuid(), helpstring("My Class") coclass MyApis { [default] interface IMyInterface; }; }; The output dll was formed as MY.dll using reference add the following line works in vb dim aa as MYLib.MyApis aa.SetUserTicket() But when using CreateObject function as CreateObject("MYLib.MyApis","") it was not working . Is the function call is correct . or i need to pass any other names to the function.
vineesh
-
hi i have a COM definitions as interface IMyInterface : IDispatch { [id(1), helpstring("method SetUserTicket")] HRESULT SetUserTicket([in] BSTR bstrTicket, [out,retval] VARIANT *varRetVal); }; [ uuid(), version(1.0), helpstring("My Type Library") ] library MYLib { uuid(), helpstring("My Class") coclass MyApis { [default] interface IMyInterface; }; }; The output dll was formed as MY.dll using reference add the following line works in vb dim aa as MYLib.MyApis aa.SetUserTicket() But when using CreateObject function as CreateObject("MYLib.MyApis","") it was not working . Is the function call is correct . or i need to pass any other names to the function.
vineesh
Did you create all the registry entries for your COM Server? When you add a reference to the .dll, VB reads the .dll and accesses it directly. When you use
CreateObject(ObjectID)
, COM looks up theObjectID
in the registry to get its GUID and then looks up the GUID in the registry to find your .dll and its type library. To be able to useCreateObject()
, there are several registry entries needed. Your library is MY.dll, so let's call the typelib MY.tlb. These are the minimum entries you need:HKEY_CLASSES_ROOT\\MYLib.MyApis = "My COM Server" _(Friendly name)_ HKEY_CLASSES_ROOT\\MYLib.MyApis\\CLSID = GUID_of_Class_MyApis HKEY_CLASSES_ROOT\\CLSID\\GUID_of_Class_MyApis = "My COM Server" _(Friendly name)_ HKEY_CLASSES_ROOT\\CLSID\\GUID_of_Class_MyApis\\InProcServer32 = "[PATH]\MY.dll" ThreadingModel = "Apartment" (This one is a value under InProcServer32, not a key) HKEY_CLASSES_ROOT\\CLSID\\GUID_of_Class_MyApis\\ProgID = "MYLib.MyApis" HKEY_CLASSES_ROOT\\CLSID\\GUID_of_Class_MyApis\\TypeLib = GUID_of_TypeLib HKEY_CLASSES_ROOT\\CLSID\\GUID_of_Class_MyApis\\Version = "1.0" HKEY_CLASSES_ROOT\\Interface\\GUID_of_Interface_IMyInterface = "My COM Interface" _(Friendly name)_ HKEY_CLASSES_ROOT\\Interface\\GUID_of_Interface_IMyInterface\\TypeLib = GUID_of_TypeLib HKEY_CLASSES_ROOT\\Interface\\GUID_of_Interface_IMyInterface\\Version = "1.0" HKEY_CLASSES_ROOT\\TypeLib\\GUID_of_TypeLib HKEY_CLASSES_ROOT\\TypeLib\\GUID_of_TypeLib\\1.0 = "My COM Server Type Library" _(Friendly name)_ HKEY_CLASSES_ROOT\\TypeLib\\GUID_of_TypeLib\\1.0\\0 HKEY_CLASSES_ROOT\\TypeLib\\GUID_of_TypeLib\\1.0\\0\\win32 = "[PATH]\MY.tlb"
Technically there is a versioning issue here, so your \\GUID_of_Class_MyApis\\ProgID value should be "MYLib.MyApis.1" and then you'd create a VersionIndependentID key to associate "MYLib.MyApis" with "MYLib.MyApis.1"... but this isn't strictly necessary. Though you can create them by hand using RegEdit, all of these keys should be created in your DLL'sDllRegisterServer()
method and they should be deleted in your DLL'sDllUnregisterServer()
method, for use with RegSvr32.exe Hope this helps, MZR