Page 1 of 1

To point or not to point

Posted: Thu Nov 22, 2007 3:59 pm
by mubinaktan
I am trying to learn using dll functions. I have seen an example from the "PureBASIC Survival Guide" (http://www.xs4all.nl/~bluez/datatalk/pu ... i_and_dlls) like this:

Code: Select all

OpenLibrary(1,"MyMagical.dll")
*a_pointer = GetFunction(1,"blahblahblah")
CallFunctionFast(*a_pointer,first_variable,second_variable) 
It works. Good...
As far as I understand, first we select a function from a dll and store its memory address in a pointer (do I understand right?)

But if we use a regular variable instead of a pointer, it works again!

Code: Select all

 non_pointer_variable = GetFunction(1,"blahblahblah")
CallFunctionFast(non_pointer_variable,first_variable,second_variable) 
How is this possible? To my logic, first example should work but the second one should not work. The pointers are for storing memory addresses so don't we HAVE TO use a pointer when storing the memory address of a function?

Posted: Thu Nov 22, 2007 4:04 pm
by Trond
A pointer is just a variable that stores a memory address. But a memory address can also be stored in other variable types, as long as the type supports large enough numbers. The default type is long, which is a 4 byte integer, the same size as a pointer.

Posted: Thu Nov 22, 2007 4:04 pm
by srod
In Purebasic, a pointer is just a variable of type long which we usually use to store addresses. Until you use pointers in combination with a structure, however, Purebasic does not differentiate between a pointer and a normal variable of type long.

For example, the following is a function for adding two values and returning the result. The fact that I use pointer varables is neither here or there :

Code: Select all

Procedure.l Add(*a, *b)
  ProcedureReturn *a + *b
EndProcedure

Debug Add(10, 20)
Don't confuse PB pointers with those from, say, c, in which they are strictly typed from the outset. In PB, only when you declare a pointer to a structure does it's context become important.

In the case of a dll I would normally use a normal variable as opposed to a pointer. I'd probably name it something like 'ptrFunction' though.

I hope this helps?

**EDIT : beaten again! Too slow. :)

Ah yes... C/C++ made the confusion

Posted: Thu Nov 22, 2007 5:14 pm
by mubinaktan
In Purebasic, a pointer is just a variable of type long which we usually use to store addresses.
That makes things clearer. C/C++ made my mind confused...

My thanks go to all PB fans...