Solved

Registring COM objects at runtime

Posted on 2000-05-03
12
299 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now