Hi, I have to write an application that can use plugins. What's more, plugins should may use some functions from my application.
I have read some articles about writing applications with plugins(but, there isn't many), and got to know that I have to use interfaces.
But something's wrong with my code(although I think it should work), because I get an AccessViolation.
My interface looks like that:
[tt][blue]
type
IAppInterface = interface(IInterface) ['{CCC6FDCA-5F81-4C99-B0F2-BD4255564439}']
procedure EmployeesChanged;
procedure EventsChanged;
procedure ErrorOccured(Error: PChar; PluginName: PChar);
end;
[/blue][/tt]
GUID is created by SHIFT+CTRL+G of course
I implement this interface in class TPlugin:
[tt][blue]
type
TPlugin = class(TInterfacedObject, IAppInterface)
public
procedure EmployeesChanged;
procedure EventsChanged;
procedure ErrorOccured(Error: PChar; PluginName: PChar);
end;
[/blue][/tt]
Then I have a class called TPluginManager that manages my plugins
I introduce there a variable:
[tt][blue]
FAppInterface: IAppInterface;
[/blue][/tt]
And create it:
[tt][blue]
FAppInterface:=TPlugin.Create;
[/blue][/tt]
Next I load library and look for function address:
[tt][blue]
procedure TPluginManager.ConnectToPlugin(PluginFileName: string);
var
ConnectProc: procedure(const AppIFace: IAppInterface; AppConnectionString: PChar);
invalidPlugin: boolean;
lHandle: THandle;
begin
invalidPlugin:=false;
lHandle:=LoadLibrary(PAnsiChar(PluginFileName));
@ConnectProc:=GetProcAddress(lHandle, 'Connect');
if @ConnectProc = nil then
begin
InvalidPlugin:=true;
end else
begin
ConnectProc(FAppInterface, PChar(AppDBConnectionString));
end;
@ConnectProc:=nil;
end;
[/blue][/tt]
When I call ConnectProc I get an AccessViolation error.
The error occurs in procedure in my dll file. This procedure looks like that:
[tt][blue]
var
AppInterface: IAppInterface;
procedure Connect(const AppIFace: IAppInterface; AppConnectionString: PChar); stdcall;
begin
AppInterface:=AppIFace; //here is the error
//other code
end;
[/blue][/tt]
Why does this error occur? What am I doing wrong?
In my application I tried also to create FAppInterface just before calling ConnectProc, but it didn't change anything.
Please, help me, because I am run out of ideas and time.
I have read some articles about writing applications with plugins(but, there isn't many), and got to know that I have to use interfaces.
But something's wrong with my code(although I think it should work), because I get an AccessViolation.
My interface looks like that:
[tt][blue]
type
IAppInterface = interface(IInterface) ['{CCC6FDCA-5F81-4C99-B0F2-BD4255564439}']
procedure EmployeesChanged;
procedure EventsChanged;
procedure ErrorOccured(Error: PChar; PluginName: PChar);
end;
[/blue][/tt]
GUID is created by SHIFT+CTRL+G of course
I implement this interface in class TPlugin:
[tt][blue]
type
TPlugin = class(TInterfacedObject, IAppInterface)
public
procedure EmployeesChanged;
procedure EventsChanged;
procedure ErrorOccured(Error: PChar; PluginName: PChar);
end;
[/blue][/tt]
Then I have a class called TPluginManager that manages my plugins
I introduce there a variable:
[tt][blue]
FAppInterface: IAppInterface;
[/blue][/tt]
And create it:
[tt][blue]
FAppInterface:=TPlugin.Create;
[/blue][/tt]
Next I load library and look for function address:
[tt][blue]
procedure TPluginManager.ConnectToPlugin(PluginFileName: string);
var
ConnectProc: procedure(const AppIFace: IAppInterface; AppConnectionString: PChar);
invalidPlugin: boolean;
lHandle: THandle;
begin
invalidPlugin:=false;
lHandle:=LoadLibrary(PAnsiChar(PluginFileName));
@ConnectProc:=GetProcAddress(lHandle, 'Connect');
if @ConnectProc = nil then
begin
InvalidPlugin:=true;
end else
begin
ConnectProc(FAppInterface, PChar(AppDBConnectionString));
end;
@ConnectProc:=nil;
end;
[/blue][/tt]
When I call ConnectProc I get an AccessViolation error.
The error occurs in procedure in my dll file. This procedure looks like that:
[tt][blue]
var
AppInterface: IAppInterface;
procedure Connect(const AppIFace: IAppInterface; AppConnectionString: PChar); stdcall;
begin
AppInterface:=AppIFace; //here is the error
//other code
end;
[/blue][/tt]
Why does this error occur? What am I doing wrong?
In my application I tried also to create FAppInterface just before calling ConnectProc, but it didn't change anything.
Please, help me, because I am run out of ideas and time.