hi,
I am having problem in assigning a value to pchar type variable. I have a base class, which is being inherited by child class. The variable is defined in base class in "protected" identifier. child class is assigning the value in its constructor.
Base Class
DELPHI Code:
[url=#top]
[/url]Original
- DELPHI Code
unit DllBaseClass;
interface
uses
Windows;
type
TDllBaseClass = class
protected
DllHandle : THandle;
initCalled : boolean;
dllName : PChar;
public
procedure init (); virtual; abstract;
end;
implementation
end.- unit
- DllBaseClass;
- interface
- uses
- Windows;
- type
- TDllBaseClass =
- class
- protected
- DllHandle : THandle;
- initCalled :
- boolean
- ;
- dllName :
- PChar
- ;
- public
- procedure
- init
- (
- )
- ; virtual; abstract;
- end
- ;
- implementation
- end
- .
Copy Code and the Child Class
DELPHI Code:
[url=#top]
[/url]Original
- DELPHI Code
unit CodeBase;
interface
uses
Windows, DllBaseClass, OtengoGlobal;
type
TCodeBase = class (TDllBaseClass)
private
DllSetDirectoryPath: procedure(DirectoryPath: PChar); stdcall;
public
procedure init (); override;
Constructor Create(); overload;
procedure SetDirectoryPath (DirectoryPath : PChar);
end;
implementation
{ TCodeBase }
constructor TCodeBase.Create;
begin
inherited;
dllName := 'OCB.dll'; {Error location}
init();
end;
procedure TCodeBase.init;
begin
DllHandle := LoadLibrary(dllName);
initCalled := true;
end;
procedure TCodeBase.SetDirectoryPath(DirectoryPath: PChar);
begin
if initCalled = false then
begin
init();
end;
if @DllSetDirectoryPath = nil then
begin
@DllSetDirectoryPath := GetProcAddress(DllHandle, _SetDirectoryPath);
end;
DllSetDirectoryPath(DirectoryPath);
end;
end.Its giving some access violation errors ... .
Error is on line # 25 in child class.
BUT, IMO, it is safer to store it as a string and do the conversion at the last minute.
Code:
protected
dllName : String;
Code:
DllHandle := LoadLibrary(pChar(dllName)); |