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
 
LVL 1
RLancasterAsked:
Who is Participating?
 
LischkeConnect With a Mentor Commented:
Hi Robert, fine that you listen again. We have been discussing your question meanwhile without you.

Unfortunately, I'm not sure what you really want. Is it the information? I believe not, because this has been given yesterday by me just a couple of minutes after you asked. So it is the code you are after, right? Although, also here I have doubts since you have not mentioned this fact anywhere. Actually, now three people are here saying all the same with different words and, I believe, the one with the most words issued will win.

Ok, then here are my words again accompanied with code:

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

function IsClassRegistered(GUID: TGUID): Boolean;

var
  ClassID: String;

begin
  with TRegistry.Create do
  try
    ClassID := GUIDToString(GUID);
    RootKey := HKEY_CLASSES_ROOT;

    Result := OpenKey('\CLSID\' + ClassID, False);
  finally
    Free;
  end;
end;


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


procedure RegisterCOMServer(DLLName: String);

var
  DLLHandle: THandle;
  RegFunc: TDllRegisterServer;

begin
  DLLHandle := LoadLibrary(DLLName);
  if Handle <> 0 then
  begin
    RegFunc := GetProcAddress(Handle, 'DllRegisterServer');
    if Assigned(RegFunc) then RegFunc;
    FreeLibrary(DLLHandle);
  end
  else RaiseLastWin32Error;
end;

Ciao, Mike
0
 
LischkeCommented:
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
0
 
hubdogCommented:
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;
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
mullet_attackCommented:
if your com server is an EXE server, simply run it with /register in the command line.
0
 
shenqwCommented:
Tregsvr source code @  $(delphi)\Demos\Activex\Tregsvr (D5)
0
 
tomer_engelCommented:
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
 
0
 
tomer_engelCommented:
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)


0
 
rwilson032697Commented:
Listening
0
 
xsoftCommented:
listening...
0
 
RLancasterAuthor Commented:
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 !!!
0
 
RLancasterAuthor Commented:
Comment accepted as answer
0
 
RLancasterAuthor Commented:
Sorry for taking to long to respond !!! It works like a dream.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.