Page 1 of 1

Windows API and Passing Values

Posted: Sun Jan 01, 2012 7:32 am
by silvercover
I know that we can pass parameters to API functions in three types of Passing by Value, Passing Indirectly and Passing by Reference.

My question is about Indirect mode; can we change address of allocated memory space on demand or that's done by windows in some restricted area which is owned by windows core?

In other words can we tell windows upon structure creation time to make and store needed structure in a memory area that we specified?

How about passing by Reference? if we call the API function which is accept parameters by reference, does windows places and keeps structures in the same memory area on each call or not?

Or if I'm wrong, please correct me.
Thanks.

Re: Windows API and Passing Values

Posted: Sun Jan 01, 2012 7:42 am
by Shield
I only know about passing by value (i.e. pushing the value of a parameter onto the stack) and passing by reference (pushing only the address of the
target data onto the stack). I'm not sure I ever heard of "passing indirectly", you need to explain that to me.
silvercover wrote:can we change address of allocated memory space on demand
You can't and it wouldn't even make sense. You get an address of memory allocated to suit your needs,
it makes no sense to chose a fix address.
silvercover wrote:In other words can we tell windows upon structure creation time to make and store needed structure in a memory area that we specified?
Now that's something entirely different. ;)
You probably mean this:

Code: Select all

EnableExplicit


Structure YourStruct
	value.i
	number.i
EndStructure


Define *yourBuffer.YourStruct

*yourBuffer = AllocateMemory(SizeOf(YourStruct))
*yourBuffer\value = 20
*yourBuffer\number = 30

Debug *yourBuffer\value
Debug *yourBuffer\number
Also don't forget about InitializeStructure().
silvercover wrote:How about passing by Reference? if we call the API function which is accept parameters by reference, does windows places and keeps structures in the same memory area on each call or not?
If you allocated memory space once it is yours until you free the buffer again. That address is never going to change
and if you forgot about it, you got yourself a memory leak. ;)

(Just so I'm not leaving details out...this is the case in PureBasic and many other languages. However, in certain languages or environments, such as .NET,
pointer addresses can change during runtime. However this does not affect your application unless you're dealing with native API calls).


If an API function requires data by reference, you just pass the reference and you'll be fine.
That data is either read / modified directly or copied internally, but that depends on the function you're calling.


Hope that helps...if not, ask again. :)

Re: Windows API and Passing Values

Posted: Sun Jan 01, 2012 11:47 am
by Trond
In some programming languages (VB, Pascal) there is a distinction between "by value" and "by reference". There are special keywords to pass a parameter by reference.

In PureBasic, the rules are different:
- Complex objects (arrays, lists, maps) are always passed by reference. These are native to PB and can't be used with API functions, so they are irrelevant here.
- Everything else is passed by value only.

To simulate a "pass-by-reference" in PB, we simply pass a pointer by value. To simulate passing a reference by reference, we pass, by value, a pointer to a pointer.

It's so happens that this is exactly how API functions work, too. :) There is no "by reference" in Windows API. There is only pointer to structure/variable, and that pointer is passed by value.

API functions don't manage your memory. You need to allocate and free all your structures manually. This is a design decision in the Windows API.

On Linux it's different. Some much-used libraries (those who use the GObject type system) have some functions that allocates memory for you. You must free it manually when done. Here's a made-up example

Code: Select all

*mygobject = g_list_new_()
; and when done:
g_list_free_(*mygobject)