Link to home
Start Free TrialLog in
Avatar of RLancaster
RLancaster

asked on

Registring COM objects at runtime

Greetings,

COM objects are usually registered using regsvr32.exe or such like. Is there any way of registering them programmatically. In other words if an application uses a COM object and it is not registered, the application must be able register it.

My questions are:

1) How can I determine if my COM object is registered

2) How can I register a COM object at run time

Thanks,

Robert Lancaster
 
Avatar of Lischke
Lischke

Hi Robert,

actually, regsvr32 also registers a COM object at runtime. It loads the DLL into memory and calls its DLLRegisterServer. This in turn triggers the ComServer instance to call the registration procedure of an eventual type lib and all COM factories of the DLL/application. Of course you can do this too.


1) Examine the registry for the 'CLSID\' + ClassID key for your COM object.

2) Call DLLRegisterServer of your DLL (do a LoadLibrary and a GetProcAddress).

Ciao, Mike
uses OLEctl,....
var
OCXHand: THandle;
RegFunc: TDllRegisterServer;
begin
OCXHand:= LoadLibrary('c:\windows\system\your.dll');//or your.ocx
RegFunc:= GetProcAddress(OCXHand, 'DllRegisterServer');
if RegFunc <> 0 then ShowMessage('Error!');
FreeLibrary(OCXHand);
end;
if your com server is an EXE server, simply run it with /register in the command line.
Tregsvr source code @  $(delphi)\Demos\Activex\Tregsvr (D5)
ok lets get some things stright:

1)if you're writing the server you sould implement some basic function in the server--if that's the case contact me again i'll help ya.

2)to check if the server is registered check out the registry using the regedit.exe located in the windows\system under HKEY_CLASSES_ROOT\CLSID and see if the server is  regutered, if you don't know the class id then ask i'll tell you how to figure it out.

3)if you hadn't wirtten it use the regsvr32.exe :beacuse every exe server is supplied(/or created if you create the file) with a dll that supplies the stub and the proxy servises,, this is what should be registered, lets say the dll is mydllServer.dll
write this line down(in the dos prompt or in a batch file )

regsvr32 -s mydllServer.dll
(to find out about more of the regsvr.exe switches just enter regsvr/? in the dos prompt.

as i said you can supply you're server with a bacth file (a file with a .bat extention) that only have the reigistration line as above

if you have more questions;ask

Tomer
 
forgot somehitng:

you asked about runtime ,so add this code where you want to check if the server s regitered(and add the Registry and the ShellAPI packages in the uses cluase)

var
  r:Tregistry;
..
..
..
begin
..
..

r:= TRegistry.create(HKEY_CLASSES_ROOT);
try
 r.OpenKey('CLSID',False);
//check to see if the server is registered
 if  r.ReadString('the CLSID of the component here') = ''
   then//class not regitered--run the batch file
     shellexecute( handle,PChar('OPEN'),PChar('your bacth file here'),PChar(''),PChar('the dirctory to the file here'),SW_MINIMIZE);
finally
 r.free;end;//the try->finally

the bacth file is the bacth file you prepered earilear(or try insted of the bacth file in the Shellexecute to, put the regsvr32.exe,, i didn't have time to try it out (sorry)


Listening
listening...
Avatar of RLancaster

ASKER

instead of using the batch file could you use the piece of code offered b hubdog near the top of this question ?

things are looking up though !!!
ASKER CERTIFIED SOLUTION
Avatar of Lischke
Lischke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Comment accepted as answer
Sorry for taking to long to respond !!! It works like a dream.