structures and procedures

Just starting out? Need help? Post your questions and find answers here.
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

structures and procedures

Post by doodlemunch »

hello i got a structure with some data in. and. i need to pass this structure through a procedure

like..

;my structure

poo.BLABLA
poo\size = sizeof(BLABLA)
poo\blaa = 356
poo\hooo = 495

;passing the structure to the procedure

myprocedure(poo)

is this correct? because im doing so and i get undesired effects
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

re

Post by srod »

Hi,

you need to use a 'pointer' to the structure and pass the address of the structure through the pointer to the procedure.

Code: Select all

;my Structure 
Structure BLABLA
  size.l
  blaa.l
  hooo.l
EndStructure

Procedure myprocedure(*arg.BLABLA); *arg sets up a pointer to a variable of type BLABLA
  *arg\blaa = 10
;etc.
EndProcedure


poo.BLABLA; Set up a variable of type BLABLA.
poo\size = SizeOf(BLABLA) 
poo\blaa = 356 
poo\hooo = 495 
Debug "poo\blaa = " + Str(poo\blaa); Displays the value of 356.
myprocedure(@poo); Calls the procedure and passes the ADDRESS OF the variable poo.
Debug "poo\blaa now = " + Str(poo\blaa); Now displays the new value of 10.
I may look like a mule, but I'm not a complete ass.
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

hi thanks

what i need to do is pass poo to the procedure and from there to a dll function. i believe im doing it wrong because even tho the function returns 1 the result is not the expected.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Can you post some code?
I may look like a mule, but I'm not a complete ass.
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

well theres no need really

I got the structure and some stuff in it.
now, i want to pass the structure via my procedure, and from there, to a value in a function im calling from a dll

even if i gave you the code, i cant give you the dll.
so i thought its pretty much logical as for needing to paste source here. sorry if i dont make sense i thought i was.
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

Here:
made an example of my problem

Code: Select all


;HOW can I pass the structure to myprocedure correctly?!?!

#meh = 232

Procedure MyProcedure(val1,val2,theval)
 ; of course we wont call this because it will crash
 ;bla = CallFunction(1,"blabla",val1,val2,theval)
  Debug val1
  Debug val2
  Debug theval ; the important one

EndProcedure

Structure HELLO
hmmm.l
blabla.l
num.l
info.l
EndStructure


hi.HELLO   ;struct
hi\hmmm        = 1
hi\blabla      = #meh
hi\num         = 4234
hi\info        = 34234234

MyProcedure(324,22,hi)


doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

as you can see the dll function needs the correct structure data .. if not it wont work as expected. and thats my problem i dont know how to send it the correct data.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

First thing is that by attempting to debug the value of a structure, you'll only ever access the memory address of the structure.
E.g. if the variable 'hi' refers to a structure variable, then the command

Code: Select all

debug hi
will simply display the address of the structure in memory and not any of it's data fields.

To display the contents of the structure you need to display each field in turn; e.g.

Code: Select all

debug hi\num
The second thing is that in your procedure definition

Code: Select all

Procedure MyProcedure(val1,val2,theval) 
the variable 'theval' by default is a type 'long' variable; NOT a structure.
You need to declare this, not as a structure, but as a pointer to a structure as follows:

Code: Select all

Procedure MyProcedure(val1,val2,*theval.HELLO) 
The third problem is that you now need to put the structure declaration before the procedure declaration etc.

Code: Select all

;HOW can I pass the structure to myprocedure correctly?!?! 

#meh = 232 

Structure HELLO 
hmmm.l 
blabla.l 
num.l 
info.l 
EndStructure 


Procedure MyProcedure(val1,val2,*theval.HELLO) 
 ; of course we wont call this because it will crash 
 ;bla = CallFunction(1,"blabla",val1,val2,theval) 
  Debug val1 
  Debug val2 
  Debug *theval\hmmm ; the important one 

EndProcedure 


hi.HELLO   ;struct 
hi\hmmm        = 1 
hi\blabla      = #meh 
um         = 4234 
hi\info        = 34234234 

MyProcedure(324,22,@hi) 
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

re

Post by srod »

Ah, I see, think I'm being thicker than a walrus sandwich today!

If all you need do is pass the address of the structure to the dll then your code looks okay. I guess the focus then should be upon the dll itself.

Without knowing more about the library it's difficult to advise.
I may look like a mule, but I'm not a complete ass.
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

lib ? its opengl32 !
by the way I believe I need to send all the data in the structure.. but how would I. I thought about passing the values inside the procedure with the pointer trick and then making a value with all the values inside ?

but i dont know how opengl32 manages this

its for the function 1590848448 (wglSetPixelFormat)
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

for example on ChoosePixelFormat i pass the address of the same structure and works nice so far

but with this other function SetPixelFormat doesnt seem to work the same way
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

doodlemunch wrote:its for the function 1590848448 (wglSetPixelFormat)
1590848448???? ;)

viewtopic.php?p=86116#86116
Good programmers don't comment their code. It was hard to write, should be hard to read.
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

thx but no it does not help
i dont want to use the function like that i want to call it directly from the opengl dll thats the joy

and so i dont know how to pass the structure i gave the address but it doesnt work- even that the function returns 1 it does not work correctly
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

SetPixelFormat is a gdi-function, not an opengl function.
Did I miss something?

However, calling the function directly from the dll is the same.

Code: Select all

gdi32.l = OpenLibrary(#PB_Any, "gdi32.dll")
opengl32.l = OpenLibrary(#PB_Any, "opengl32.dll")

*SetPixelFormat     = IsFunction(gdi32, "SetPixelFormat")
*ChoosePixelFormat  = IsFunction(gdi32, "ChoosePixelFormat")
*wglMakeCurrent     = IsFunction(opengl32, "wglMakeCurrent")
*wglCreateContext   = IsFunction(opengl32, "wglCreateContext")


pfd.PIXELFORMATDESCRIPTOR
ZeroMemory_(@pfd, SizeOf(PIXELFORMATDESCRIPTOR))

pfd\nSize = SizeOf(PIXELFORMATDESCRIPTOR)
pfd\nVersion = 1
pfd\dwFlags = #PFD_DRAW_TO_WINDOW | #PFD_SUPPORT_OPENGL | #PFD_DOUBLEBUFFER
pfd\iPixelType = #PFD_TYPE_RGBA
pfd\cColorBits = 32
pfd\cDepthBits = 16
pfd\iLayerType = #PFD_MAIN_PLANE


hdc.l = GetDC_(OpenWindow(0,0,0,640,480,#PB_Window_SystemMenu,"OpenGL"))
pixelFormat.l = CallFunctionFast(*ChoosePixelFormat, hdc, @pfd)
CallFunctionFast(*SetPixelFormat, hdc, pixelFormat, @pfd)
CallFunctionFast(*wglMakeCurrent, hdc, CallFunctionFast(*wglCreateContext, hdc))

While EventID<>#PB_Event_CloseWindow
  EventID=WaitWindowEvent()
  
  Delay(10)
Wend 

CloseLibrary(opengl32)
CloseLibrary(gdi32)

End
Good programmers don't comment their code. It was hard to write, should be hard to read.
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

i know i got a dillema because i saw the same function on both opengl32 and gdi32 look

from opengl wglSetPixelFormat 1590848448
from gdi GdiSetPixelFormat 2012498864

of course im in the need of the WGL one because im working with opengl and as far as i know thats the correct one? or i missed something again? :roll:
Post Reply