Get the real value: Pointer to a Pointer

Just starting out? Need help? Post your questions and find answers here.
User avatar
Zizaco
User
User
Posts: 26
Joined: Tue May 16, 2006 8:20 pm
Contact:

Get the real value: Pointer to a Pointer

Post by Zizaco »

I have to use a library that have a callback to a function sending as one of the parameters a IStream pointer to a pointer (in lib documentation: IStream **VarName).

How can i get the real value of it? (The "final pointer chain" value)

I've tried this:

Code: Select all

Procedure.l GlobalOnLoadExternalResourceHandler(lpszURL.l, *pStream.l, hFPC.l, lParam.l)
	Debug *pStream ;This Debug prints "1242692"
	@*pStream = *pStream
	@*pStream = *pStream

	;[...] - Gen the lpData, dwSize and the Empty nWrittenBytes, It's all ok here.

	CallFunction(0,"FPC_IStream_Write",pStream, lpData, dwSize, @nWrittenBytes)	;<--- This is a generic function to write in the IStream by receiving it's address. It didn't work. The nWrittenBytes still 0 after it.
I figured out the problem while converting the code from VB6. See that the pStream paramter is "ByRef"
In the original version the code is this and everything works:

Code: Select all

Public Function GlobalOnLoadExternalResourceHandler(ByVal lpszURL As Long, ByRef pStream As Long, ByVal hFPC As Long, ByVal lParam As Long) As Long
	MsgBox(Str(pStream)) 'This MsgBox prints "21222640" (the real "final" value)
	
	;[...] - Gen the lpData, dwSize and the Empty nWrittenBytes, It's all ok here.
	
	FPC_IStream_Write pStream, ByVal lpData, dwSize, nWrittenBytes ;<--- Here it works. I got 139912 in the nWrittenBytes. And the rest of the program works.
Then i changed the "ByRef" in VB6 code to ByVal (This way the VB6 considers it value, not the final pointed value)

Code: Select all

Public Function GlobalOnLoadExternalResourceHandler(ByVal lpszURL As Long,BYVAL pStream As Long, ByVal hFPC As Long, ByVal lParam As Long) As Long
	MsgBox(Str(pStream)) 'This MsgBox prints "1242692" the same value I got in PB.
	
	;[...] - Gen the lpData, dwSize and the Empty nWrittenBytes, It's all ok here.
	
	FPC_IStream_Write pStream, ByVal lpData, dwSize, nWrittenBytes 	;<--- I got the same problem that i got in PB. Zero in nWrittenBytes and the same problems in the rest of the program.
While searching for a solution (the way to get the value of a pointer to a pointer) i came across this topic: viewtopic.php?f=3&t=43060 but there is none conclusion on it.

Please can someone help me? Is possible to get the real value trought In-Line Assembly? How?

PS: Sorry for my bad english
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Get the real value: Pointer to a Pointer

Post by luis »

If *pStream is a pointer to a pointer then maybe:

Code: Select all

*val = PeekL(*pStream)
and then *val should point to the actual data

So if the actual data is a long you can fetch it with PeekL(*val) or if the data is a structured type just declare *val as a var of that type
define *val.mydata
and then use *val\fieldname1, *val\fieldnameN to access it

All of the above if I understood correctly what you said, it's 4 AM here in Italy and I had a long night :mrgreen:


BTW: for portability better to use I instead of L (for data types, PeekX functions, etc.) unless you specifically want L (4 bytes) data/pointers.
"Have you tried turning it off and on again ?"
A little PureBasic review
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Get the real value: Pointer to a Pointer

Post by Mistrel »

Egads, luis!

Code: Select all

*val = PeekI(*pStream)
Integer for pointers, not long! :)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Get the real value: Pointer to a Pointer

Post by Trond »

Code: Select all

Procedure.l GlobalOnLoadExternalResourceHandler(lpszURL.l, *pStream.l, hFPC.l, lParam.l)
   Debug *pStream ;This Debug prints "1242692"
   @*pStream = *pStream
;    @*pStream = *pStream ; remove this

   ;[...] - Gen the lpData, dwSize and the Empty nWrittenBytes, It's all ok here.

   CallFunction(0,"FPC_IStream_Write", *pStream, lpData, dwSize, @nWrittenBytes)   ;<--- This is a generic function to write in the IStream by receiving it's address. It didn't work. The nWrittenBytes still 0 after it.
Your pointer variable is *pStream, but you pass the parameter pStream, which is a different variable, initialized to 0.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: Get the real value: Pointer to a Pointer

Post by PMV »

*pStream is the pointer of the pointer, and you want to get the pointer in it?
like Mistrel wrote:

Code: Select all

Procedure.l GlobalOnLoadExternalResourceHandler(lpszURL.l, *pStream, hFPC.l, lParam.l)
	Debug *pStream
	Debug PeekI(*pStream)  ; pointer in it

	CallFunction(0,"FPC_IStream_Write", PeekI(*pStream), lpData, dwSize, @nWrittenBytes)
The @ is in PureBasic an operator and it is for the revers operation.

Code: Select all

Debug *pStream ; normal pointer
*pStream = @*pStream ; pointer of pointer
Debug *pStream
Debug PeekI(*pStream) ; pointer in it (normal pointer)
MFG PMV
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Get the real value: Pointer to a Pointer

Post by luis »

egads ? Never heard that word before.

http://www.urbandictionary.com/define.php?term=egads

Ahhh, I see

Mistrel wrote:
Integer for pointers, not long! :)
Yes, and integer is better in general too as I wrote
BTW: for portability better to use I instead of L (for data types, PeekX functions, etc.) unless you specifically want L (4 bytes) data/pointers.
but I was trying to uniform my example to his "L" usage, supposing he's writing for 32 bits since he used .l for all the numerical params instead of .i

Anyway better get used to use .i and peeki() UNLESS you need to access something that is always 4 bytes long.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Zizaco
User
User
Posts: 26
Joined: Tue May 16, 2006 8:20 pm
Contact:

Re: Get the real value: Pointer to a Pointer

Post by Zizaco »

Thanks everybody!
Post Reply