Solved

Registring COM objects at runtime

Posted on 2000-05-03
12
303 Views
Last Modified: 2010-04-04
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
 
0
Comment
Question by:RLancaster
  • 3
  • 2
  • 2
  • +5
12 Comments
 
LVL 10

Expert Comment

by:Lischke
ID: 2772572
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
 
LVL 2

Expert Comment

by:hubdog
ID: 2772592
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
 
LVL 2

Expert Comment

by:mullet_attack
ID: 2772870
if your com server is an EXE server, simply run it with /register in the command line.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:shenqw
ID: 2773270
Tregsvr source code @  $(delphi)\Demos\Activex\Tregsvr (D5)
0
 
LVL 1

Expert Comment

by:tomer_engel
ID: 2774869
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
 
LVL 1

Expert Comment

by:tomer_engel
ID: 2774994
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
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2775191
Listening
0
 
LVL 1

Expert Comment

by:xsoft
ID: 2775621
listening...
0
 
LVL 1

Author Comment

by:RLancaster
ID: 2776443
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
 
LVL 10

Accepted Solution

by:
Lischke earned 200 total points
ID: 2776755
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
 
LVL 1

Author Comment

by:RLancaster
ID: 2866575
Comment accepted as answer
0
 
LVL 1

Author Comment

by:RLancaster
ID: 2866576
Sorry for taking to long to respond !!! It works like a dream.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question