fomcl at yahoo.com
Sun Mar 25 10:12:00 CEST 2012
> From: Tim Golden <mail at timgolden.me.uk>
>To: Albert-Jan Roskam <fomcl at yahoo.com>
>Cc: Python Mailing List <tutor at python.org>
>Sent: Saturday, March 24, 2012 11:25 PM
>Subject: Re: [Tutor] getUncPath(mappedDrive)
>On 24/03/2012 21:29, Albert-Jan Roskam wrote:
>> Thanks! This seems a feasible approach. I have found this Python
>> project that exposes some of the functions of mpr.dll:
>> http://sourceforge.net/projects/wnetconnect/ WNetGetConnection is
>> not among the functions, but the code will help. I have to read up
>> on ctypes.Structure though as I never really understood this.
>This particular function call doesn't require too much work
>in fact. Something like the following code -- error-handling
>mostly omitted -- should do the trick:
># Get the ANSI version of the function from its DLL
>WNetGetConnection = ctypes.windll.mpr.WNetGetConnectionA
>ERROR_MORE_DATA = 234
># Set up the drive name to map back from
># and an empty buffer with zero length.
>local_name = "Z:"
>length = ctypes.c_long (0)
>remote_name = ctypes.create_string_buffer ("")
># Call the function, expecting to receive an ERROR_MORE_DATA
># result, which indicates that the buffer is too small and
># which populates the length field with the right length.
>result = WNetGetConnection (
> ctypes.byref (length)
># Assuming we did get that error, recreate the buffer and
># call again with the supplied length. This isn't strictly
># necessary (you could probably get away with hard-coding
># 2048 or whatever) but it does save you having to guess.
>if result == ERROR_MORE_DATA:
> remote_name = ctypes.create_string_buffer (length.value)
> result = WNetGetConnection (
> ctypes.byref (length)
># If the result of either call was an error, raise an Exception
>if result != 0:
> raise RuntimeError ("Error %d" % result)
>print "Remote name is", remote_name.value
>Thank you so much for this! I think this would also be a valuable addition to os.path (where I'd expect it to be).
>You call WNetGetConnection twice: one time with a 'dummy' string buffer, and one time with a buffer of the exact required length. If I'd run a function getUncPath() on a gazillion paths, wouldn't it be more effcient to hard-code the length to 2048 as you suggest in your comment?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor