Win32API help: PathFindOnPath()

D

Daniel Berger

Hi all,

The second argument is an optional null-terminated array of
directories. How do I create the Ruby structure to pass to it?

require "Win32API"

PathFindOnPath = Win32API.new("shlwapi","PathFindOnPath","PP","L")

file = "ruby.exe"
path = "C:\\foo;C:\\bar"

p PathFindOnPath.call(file, path) # fails

What's the proper way to call this? Thanks.

Dan

# Actual C function declaration
BOOL PathFindOnPath(
LPTSTR pszFile,
LPCTSTR *ppszOtherDirs
);
 
N

nobuyoshi nakada

Hi,

At Tue, 8 Nov 2005 14:12:12 +0900,
Daniel Berger wrote in [ruby-talk:164722]:
The second argument is an optional null-terminated array of
directories. How do I create the Ruby structure to pass to it?

$ ruby18-mswin32 -rWin32API -e '
PathFindOnPath = Win32API.new("shlwapi","PathFindOnPath","PP","L")
file = "ruby.exe"+"\0"*256
path = ENV["PATH"].split(File::pATH_SEPARATOR).pack("p*")
p PathFindOnPath.call(file, path)
p file[/\A[^\0]+/]'
1
"c:\\cygwin\\bin\\ruby.exe"
 
N

nobu.nokada

Hi,

At Tue, 8 Nov 2005 15:11:58 +0900,
nobuyoshi nakada wrote in [ruby-talk:164725]:
path = ENV["PATH"].split(File::pATH_SEPARATOR).pack("p*")

Sorry, the terminator was missing.

path = (ENV["PATH"].split(File::pATH_SEPARATOR) << nil).pack("p*")
 
D

daz

Thanks for posting that tricky snippet, Nobu.

IIUC, the ENV['PATH'] prefix is /not/ required because environment
PATH is /always/ searched after any additional dirs added by us.

Instead of asking you for clarification, I'll assume (A) you don't
expect M$ API to do anything useful ;) or (B) you pasted it
from a similar example and forgot to remove it.


Test:
#---------------------
require 'Win32API'
PathFindOnPath = Win32API.new('shlwapi', 'PathFindOnPath', 'PP', 'L')

def path_fop(file, *xtra)
buf = file + ("\0" * (260-file.length)) # buflen >= MAX_PATH(260)
path = (xtra << nil).pack('p*')
PathFindOnPath.call(buf, path).nonzero? and buf[/\A[^\0]+/]
end

puts path_fop('set_test.rb', 'C:\TEMP', 'C:\TEMP\clr') #-> C:\TEMP\clr\set_test.rb
puts path_fop('set_test.rb', 'C:\TEMP' ) #-> nil
puts path_fop('kernel32.dll', 'C:\TEMP', 'C:\TEMP\clr') #-> C:\WINDOWS\SYSTEM\kernel32.dll
puts path_fop('kernel32.dll' ) #-> C:\WINDOWS\SYSTEM\kernel32.dll
#---------------------


daz
 
N

nobuyoshi nakada

Hi,

At Wed, 9 Nov 2005 00:07:12 +0900,
daz wrote in [ruby-talk:164771]:
Instead of asking you for clarification, I'll assume (A) you don't
expect M$ API to do anything useful ;) or (B) you pasted it
from a similar example and forgot to remove it.

(C) just hadn't heard of that API.
 

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,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top