Win32API question - pathstriptoroot

B

Berger, Daniel

Hi all,

Windows XP Pro
Ruby 1.8.2

require "Win32API"

PathStripToRoot =3D Win32API.new("shlwapi","PathStripToRoot","P","L")
path =3D 'C:\foo\bar'

rv =3D PathStripToRoot.call(path)
puts "RV: #{rv}"
puts "Path is [#{path}]"

With that I end up with:

RV: 1
Path is [C:\ oo bar]

Do I need to encode and/or decode 'path' somehow first?

Regards,

Dan
 
A

Austin Ziegler

require "Win32API"
=20
PathStripToRoot =3D Win32API.new("shlwapi","PathStripToRoot","P","L")
path =3D 'C:\foo\bar'
=20
rv =3D PathStripToRoot.call(path)
puts "RV: #{rv}"
puts "Path is [#{path}]"
=20
With that I end up with:
=20
RV: 1
Path is [C:\ oo bar]
=20
Do I need to encode and/or decode 'path' somehow first?

All PathStripToRoot is doing is putting \000 after C:\. Do an #inspect.

I'm not sure what the right answer is in this case.

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
B

Brian Schröder

require "Win32API"

PathStripToRoot =3D Win32API.new("shlwapi","PathStripToRoot","P","L")
path =3D 'C:\foo\bar'

rv =3D PathStripToRoot.call(path)
puts "RV: #{rv}"
puts "Path is [#{path}]"

With that I end up with:

RV: 1
Path is [C:\ oo bar]

Do I need to encode and/or decode 'path' somehow first?
=20
All PathStripToRoot is doing is putting \000 after C:\. Do an #inspect.
=20
I'm not sure what the right answer is in this case.

I can't test it because I'm on linux, but wouldn't
rv =3D PathStripToRoot.call(path)
path =3D path.split(0.chr, 2)[0]
puts "RV: #{rv}"
puts "Path is [#{path}]"

work?

best regards,

Brian
--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
P

Paul Leonard

The api call is just taking the char buffer you're sending it, dropping a=
null after the path to terminate it just like a normal Windows application=
would expect. I would have thought that the Win32API module would take care=
of this sort of thing for you, but that doesn't seem to be the case.
You could do the split, or the amusing but probably terrible:

puts "Path is [#{path.unpack(A4*).join}]"

Paul Leonard

=A0On 19/05/05 said:
=A0On 5/19/05 said:
=A0require "Win32API"

=A0PathStripToRoot =3D
=A0Win32API.new("shlwapi","PathStripToRoot","P","L") path =3D
=A0'C:\foo\bar'

=A0rv =3D PathStripToRoot.call(path)
=A0puts "RV: #{rv}"
=A0puts "Path is [#{path}]"

=A0With that I end up with:

=A0RV: 1
=A0Path is [C:\ oo bar]

=A0Do I need to encode and/or decode 'path' somehow first?
=A0All PathStripToRoot is doing is putting \000 after C:\. Do an
=A0#inspect.

=A0I'm not sure what the right answer is in this case.
=A0I can't test it because I'm on linux, but wouldn't
=A0rv =3D PathStripToRoot.call(path)
=A0path =3D path.split(0.chr, 2)[0]
=A0puts "RV: #{rv}"
=A0puts "Path is [#{path}]"

=A0work?

=A0best regards,

=A0Brian
 
D

Daniel Berger

Paul said:
The api call is just taking the char buffer you're sending it,
dropping a null after the path to terminate it just like a normal
Windows application would expect. I would have thought that the
Win32API module would take care of this sort of thing for you, but that
doesn't seem to be the case.
You could do the split, or the amusing but probably terrible:

puts "Path is [#{path.unpack(A4*).join}]"

Paul Leonard

I don't think it's Win32API per se, so much as just the way Ruby
handles strings. I've picked up '\000' before in strings parsing /proc
on Linux, for example. I'll just have to deal with it (or use C). :)

Thanks to everyone who replied.

Regards,

Dan
 
P

Paul Leonard

I think that it could be that the Win32API package is handline lzstptr=
parameters as Ruby strings as opposed to null terminated byte arrays. The=
behavior that is being displayed is much more like the BSTR, which (while=
still being null terminated) also carries a length value, so it can include=
/000 in the data.

Should the two be handled differently? I would say yes, that if you're=
calling an API that is taking a long zero char pointer as a string value,=
then you should approach it differently than if the API call is expecting a=
BSTR.

Paul

=A0Paul Leonard said:
=A0The api call is just taking the char buffer you're sending it,
=A0dropping a null after the path to terminate it just like a normal
=A0Windows application would expect. I would have thought that the
=A0Win32API module would take care of this sort of thing for you, but
=A0that doesn't seem to be the case.
=A0You could do the split, or the amusing but probably terrible:

=A0puts "Path is [#{path.unpack(A4*).join}]"

=A0Paul Leonard
=A0I don't think it's Win32API per se, so much as just the way Ruby
=A0handles strings. =A0I've picked up '\000' before in strings parsing
=A0/proc on Linux, for example. =A0I'll just have to deal with it (or
=A0use C). :)

=A0Thanks to everyone who replied.

=A0Regards,

=A0Dan
 

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