Development :  K-Meleon Web Browser Forum
K-Meleon development related discussions. 
Creating plugins in delphi?
Posted by: Rob Vonk
Date: December 05, 2001 10:51AM

I'm trying to build a plugin in Delphi but i'm stuck..I'm sure the code that i wrote isn't the best in the world but i hope that someone with Delpi AND C knowledge can tell me what i'm doing wrong..

I made some structs in Delphi. I don't have any Functions defined so the pluginsfuncitons record only has some pointers instead of pointers to functions.

Every time i try to load the plugin ( by loading kmeleon with the plugin in the plugin dir) it gives an exception..

Here's the code..

Rob

library my_k_meleon_plugin;

uses
SysUtils,
Classes;

const
KMEL_PLUGIN_VER = $10;

type
TkmeleonDocInfo = record
title : PChar;
url : PChar;
end;
PkmeleonDocInfo = ^TkmeleonDocInfo;

TcommandIDs = Function ( num : integer) : Integer;
TNavigateTo = Procedure(url : PChar; WindowState : Integer);
TGetDocInfo = Function (mainWnd : Integer) : PkmeleonDocInfo;
TGetPreference = Procedure (ptype : Integer; Preference : PChar; Result : Pointer; defaulValue : Pointer);
TSetPreference = Procedure (ptype : Integer; Preference : PChar; Value : Pointer);
TGetMozillaSessionHistory = Function (titles : PChar; Count: Integer; Index : Integer) : Integer;
TGotoHistoryIndex = Procedure (index : Integer);
TRegisterBand = Procedure (hWnd : Integer; name : PChar; VisibleOnMenu : Integer);

TkmeleonFunctions = record
// this function allocates <num> successive ids for the plugin, then returns the first one.
// use it to get an unused command id. this way plugins won't step on others toes.
GetCommandIDs : ^TCommandIDs;
NavigateTo : ^TNavigateTo;
GetDocInfo : ^TGetDocInfo;
GetPreference : ^TGetPreference;
SetPreference : ^TSetPreference;
GetMozillaSessionHistory : ^TGetMozillaSessionHistory;
GotoHistoryIndex : ^TGotoHistoryIndex;
RegisterBand : ^TRegisterBand;
end;
pkmeleonFunctions = ^TkmeleonFunctions;

type
pluginFunctions = record
Init : Pointer;
Create : Pointer;
Config : Pointer;
Quit : Pointer;
DoMenu : Pointer;
Dorebar : Pointer;
DoAccel : Pointer;
end {pluginFunctions};
ppluginFunctions = ^pluginFunctions;

type
HINSTANCE = LongWord;

type
TkmeleonPlugin = record
{/// Filled in by the plugin }
version: Integer;
description: PChar;
pf: PPLUGINFUNCTIONS;
{/// Filled in by k-meleon }
hParentInstance: HINSTANCE;
hDllInstance: HINSTANCE;
kf: PKMELEONFUNCTIONS;
dllname: PChar;
loaded: Boolean;
end {kmeleonPlugin};

var
PluginFunctionsArray : PPluginFunctions;
kmeleonFunctions : pkmeleonFunctions;

Function GetKmeleonPlugin : TkmeleonPlugin;
begin
With result do begin
new(PluginFunctionsArray);
PluginFunctionsArray.Init := nil;
PluginFunctionsArray.Create := nil;
PluginFunctionsArray.Config := nil;
PluginFunctionsArray.Quit := nil;
PluginFunctionsArray.DoMenu := nil;
PluginFunctionsArray.Dorebar := nil;
PluginFunctionsArray.DoAccel := nil;

New(kmeleonFunctions);

version := KMEL_PLUGIN_VER;
description := 'My Delphi plugin';
pf := PluginFunctionsArray;
end;
end;

exports
GetKmeleonPlugin;

{$R *.RES}

begin


end.

Options: ReplyQuote
Re: Creating plugins in delphi?
Posted by: Brian
Date: December 05, 2001 05:23PM

Well, I don't know Delphi, but I do know the plugin system quite well (since I wrote it smiling smiley)

Everything *looks* ok (though that doesn't really say much). You may want to check to make sure delphi isn't aligning the structure. In C, you have to use #pragma pack to get it to align on bytes rather than words or double words.

Can you step through the code in delphi? I'm not sure how advanced their debugger is, but I know in VC++ you can debug a dll without actually debugging the program that uses it.

Is it crashing in kmeleon, or in your plugin?

Options: ReplyQuote
Re: Creating plugins in delphi?
Posted by: Brian
Date: December 05, 2001 05:26PM

Wait, I just realized you don't need to make sure it's byte-aligned, you just need to make sure it's using the same alignment that we're using. I actually have no idea what alignment we're using, so I'm thinking the in next version we'll byte-align the structure to make it easier for plugin authors.

Options: ReplyQuote
Re: Creating plugins in delphi?
Posted by: Rob Vonk
Date: December 06, 2001 08:20AM

Unfortunatly I can't debug the dll without running the 'host' program. I'm not sure what you mean with 'aligning the structure' and 'to align on bytes'. Are you going to use bytes instead of integers for the new plugin structs?

Any idea when the new version is about to release? Maybe i'd better wait for that then.

Rob

Options: ReplyQuote
Re: Creating plugins in delphi?
Posted by: Brian
Date: December 06, 2001 05:53PM

compilers will often times align structures on word boundries (a word is 2 bytes) to make the program run faster. Processors can access memory on a word boundry much faster than it can if the variable is not on a word boundry. If you have 2 pieces of code that share a bit of memory (the kmeleonplugin structure in this case), you have to be 100% sure that the physical layout of the structure is *exactly* the same. When one bit of code is aligning it on word boundries, and some other code is aligning it on bytes or even double words, then you get 2 structures that don't match. That's a bad thing (tm).

Options: ReplyQuote


K-Meleon forum is powered by Phorum.