Hello,
I'm very very new to Lazarus, but work with Delphi for years. I didn't know this IDE, and I'm more and more amazed day by day with the possibilities Lazarus brought to me since I've discovered I can make my code platform independent.
My first approach have happened when I came across a specific need to compile 64 bit code. Lazarus saved my life!!
Now I've a Delphi project ported to Lazarus and I'm having a problem when calling a specific function from a external C DLL.
Here is the C DLL call
typedef struct _c_typedef {
wchar_t text[128];
int number;
} c_typedef;
int c_function(c_typedef *var1, c_typedef *var2)
In my unit I use a wrapper and I call it this way:
Declaration
function c_function(var1:Pointer;var2:Pointer):integer;stdcall;external 'C.dll';
Wrapper
type c_typedef = record
text:array[0..MAX_PASSWORD] of WideChar;
number:DWORD;
end;
function StrToCtypedef(str:String):c_typedef;
var
i:integer;
begin
if (Length(str) > 126) then Str := leftstr(Str,126);
Result.number := Length(str);
StringToWideChar(str,@Result.text[0],Result.number +1 );
Result.number := Result.number * 2;
end;
function c_function_wrapper(var1:String; var2:String):integer;
var
newvar1:c_typedef;
newvar2:c_typedef;
begin
newvar1:= StrToCtypedef(var1);
newvar2:= StrToCtypedef(var2);
result :=c_function(@newvar1,@newvar2);
end;
Calling from my application
procedure TForm1.Button1Click(Sender: TObject);
var
var1, var2: string;
begin
var1 := (InputBox('Vars', 'var1', ''));
var2 := (InputBox('Vars', 'var2', ''));
c_function_wrapper(var1, var2);
end;
The problem arrives when my wrapper passes the Pointers. I'm getting an Access Violation error. In Delphi the code is exactly the same and the call works fine. In fact calling from Delphi I don't even need the wrapper.
I've the full source of everything, so if I need to change something in C to adequate to Lazarus there is no problem at all.
I've tried changing most of compiling parameters in FPC, but the error continues. Any hint about this?
Thank you guys.