# Dereferencing pointers in Win32 API

**URL:** <https://rubytalk.org/t/dereferencing-pointers-in-win32-api/9634>\
**Category:** ruby-talk\
**Created:** [10 January 2004 17:31 UTC](https://rubytalk.org/t/dereferencing-pointers-in-win32-api/9634 "2004-01-10T17:31:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shashank\_Date2](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Shashank\_Date2](https://rubytalk.org/u/Shashank_Date2)\
**Post date:** [10 January 2004 17:31 UTC](https://rubytalk.org/t/dereferencing-pointers-in-win32-api/9634/1 "2004-01-10T17:31:41Z")

</div>

Ruby/Win Gurus,

I haven’t figured out how to dereference pointers passed in as arguments  
to Win32 API. For example, in the following call (in C) to NetStatisticsGet  
API (which may be broken on NT, but that is not the point here).

LPBYTE lpBuffer;  
int ret;  
ret = NetStatisticsGet (  
NULL,  
(char \*)is\_workstation ? L"LanmanWorkstation" : L"LanmanServer"),  
0,  
0,  
&lpBuffer);

The last parameter lpBuffer is a pointer to a pointer.  
The API call internally allocates memory and sets the value of the pointer  
to the  
variable whose pointer you have passed in. It is your responsibility to free  
the  
memory using NetApiBufferFree function.

When I call this in Ruby, I do the following:

> **···**
>
> #----------------
> 
> netStatisticsGet = Win32API.new(‘netapi32’,‘NetStatisticsGet’,‘PPNNP’,‘I’)
> 
> lpBuffer = ’ ’ \* 4 # area to store the pointer value
> 
> # assume that the variable ‘service’ contains “LanmanWorkstation” in Unicode
> 
> ret = netStatisticsGet.call(0,service,0,0,lpBuffer)
> 
> #-----------------
> 
> This call succeeds (because I get ret == 0) which means lpBuffer now points  
> to the  
> buffer in memory. My questions is how do I de-reference this variable in  
> Ruby?
> 
> TIA,  
> – shanko

---

<div class="post-metadata">

**Author:** ![jeepcreep](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jeepcreep](https://rubytalk.org/u/jeepcreep)\
**Post date:** [10 January 2004 17:44 UTC](https://rubytalk.org/t/dereferencing-pointers-in-win32-api/9634/2 "2004-01-10T17:44:48Z")

</div>

Please unsubscribe

Shashank Date [sdate@everestkc.net](mailto:sdate@everestkc.net) wrote:Ruby/Win Gurus,

I haven’t figured out how to dereference pointers passed in as arguments  
to Win32 API. For example, in the following call (in C) to NetStatisticsGet  
API (which may be broken on NT, but that is not the point here).

LPBYTE lpBuffer;  
int ret;  
ret = NetStatisticsGet (  
NULL,  
(char \*)is\_workstation ? L"LanmanWorkstation" : L"LanmanServer"),  
0,  
0,  
&lpBuffer);

The last parameter lpBuffer is a pointer to a pointer.  
The API call internally allocates memory and sets the value of the pointer  
to the  
variable whose pointer you have passed in. It is your responsibility to free  
the  
memory using NetApiBufferFree function.

When I call this in Ruby, I do the following:

> **···**
>
> #----------------
> 
> netStatisticsGet = Win32API.new(‘netapi32’,‘NetStatisticsGet’,‘PPNNP’,‘I’)
> 
> lpBuffer = ’ ’ \* 4 # area to store the pointer value
> 
> # assume that the variable ‘service’ contains “LanmanWorkstation” in Unicode
> 
> ret = netStatisticsGet.call(0,service,0,0,lpBuffer)
> 
> #-----------------
> 
> This call succeeds (because I get ret == 0) which means lpBuffer now points  
> to the  
> buffer in memory. My questions is how do I de-reference this variable in  
> Ruby?
> 
> TIA,  
> – shanko

---

<div class="post-metadata">

**Author:** ![Dennis\_Ranke1](https://avatars.discourse-cdn.com/v4/letter/d/dec6dc/32.png) [@Dennis\_Ranke1](https://rubytalk.org/u/Dennis_Ranke1)\
**Post date:** [10 January 2004 18:36 UTC](https://rubytalk.org/t/dereferencing-pointers-in-win32-api/9634/3 "2004-01-10T18:36:40Z")

</div>

If I’m not completely wrong, you should be able to dereference it using  
lpBuffer.unpack(‘P’) where is the size of the buffer in  
bytes. Just remember that unpack returns an array, the first element of  
which will be the dereferenced buffer as a string.

> **···**
>
> On Sat, 10 Jan 2004 11:26:59 -0600, Shashank Date [sdate@everestkc.net](mailto:sdate@everestkc.net) wrote:
> 
> > When I call this in Ruby, I do the following:  
> > #----------------
> > 
> > netStatisticsGet =  
> > Win32API.new(‘netapi32’,‘NetStatisticsGet’,‘PPNNP’,‘I’)
> > 
> > lpBuffer = ’ ’ \* 4 # area to store the pointer value
> > 
> > # assume that the variable ‘service’ contains “LanmanWorkstation” in
> > 
> > Unicode  
> > ret = netStatisticsGet.call(0,service,0,0,lpBuffer)
> > 
> > #-----------------
> > 
> > This call succeeds (because I get ret == 0) which means lpBuffer now  
> > points  
> > to the  
> > buffer in memory. My questions is how do I de-reference this variable in  
> > Ruby?
> 
> –  
> Dennis Ranke

---

<div class="post-metadata">

**Author:** ![Shashank\_Date2](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Shashank\_Date2](https://rubytalk.org/u/Shashank_Date2)\
**Post date:** [10 January 2004 20:46 UTC](https://rubytalk.org/t/dereferencing-pointers-in-win32-api/9634/4 "2004-01-10T20:46:40Z")

</div>

Thanks for looking.

“Dennis Ranke” [exoticorn@epost.de](mailto:exoticorn@epost.de) wrote in message

> If I’m not completely wrong, you should be able to dereference it using  
> lpBuffer.unpack(‘P’) where is the size of the buffer in  
> bytes. Just remember that unpack returns an array, the first element of  
> which will be the dereferenced buffer as a string.

I tried that and i get:

C:/atest/tst\_netapi32.rb:28:in `unpack’: no associated pointer  
(ArgumentError)  
from C:/atest/tst\_netapi32.rb:28

Here is the program, and I am on Win XP (Home)  
ruby 1.8.0 (2003-08-04) [i386-mswin32]:

> **···**
>
> #---------------------------------------------------------------------------  
> require “Win32API”
> 
> SERVICE\_WORKSTATION = [  
> 76,0,97,0,110,0,109,0,97,0,110,0,87,0,111,0,114,0,  
> 107,0,115,0,116,0,97,0,116,0,105,0,111,0,110,0,0,0]
> 
> SERVICE\_WORKSTATION.each{|e| print “%c” % e if e \> 0}  
> puts
> 
> service = SERVICE\_WORKSTATION.pack(“U\*”) # Unicode
> 
> NetStatisticsGet = Win32API.new(‘netapi32’,‘NetStatisticsGet’,‘PPNNP’,‘I’)
> 
> bufptr = " " \* 4  
> ret = NetStatisticsGet.call(0,service,0,0,bufptr)
> 
> if 0 != ret  
> puts “Failed:#{ret}”  
> exit  
> end
> 
> # Here’s where I’m confused…
> 
> #packstring = “I” \* 13  
> #packstring += “L” \* 27  
> #struct = bufptr.unpack(packstring)
> 
> STAT\_WORKSTATION\_SIZE = (13_8) + (28_4)  
> p STAT\_WORKSTATION\_SIZE
> 
> struct = bufptr.unpack(“P#{STAT\_WORKSTATION\_SIZE}”)  
> p struct
> 
> **END**

---

<div class="post-metadata">

**Author:** ![Park\_Heesob3](https://avatars.discourse-cdn.com/v4/letter/p/c89c15/32.png) [@Park\_Heesob3](https://rubytalk.org/u/Park_Heesob3)\
**Post date:** [10 January 2004 23:37 UTC](https://rubytalk.org/t/dereferencing-pointers-in-win32-api/9634/5 "2004-01-10T23:37:11Z")

</div>

Hi,

> Thanks for looking.
> 
> “Dennis Ranke” [exoticorn@epost.de](mailto:exoticorn@epost.de) wrote in message
> 
> > If I’m not completely wrong, you should be able to dereference it using  
> > lpBuffer.unpack(‘P’) where is the size of the buffer in  
> > bytes. Just remember that unpack returns an array, the first element of  
> > which will be the dereferenced buffer as a string.
> 
> I tried that and i get:
> 
> C:/atest/tst\_netapi32.rb:28:in `unpack’: no associated pointer  
> (ArgumentError)  
> from C:/atest/tst\_netapi32.rb:28

Just use memcpy.  
Try this code.

require “Win32API”

SERVICE\_WORKSTATION = [  
76,0,97,0,110,0,109,0,97,0,110,0,87,0,111,0,114,0,  
107,0,115,0,116,0,97,0,116,0,105,0,111,0,110,0,0,0]

SERVICE\_WORKSTATION.each{|e| print “%c” % e if e \> 0}  
puts

service = SERVICE\_WORKSTATION.pack(“U\*”) # Unicode

NetStatisticsGet = Win32API.new(‘netapi32’,‘NetStatisticsGet’,‘PPNNP’,‘I’)

bufptr = “\0” \* 4  
ret = NetStatisticsGet.call(0,service,0,0,bufptr)

if 0 != ret  
puts “Failed:#{ret}”  
exit  
end

STAT\_WORKSTATION\_SIZE = (13_8) + (28_4)

rbuf = “\0” \* STAT\_WORKSTATION\_SIZE  
memcpy = Win32API.new(‘msvcrt’,‘memcpy’,‘PPL’,‘P’)  
memcpy.Call(rbuf,bufptr,STAT\_WORKSTATION\_SIZE)

struct = rbuf.unpack(“L\*”)  
p struct

**END**

Regards,

Park Heesob

---

<div class="post-metadata">

**Author:** ![Dennis\_Ranke1](https://avatars.discourse-cdn.com/v4/letter/d/dec6dc/32.png) [@Dennis\_Ranke1](https://rubytalk.org/u/Dennis_Ranke1)\
**Post date:** [11 January 2004 12:41 UTC](https://rubytalk.org/t/dereferencing-pointers-in-win32-api/9634/6 "2004-01-11T12:41:38Z")

</div>

You are right, after reading the relevant ruby source, it seems that you  
can only dereference pointers that you have written into the string  
yourself using Array#pack(‘P’). So the suggestion of Park to use memcpy  
might be your best bet.

As a sidenote to matz:

Is it intentional that String#unpack(‘P’) can raise both  
“no associated pointer” (when the string doesn’t have any associates)  
and  
“non associated pointer” (then the string does have associates, but this  
pointer isn’t among them)?

> **···**
>
> On Sat, 10 Jan 2004 14:43:19 -0600, Shashank Date [sdate@everestkc.net](mailto:sdate@everestkc.net) wrote:
> 
> > > If I’m not completely wrong, you should be able to dereference it using  
> > > lpBuffer.unpack(‘P’) where is the size of the buffer in  
> > > bytes. Just remember that unpack returns an array, the first element of  
> > > which will be the dereferenced buffer as a string.
> > 
> > I tried that and i get:
> > 
> > C:/atest/tst\_netapi32.rb:28:in `unpack’: no associated pointer  
> > (ArgumentError)  
> > from C:/atest/tst\_netapi32.rb:28
> 
> –  
> Dennis Ranke

---

<div class="post-metadata">

**Author:** ![Shashank\_Date2](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Shashank\_Date2](https://rubytalk.org/u/Shashank_Date2)\
**Post date:** [11 January 2004 15:01 UTC](https://rubytalk.org/t/dereferencing-pointers-in-win32-api/9634/7 "2004-01-11T15:01:45Z")

</div>

This worked :

> Just use memcpy.  
> Try this code.
> 
> require “Win32API”
> 
> SERVICE\_WORKSTATION = [  
> 76,0,97,0,110,0,109,0,97,0,110,0,87,0,111,0,114,0,  
> 107,0,115,0,116,0,97,0,116,0,105,0,111,0,110,0,0,0]
> 
> SERVICE\_WORKSTATION.each{|e| print “%c” % e if e \> 0}  
> puts
> 
> service = SERVICE\_WORKSTATION.pack(“U\*”) # Unicode
> 
> NetStatisticsGet = Win32API.new(‘netapi32’,‘NetStatisticsGet’,‘PPNNP’,‘I’)
> 
> bufptr = “\0” \* 4  
> ret = NetStatisticsGet.call(0,service,0,0,bufptr)
> 
> if 0 != ret  
> puts “Failed:#{ret}”  
> exit  
> end
> 
> STAT\_WORKSTATION\_SIZE = (13_8) + (28_4)
> 
> rbuf = “\0” \* STAT\_WORKSTATION\_SIZE  
> memcpy = Win32API.new(‘msvcrt’,‘memcpy’,‘PPL’,‘P’)  
> memcpy.Call(rbuf,bufptr,STAT\_WORKSTATION\_SIZE)
> 
> struct = rbuf.unpack(“L\*”)  
> p struct
> 
> **END**
> 
> Regards,
> 
> Park Heesob

Thanks, Park.  
– shanko
