PDA

View Full Version : linking DLL's..



M109uk
10-03-2003, 12:55 AM
Hi all,

i have a basic plugin system using DLL's with certain procedures in them, ie:

function cxHeader: TcxPluginHeader; stdcall;
..
procedure cxInitialize; stdcall;
..
procedure cxRender; stdcall;
..
procedure cxFinalize; stdcall;
..

and these are exported, then i created a class structor to use to call these plugins:

class
cxHeader: function: TcxPluginHeader;
cxInitialize: procedure;
cxRender: procedure;
cxFinalize: procedure;
hPlugin: Integer;

and in the constructor i have the following code:
// cxg is a dll
hPlugin := LoadLibrary('plugin.cxg');
if hPlugin = 0 Then
begin
FreeLibrary(hPlugin);
Exit;
end;
@cxHeader := GetProcAddress(hPlugin, 'cxHeader');
@cxInitialize := GetProcAddress(hPlugin, 'cxInitialize');
@cxRender := GetProcAddress(hPlugin, 'cxRender');
@cxFinalize := GetProcAddress(hPlugin, 'cxFinalize');

try
Header := cxHeader;
excpet
showmessage('failed');
end;

this all seems to work and it is all created, but once everything has been created i get a
Debugger fault notification
saying:
Project H:\cxgViewer.exe faulted with message: 'access violation at 0x004638fd: Read of address 0x00000028' Process stopped. Use step or run to continue.

i don't seem to be getting any information back from the dll, is their some thing that i am doing wrong?

i have tried looking around on the internet and they mention the same method that im using or using the external command.
This is really confusing me :?

thanks for any help

Alimonster
10-03-2003, 11:12 AM
You have to declare your function pointers with stdcall before using them - this is important, since stdcall isn't the default calling convention. i.e.,

cxHeader: function: TcxPluginHeader; stdcall;
cxInitialize: procedure; stdcall;
cxRender: procedure; stdcall;
cxFinalize: procedure; stdcall;

Make sure that you've exported the functions from the DLL too! It's could be a good move to specify a name in the exports clause for each exported proc/func, just in case...

Do you not want to call FreeLibrary only if the DLL loads okay later on, once you're done with the function pointers? I tested the FreeLibrary on a non-existant library and it returned 0, indicating an error. I can't say that I've looked at DLLs much, though, so that might be wrong, but still...

As an aside: Sly has written a pretty cool OpenGL terrain generator. You might want to have a look because it uses a DLL-based plug-in system (I think it's over here (http://turbo.gamedev.net/ScapeGen.zip)).

M109uk
10-03-2003, 11:43 AM
hi,

thanks for your reply.

i have tried it with and without stdcall in both the DLL and the Application, and it does the same.

in the DLL they are being exported like:


Exports
cxHeader name 'cxHeader',
cxInitialize name 'cxInitialize',
cxRender name 'cxRender',
cxFinalize name 'cxFinalize';


i have tried it with and without 'Name' but still the same thing happens. its like it is'nt being loaded up correctly, i am getting a Handle value but thats is about it.

i used 'FreeLibrary' when the handle value is 0 and when the plugin manager is destroyed.

great thanks, i will have a look :)

Alimonster
10-03-2003, 11:51 AM
Here's a quick DLL test project for you, basically nabbed from the help files: dlltest.zip (http://www.alistairkeys.co.uk/download/dlltest.zip) (12K). Maybe the solution's in there.

M109uk
10-03-2003, 12:07 PM
Thanks :D

I have looked through them both and i changed some parts and now works perfectly (well almost :roll: )

i had:


TManager = class
private
cxHeader: function: TcxPluginHeader; stdcall;
....

and changed it to:


TcxHeader = function: TcxPluginHeader; stdcall;
....
TManager = class
private
cxHeader: TcxHeader;
....

im not sure why but now it works :?

any way thanks for your help! :D