NULL pointer given, Win32API

B

Berger, Daniel

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
 
S

Shusaku

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.

MSDN> HANDLE OpenEventLog(
MSDN> LPCTSTR lpUNCServerName, // pointer to server name
MSDN> LPCTSTR lpSourceName // pointer to source name
MSDN> );
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top