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?
Delphi

Avatar of undefined
Last Comment
amit_shriv
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
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.
Avatar of amit_shriv
amit_shriv

ASKER

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.
Avatar of AmigoJack
AmigoJack

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of amit_shriv
amit_shriv

ASKER

Thanks a lot AmigoJack. It did work.
Avatar of amit_shriv
amit_shriv

ASKER

It's working great AmigoJack.  The problem was getmem(p, 4);  // reserving 4 bytes. I was not using this statement.
Delphi
Delphi

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Development™. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.

60K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo