# NULL pointer given, Win32API

**URL:** <https://rubytalk.org/t/null-pointer-given-win32api/9697>\
**Category:** ruby-talk\
**Created:** [15 January 2004 16:28 UTC](https://rubytalk.org/t/null-pointer-given-win32api/9697 "2004-01-15T16:28:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Berger\_Daniel2](https://avatars.discourse-cdn.com/v4/letter/b/9fc348/32.png) [@Berger\_Daniel2](https://rubytalk.org/u/Berger_Daniel2)\
**Post date:** [15 January 2004 16:28 UTC](https://rubytalk.org/t/null-pointer-given-win32api/9697/1 "2004-01-15T16:28:23Z")

</div>

Hi all,

Windows XP  
Ruby 1.8.1

How do I pass a straight-up string to a function in the Win32API? I  
understand how to pad a buffer store a value in a buffer, but not how to  
convert a Ruby string to an LPCTSTR, for example. I’m guessing that I  
pack’d it wrong.

require "Win32API"  
module Win32  
CloseEventLog = Win32API.new(“advapi32”,“CloseEventLog”,“p”,“i”)

```
OpenEventLog = Win32API.new("advapi32","OpenEventLog","pp","p")
class EventLog
	def initialize(source,machine=0)
		lpSource = [source].pack("p")
		unless 0 == machine
			lpMachine = [machine].pack("p")
		end
		@h = OpenEventLog.call(source,machine)
		return @h
	end
	
	def close
		CloseEventLog.call(@h)
	end
end

```

end

if $0 == **FILE**  
e = EventLog.new(“Application”) # results in "NULL pointer given"  
e.close  
end

This is part of an effort to provide pure-ruby versions of the  
win32utils code. Please help.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![Shusaku](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@Shusaku](https://rubytalk.org/u/Shusaku)\
**Post date:** [15 January 2004 19:05 UTC](https://rubytalk.org/t/null-pointer-given-win32api/9697/2 "2004-01-15T19:05:19Z")

</div>

Hi,

> OpenEventLog = Win32API.new(“advapi32”,“OpenEventLog”,“pp”,“p”)  
> …  
> def initialize(source,machine=0)  
> …  
> @h = OpenEventLog.call(source,machine)

It seems that the parameters of the API are passed in wrong order.

> HANDLE OpenEventLog(  
> LPCTSTR lpUNCServerName, // pointer to server name  
> LPCTSTR lpSourceName // pointer to source name  
> );

> e = EventLog.new(“Application”) # results in “NULL pointer given”

I don’t know why this error message is claimed, although it is  
certain that the message claims for a return value of zero.  
Needless to say, zero means a failure of API call because of  
wrong ordered parameters.

The work-around of this problem is to declare ‘HANDLE’ as “l” or “i”  
instead of “p”.

But, I think there should be more reasonable solution.

> **···**
>
> On Thu, 15 Jan 2004 10:27:50 -0600 “Berger, Daniel” [djberge@qwest.com](mailto:djberge@qwest.com) wrote:
> 
> –  
> Shusaku [tsyk@yk.rim.or.jp](mailto:tsyk@yk.rim.or.jp)
