2

Currently, you will have to regsvr32 COM.dll before you can use the COM object in the application (EXE).

Is there any way to wrap the COM.dll somehow in the EXE when deployment, so that the users don't have to manually register the COM.dll ?

I am using Delphi XE8

justyy
  • 5,831
  • 4
  • 40
  • 73
  • 7
    Yes. It's called registration free COM. A websearch will yield the details. – David Heffernan Nov 06 '15 at 11:38
  • **COM** in general or **COM+**? – Free Consulting Nov 06 '15 at 14:21
  • 2
    Elaborating on @David Heffernan’s tip there seems to be a relevant SO post: http://stackoverflow.com/questions/5074563/registration-free-com-dll – Lars Nov 06 '15 at 14:39
  • i don't know any difference between COM or COM+, i thought they meant the same thing. – justyy Nov 06 '15 at 15:03
  • 2
    The difference is that the term "COM+" implies the use of services such as [just-in-time activation, object pooling, application pooling, (distributed) transaction management, queued components, among other things](https://msdn.microsoft.com/en-us/library/windows/desktop/ms687015(v=vs.85).aspx). If you don't use any of these things, it's simply "COM". – acelent Nov 06 '15 at 16:20
  • ok. thanks. so in my case, it is simply a COM DLL – justyy Nov 06 '15 at 16:21
  • @FreeConsulting pauloMadeira Why cause additional confusion? – Toby Allen Nov 08 '15 at 10:43
  • @TobyAllen, what additional confusion? If there is any, it's already there, [check MSDN, it separates COM and COM+](https://msdn.microsoft.com/en-us/library/ee663297(v=vs.85).aspx). It used to be worse. The thing is that the question is about base COM, not about things that depend on COM+ services. – acelent Nov 09 '15 at 13:14
  • @TobyAllen, another thing is that registration-free COM doesn't support some types of COM+ components. For instance, if you have application pooling, or if your component is queued, the component needs to be registered globall. However, for base COM, registration-free COM works for most common cases. Even so, it doesn't work for things that require further registration, such as component categories. – acelent Nov 09 '15 at 13:26

2 Answers2

5

I recently was looking into something similar, and it turns out it's pretty straight-forward to do. A COM DLL has to export a DllGetClassObject entry among a few others. In general the selected development platform does this for you, but in the final DLL it's right there, both available for use by the COM sub-system, but just as well by yourself with code like this:

type
  T_DGCO=function(const CLSID, IID: TGUID; var Obj): HResult; stdcall;//DllGetClassObject
var
  p:T_DGCO;
  f:IClassFactory;
  x:IMyObject;//replace by an interface of choice
begin
  p:=GetProcAddress(LoadLibrary(FullPathToDLL),'DllGetClassObject');
  if @p=nil then RaiseLastOSError;
  OleCheck(p(CLASS_MyObject,IClassFactory,f));
  OleCheck(f.CreateInstance(nil,IMyObject,x));
  x.Hello('World');//or whatever your object does
end;
Stijn Sanders
  • 35,982
  • 11
  • 45
  • 67
-3
function RegisterServer(const aDllFileName: string; aRegister: Boolean): Boolean;
type
  TRegProc = function: HResult;
  stdcall;
const
  cRegFuncNameArr: array [Boolean] of PChar =
    ('DllUnregisterServer', 'DllRegisterServer');
var
  vLibHandle: THandle;
  vRegProc: TRegProc;
begin
  Result := False;
  vLibHandle := LoadLibrary(PChar(aDllFileName));
  if vLibHandle = 0 then Exit;
    @vRegProc := GetProcAddress(vLibHandle, cRegFuncNameArr[aRegister]);
  if @vRegProc <> nil then
    Result := vRegProc = S_OK;
  FreeLibrary(vLibHandle);
end;
Jens Borrisholt
  • 6,174
  • 1
  • 33
  • 67
  • 1
    This answer seems plagiarised from, and identical to, code in http://www.swissdelphicenter.ch/en/showcode.php?id=1281 without acknowledgement or attribution. ? – MartynA Nov 06 '15 at 12:48
  • @MartynA no it actually came from my local library. And that might have originated from that site. I don't remember. But still it is an working answer to the question. – Jens Borrisholt Nov 06 '15 at 12:49
  • 2
    IMO, it doesn't deserves that downvote spree, but idea of registering COM server on the fly is rather bad than good due the privileges required. – Free Consulting Nov 06 '15 at 14:19
  • 1
    can you execute this code as Everyone User without elevation? – kobik Nov 06 '15 at 15:16
  • Usually if I see code on the net and wish to use it, and there is no copyright notice I'll try to contact the author, find out what license the code is available for and then decide to put it in my project. The one thing I will always do is state the original Author, where it was found etc and place that information in the code comments. That way if I ever want to use it (like posting on SO) I'll remember, oh yeah I snatched that from someone else and can attribute it to the real author and not myself. Some sites of course have a license for code contributed by its users. – Michael Petch Nov 07 '15 at 00:50
  • What this code achieves is the same as running `regsvr32 dllname` or `regsvr32 /u dllname`, effectively (un)registering the DLL globally. – acelent Nov 09 '15 at 13:18