2

I'm completely new to free-pascal and I try to implement a simple dll that should register a COM class. Unfortunately I could only find little information about COM Programming for freepascal. Thus I hope that someone here can give me some hints or even a link to some examples.

So here is what I did:

  • my operating system is Windows 7 64 bit
  • downloaded and installed Lazarus 32bit version
    • Version #: 1.2.6
    • Date: 2014-10-11
    • FPC: Version 2.6.4
    • SVN Revision: 46529
    • i386-win32-win32/win64
  • installed the ActiveX package in Lazarus
  • made a new project - type Library with a simple TAutoObject and a default TAutoObjectFactory for the COM registration: source code included after this description
  • build the dll
  • use regsvr32.exe to register my dll --> this fails with

    "make sure the binary is stored at the specified path ..."
    Invalid access to memory location.

  • then I tried to change the default project options:
    under Compiler Options - Config and Target, I set
    • Target OS: Win32
    • Target CPU family: i386
  • still the same error occurs
Project source
library LazarusSimpleComRegTest;

{$mode objfpc}{$H+}

uses
  Classes,
  { you can add units after this }
  ComServ, MyComObj;

exports
  DllGetClassObject,
  DllCanUnloadNow,
  DllRegisterServer,
  DllUnregisterServer;

end.           

MyComObj Unit:

unit MyComObj;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, ComObj;

const
  CLASS_Plugin: TGUID = '{5E020FB0-B593-4ADF-9288-801C2FD432CF}';

type
  TPlugin = class(TAutoObject)

  end;

implementation

uses ComServ;

initialization
  TAutoObjectFactory.Create(ComServer, TPlugin, CLASS_Plugin,
    ciMultiInstance, tmApartment);

end.
TmTron
  • 17,012
  • 10
  • 94
  • 142
  • Did you run the command prompt with admin rights ? – TLama Nov 18 '14 at 16:17
  • yes - I have admin rights – TmTron Nov 18 '14 at 17:02
  • 1
    You declared the 4 required COM server exports but you didn't write them. It isn't automagic. Google "freepascal dllregisterserver" to find example code, [like this](https://github.com/alrieckert/freepascal/blob/master/packages/winunits-base/src/comserv.pp). – Hans Passant Nov 18 '14 at 17:10
  • 1
    those methods are implemented in ComServ which I include in my uses-list (I'm not sure if `ComServ` is part of the standard free-pascal or or the ActiveX package) - so they should be exported, right? – TmTron Nov 18 '14 at 17:14
  • 1
    alos: if the functions did not exist, I could not even build the dll. – TmTron Nov 19 '14 at 08:39

1 Answers1

0

I think the main problem was, that I did not include the type library as a resource in my dll file: Now it works fine.

I've made a very basic and simple working example on git-hub with some basic documentation: lazarus-com-example

TmTron
  • 17,012
  • 10
  • 94
  • 142