Lazarus
Home
Forum
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Using the Lazarus IDE
»
General
»
<Solved> Linux library access violation
Downloads
Daily Snapshots
FAQ
Wiki
Bugtracker
IRC channel
Developer Blog
Follow us on Twitter
Mailing List
Free pascal
Other languages
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
About donations (wiki)
Bookstore
Lazarus, the complete guide
Search
Advanced search
« previous
next »
Print
Pages: [
1
]
Author
Topic: <Solved> Linux library access violation (Read 1997 times)
ChrisX
New member
Posts: 16
<Solved> Linux library access violation
«
on:
October 29, 2006, 09:46:26 pm »
I get an "Access violation" when I call a function from a C library.
Not sure if I can explain well enough, so I will just post the code:
The C library:
#include <stdio.h>
#include <string.h>
int testreturn(const char *thename, char *rcity, char *rstate)
{
memcpy (rcity,"Boston",7);
memcpy (rstate,"Massachusetts",14);
return strlen(thename);
}
The C library is compiled and named libTester.so
The code I call to the function from is:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
Buttons, StrUtils;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
function testreturn(thename:pchar;rcity:pchar;rstate:pchar):integer; stdcall; external 'libTester';
implementation
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
rlines : array[0..1] of string;
retint : integer;
ganame : string;
begin
ganame := Trim(Edit1.Text);
rlines[0] := StringOfChar('0', 80);
rlines[1] := StringOfChar('0', 80);
retint := testreturn(pchar(ganame), pchar(rlines[0]), pchar(rlines[1]));
Edit2.Text := AnsiLeftStr(rlines[0], pos(Chr(0), rlines[0]) - 1);
Edit3.Text := AnsiLeftStr(rlines[1], pos(Chr(0), rlines[1]) - 1);
Form1.Caption := 'Name Length: ' + inttostr(retint);
end;
initialization
{$I unit1.lrs}
end.
It works well but, I get a message box that pops up that says "Access violation" press OK to continue and risk possible data corruption or Cancel to exit.
I click OK and its fine. It returns everything correctly. That message box is a pain though. I tried:
try
retint := testreturn(pchar(ganame), pchar(rlines[0]), pchar(rlines[1]));
except
//do something
end;
But the program just quits.
Any help?
Thanks
Edit:
I changed function testreturn(thename:pchar;rcity:pchar;rstate:pchar):integer; stdcall; external 'libTester';
to
function testreturn(thename:pchar;rcity:pchar;rstate:pchar):integer; cdecl; external 'libTester';
and all is good
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Using the Lazarus IDE
»
General
»
<Solved> Linux library access violation
Recent
Custom Draw Controls miss...
by
lainz
[February 10, 2012, 11:30:47 pm]
[SOLVED] Stream newb - si...
by
User137
[February 10, 2012, 11:27:01 pm]
TTogglebox color
by
Switcher
[February 10, 2012, 11:00:39 pm]
LDAP, objectsid is there ...
by
snorkel
[February 10, 2012, 09:46:59 pm]
SSH DLL (w-putty-cd) coul...
by
ludob
[February 10, 2012, 05:48:02 pm]
TCDButtom select dsAndroi...
by
wcleyton
[February 10, 2012, 05:47:36 pm]
Get list of drives
by
ludob
[February 10, 2012, 05:07:46 pm]
POCKET PC 2002 ERROR WITH...
by
delphiturk
[February 10, 2012, 03:20:20 pm]
GetCursorPos
by
User137
[February 10, 2012, 03:18:27 pm]
FPCUp FPC/Lazarus SVN ins...
by
BigChimp
[February 10, 2012, 02:48:46 pm]