Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Function call.

Posted on 1999-07-30
11
Medium Priority
?
117 Views
Last Modified: 2010-04-04
Hi,
How to put the address of a variable,say "temp" into a function.
ie. During the execution of the function, the variable, temp, can be changed by the function.
Something like call by reference.
Who can help?
0
Comment
Question by:yingkit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 7

Accepted Solution

by:
RBertora earned 20 total points
ID: 1391970
procedure (var temp);

0
 
LVL 7

Expert Comment

by:RBertora
ID: 1391971
full answer:

declaration:
============
procedure MyProcedure ( Var Temp : String);
begin
  Temp := Temp + 'you';
end;


call:
=====
var
 Temp : String;
begin
  Temp := 'hello';

showmessage(MyProcedure(Temp));

this will produce message 'Hello you'
Rob.



0
 

Author Comment

by:yingkit
ID: 1391972
I need to clarify my question.
The function being called is not coded by me, it's in a DLL, so actually, the function being called is an API.  Here is the prototype (declaration) of the API:
BOOL WINAPI RegAPICall_GetWidth(int &iWidth);
But I cannot call the API by:
var
   width:integer;
begin
     width:=0;
     RegAPICall_GetWidth(width);
........
end.

After execution, the width will be unchanged.  What I want to know is how to call the API which the API can directly modify the variable input, ie. width.
Thanks
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Expert Comment

by:Lischke
ID: 1391973
You need to pass the address of the variable by calling:

  RegAPICall_GetWidth(@Width);

Ciao, Mike
0
 

Author Comment

by:yingkit
ID: 1391974
Hi Mike,
When compiling, the compiler said that imcompatible types: 'integer' and 'pointer', is there any ways to convert an integer to a pointer?
Thanks
0
 
LVL 7

Expert Comment

by:RBertora
ID: 1391975
After clarification of question...

reject my original answer please.. I think mike has answered your question.

Rob
0
 
LVL 7

Expert Comment

by:RBertora
ID: 1391976
Type


Perhaps this little sample will help you

Modify is your dll function equivalent.
variable b is equivalent to your width.

Type :
  IntPointer = ^integer;

procedure modify (A : IntPointer);
begin
 A^ := 3;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 l : Integer; b: intpointer;
begin
 l := 17;
 b := AllocMem(sizeof(IntPointer));
 b^:= l;
 showmessage(inttostr(b^));
 modify(b);
 showmessage(inttostr(b^));
end;

Rob.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 1391977
yingkit,

it seems the Delphi translation of the function does not directly correspond to the original version. I assume it looks like:

  function RegAPICall_GetWidth(var Width: Integer): BOOL; stdcall;

while my previous comment implicitely supposed that it would look so:

  function RegAPICall_GetWidth(Width: PInteger): BOOL; stdcall;

Anyway, this isn't the problem at all. You wrote Width isn't change after the call so it seems to me your Delphi translation is missing the var keyword.

If all this doesn't fit then please post the Delphi declaration of the function so we can have a look...

Ciao, Mike
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1391978
hi yingkit

try

RegAPICall_GetWidth(Integer(@Width));

meikl
0
 
LVL 7

Expert Comment

by:RBertora
ID: 1391979
I think all you have to do is:

var
   pwidth : ^integer;
   width:integer;
begin
     Pwidth := AllocMem(sizeof(Integer));
     width:=0;
     RegAPICall_GetWidth(Pwidth);
     width := Pwidth^;
Rob.


0
 

Author Comment

by:yingkit
ID: 1391980
Hee...thank you all for your helps, I've solved the problem.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

661 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