[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Win32API question - pathstriptoroot

Berger, Daniel

5/19/2005 7:41:00 PM

Hi all,

Windows XP Pro
Ruby 1.8.2

require "Win32API"

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

rv = 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


5 Answers

Austin Ziegler

5/19/2005 9:08:00 PM

0

On 5/19/05, Berger, Daniel <Daniel.Berger@qwest.com> wrote:
> require "Win32API"
>
> PathStripToRoot = Win32API.new("shlwapi","PathStripToRoot","P","L")
> path = 'C:\foo\bar'
>
> rv = 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?

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
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Brian Schröder

5/19/2005 9:53:00 PM

0

On 19/05/05, Austin Ziegler <halostatue@gmail.com> wrote:
> On 5/19/05, Berger, Daniel <Daniel.Berger@qwest.com> wrote:
> > require "Win32API"
> >
> > PathStripToRoot = Win32API.new("shlwapi","PathStripToRoot","P","L")
> > path = 'C:\foo\bar'
> >
> > rv = 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?
>
> All PathStripToRoot is doing is putting \000 after C:\. Do an #inspect.
>
> 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 = PathStripToRoot.call(path)
path = path.split(0.chr, 2)[0]
puts "RV: #{rv}"
puts "Path is [#{path}]"

work?

best regards,

Brian
--
http://ruby.brian-sch...

Stringed instrument chords: http://chordlist.brian-sch...


Paul Leonard

5/20/2005 2:58:00 AM

0

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

On Fri, 20 May 2005 06:52:37 +0900, Brian Schröder wrote:
> On 19/05/05, Austin Ziegler <halostatue@gmail.com> wrote:
>
>> On 5/19/05, Berger, Daniel <Daniel.Berger@qwest.com> wrote:
>>
>>> require "Win32API"
>>>
>>> PathStripToRoot =
>>> Win32API.new("shlwapi","PathStripToRoot","P","L") path =
>>> 'C:\foo\bar'
>>>
>>> rv = 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?
>>>
>> All PathStripToRoot is doing is putting \000 after C:\. Do an
>> #inspect.
>>
>> 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 = PathStripToRoot.call(path)
> path = path.split(0.chr, 2)[0]
> puts "RV: #{rv}"
> puts "Path is [#{path}]"
>
> work?
>
> best regards,
>
> Brian




Daniel Berger

5/20/2005 3:20:00 AM

0


Paul Leonard wrote:
> 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

Paul Leonard

5/20/2005 12:34:00 PM

0

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

On Fri, 20 May 2005 12:25:14 +0900, Daniel Berger wrote:
>
> Paul Leonard wrote:
>> 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