Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Convert function in delphi

Posted on 2006-05-02
10
Medium Priority
?
414 Views
Last Modified: 2010-04-05
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?
0
Comment
Question by:amit_shriv
  • 4
  • 4
  • 2
10 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 16583644
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.
0
 

Author Comment

by:amit_shriv
ID: 16583783
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.
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 16583994
How should i answer that? Only you have the documentation for this specific function.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:amit_shriv
ID: 16584057
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;
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 16584146
...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.
0
 
LVL 12

Expert Comment

by:AmigoJack
ID: 16584432
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.
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 16584793
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.
0
 
LVL 12

Accepted Solution

by:
AmigoJack earned 2000 total points
ID: 16585316
robert_marquardt, not crashing does not automatically qualify the conclusion "im using it the right way". you know that.

googling it gave me the sight that this function should be meant for unix systems. as the manuals say, return -1 means an error, and errno is set to 14 you say. looking for the possible errno constants in google gave me:

#define  EFAULT   14

whereas the manual (http://perso.efrei.fr/cgi-bin/cgiwrap/parmelan/man2www?sched_getaffinity.2) say:

EFAULT
    A supplied memory address was invalid.

combining all that plus now the fact it seemed to be used the right way, id simply say you forgot to reserve memory for the mask-parameter :) so you could call it this way:

function sched_getaffinity(pid, len: dword; mask: Pdword): Integer; cdecl;

var
  p: pdword;
  i: integer;
begin
  getmem(p, 4);  // reserving 4 bytes
  i:= sched_getaffinity(0, 4, p);
  ...
  do something with     dword(p^)
  ...
  freemem(p);
end;
0
 

Author Comment

by:amit_shriv
ID: 16585360
Thanks a lot AmigoJack. It did work.
0
 

Author Comment

by:amit_shriv
ID: 16585483
It's working great AmigoJack.  The problem was getmem(p, 4);  // reserving 4 bytes. I was not using this statement.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question