Win32 drive mapping... aka "net use"

Alex Martelli alex at magenta.com
Fri Jul 28 08:41:56 EDT 2000


<lshuler at my-deja.com> wrote in message news:8lr9nc$tdf$1 at nnrp1.deja.com...
> Is there an API using Win32 extensions to map a
> drive share? I have not found one yet and have
> tried searching "Win32 map drive". I am looking
> for the equivalent of:
>
> net use <share path>
>
> If not, is there an alternative other than making
> a call to system("net use <path>").

import win32net
win32net.NetUseAdd(None,1,{'remote':r'\\server\share','local':'K:'})

is an example (not all that easy to fathom from the docs, but I
found it out with a little help from the docs, a little from MSDN,
and a little experimentation).  If you're not already validated with
the server, you may need to add a 'password':'whatever' entry
to the dictionary that is the third argument to NetUseAdd.  But
this is equivalent to
    net use K: \\server\share
as I do not know what net use means _without_ the local
devicename (e.g., the 'K:' here).

win32net contains all the API's you may desire to emulate
"net use" and other subcommands of "net"; much of what
you need to know about it, you can gather from MSDN's
entries about "Platform SDK: Network Management" (use
msdn.microsoft.com for online access, if you don't have a
handy CD or DVD with MSDN on it), but of course there is
some mapping between the argument types involved.  Where
the original (C-level) API wants a structure, win32net uses a
dictionary instead, with entries corresponding to the struct's
fieldnames shorn of prefixes (e.g., the struct USE_INFO_1
that the C-level NetUseAdd API wants has fields named
ui1_remote and ui1_local).

But if you plan to do a lot of system programming and/or
system administration on Windows with Python (a very
workable and excellent plan, as it happens) you really
should splurge for the "Python Programming on Win32"
book by Hammond and Robinson, published by O'Reilly.
It's not perfect (for example, I can't find in its reference
Appendix any specific info on NetUseAdd, while it does
document a lot of OTHER win32net functions!-) but it
still helps a lot.


Alex






More information about the Python-list mailing list