Link to home
Start Free TrialLog in
Avatar of amit_shriv
amit_shriv

asked on

Convert function in delphi

I need to use C/C++ function int sched_getaffinity(pid_t pid, unsigned int len, unsigned long *mask); What will be the compatible function in Delphi.
I Converted above function as;
type
  PCardinal = ^cardinal;

function sched_getaffinity(pid: cardinal; len: cardinal; mask: PCardinal): Integer; cdecl;

is this correct?
Avatar of robert_marquardt
robert_marquardt

The function should work.

Better:

type
  PCardinal;
  pid_t = Cardinal;

function sched_getaffinity(pid: pid_t; len: Cardinal; var mask: Cardinal): Integer; cdecl;
function sched_getaffinity(pid: pid_t; len: Cardinal; mask: PCardinal): Integer; cdecl;

It looks very much as if the affinity mask is placed in mask by the function so var is the better conversion.
If len indicates that maks points to an array of mask values then PCardinal is the better conversion.
Use the pid_t type to make the function more readable.
cdecl is more probable than stdcall here, but stdcall is not impossible.
Avatar of amit_shriv

ASKER

Ok now I ma using function sched_getaffinity(pid: pid_t; len: Cardinal; var mask: Cardinal): Integer; cdecl;
but I am getting return value -1 and errorno 14.
How should i answer that? Only you have the documentation for this specific function.
It's not my function it C function in Sched.c libarary
function sched_getaffinity(pid: pid_t; len: Cardinal; var mask: Cardinal): Integer; cdecl;
...and what is sched.c? You are talking about a 3rd party file and i have no clue what it really does.
You obviously get at least an error. I would guess that you do not hand in a valid pid.
sched_getaffinity(pid_t pid, unsigned int len, unsigned long *mask);

please also look up how pid_t is defined. you try it with a cardinal, but are you sure its an "unsigned long"? also guys, please dont mix up int and long since i think int is 16bit while long is 32bit. so the best translation would read as the following

uses
  windows;

type
  pid_t= { yeah what? } DWord;

function sched_getaffinity(pid: pid_t; len: Word; mask: PDWord): Integer; STDCALL;

the STDCALL is a shot in the blue, as i experienced that calling convention the most times. you might also try CDECL here.
Please enter the 21st century. int = 16 bit is long over. We are talking Win32 after all.
The absence of any qualifiers in the C declaration is more a hint towards cdecl than stcall.

The conversion is obviously already correct. He gets error values, but no crashes.
It now depends on the documentation of the API. Without it it is impossible to understand how the function works.
I would assume that you first call another function which provides a valid pid.
ASKER CERTIFIED SOLUTION
Avatar of AmigoJack
AmigoJack

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot AmigoJack. It did work.
It's working great AmigoJack.  The problem was getmem(p, 4);  // reserving 4 bytes. I was not using this statement.