<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><br> <div><br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; margin-top: 5px; padding-left: 5px;">  <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div dir="ltr"> <font size="2" face="Arial"> <hr size="1">  <b><span style="font-weight:bold;">From:</span></b> Tim Golden &lt;mail@timgolden.me.uk&gt;<br> <b><span style="font-weight: bold;">To:</span></b> Albert-Jan Roskam &lt;fomcl@yahoo.com&gt; <br><b><span style="font-weight: bold;">Cc:</span></b> Python Mailing List &lt;tutor@python.org&gt; <br> <b><span style="font-weight: bold;">Sent:</span></b> Saturday, March 24, 2012 11:25 PM<br> <b><span style="font-weight: bold;">Subject:</span></b> Re: [Tutor] getUncPath(mappedDrive)<br>
 </font> </div> <br>
On 24/03/2012 21:29, Albert-Jan Roskam wrote:<br>&gt;&nbsp; &nbsp;  Thanks! This seems a feasible approach. I have found this Python<br>&gt;&nbsp; &nbsp;  project that exposes some of the functions of mpr.dll:<br>&gt;&nbsp; &nbsp;  http://sourceforge.net/projects/wnetconnect/ WNetGetConnection is<br>&gt;&nbsp; &nbsp;  not among the functions, but the code will help. I have to read up<br>&gt;&nbsp; &nbsp;  on ctypes.Structure though as I never really understood this.<br><br>This particular function call doesn't require too much work<br>in fact. Something like the following code -- error-handling<br>mostly omitted -- should do the trick:<br><br>&lt;code&gt;<br>import ctypes<br>#<br># Get the ANSI version of the function from its DLL<br>#<br>WNetGetConnection = ctypes.windll.mpr.WNetGetConnectionA<br><br>ERROR_MORE_DATA = 234<br><br>#<br># Set up the drive name to map back from<br># and an empty buffer with zero length.<br>#<br>local_name = "Z:"<br>length =
 ctypes.c_long (0)<br>remote_name = ctypes.create_string_buffer ("")<br><br>#<br># Call the function, expecting to receive an ERROR_MORE_DATA<br># result, which indicates that the buffer is too small and<br># which populates the length field with the right length.<br>#<br>result = WNetGetConnection (<br>&nbsp; local_name,<br>&nbsp; remote_name,<br>&nbsp; ctypes.byref (length)<br>)<br>#<br># Assuming we did get that error, recreate the buffer and<br># call again with the supplied length. This isn't strictly<br># necessary (you could probably get away with hard-coding<br># 2048 or whatever) but it does save you having to guess.<br>#<br>if result == ERROR_MORE_DATA:<br>&nbsp; remote_name = ctypes.create_string_buffer (length.value)<br>&nbsp; result = WNetGetConnection (<br>&nbsp; &nbsp; local_name,<br>&nbsp; &nbsp; remote_name,<br>&nbsp; &nbsp; ctypes.byref (length)<br>&nbsp; )<br><br>#<br># If the result of either call was an error, raise an
 Exception<br>#<br>if result != 0:<br>&nbsp; raise RuntimeError ("Error %d" % result)<br><br>print "Remote name is", remote_name.value<br><br>&lt;/code&gt;<br><br>TJG<br><br>Hi Tim,<br><br>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).<br>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?<br><br>Regards,<br>Albert-Jan<br> </div> </div> </blockquote>  </div></div></body></html>