Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Win32 drive mapping... aka "net use"

566 views
Skip to first unread message

lsh...@my-deja.com

unread,
Jul 28, 2000, 3:00:00 AM7/28/00
to
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>").

Thanks in advance,
Lance


Sent via Deja.com http://www.deja.com/
Before you buy.

richard_c...@my-deja.com

unread,
Jul 28, 2000, 3:00:00 AM7/28/00
to
I think you want to use the win32net extension. If you select Help -
Others - Win32 Extensions in PythonWin there is a help section on it.

Richard

In article <8lr9nc$tdf$1...@nnrp1.deja.com>,

Steve Purcell

unread,
Jul 28, 2000, 3:00:00 AM7/28/00
to lsh...@my-deja.com
lsh...@my-deja.com wrote:
> 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>

Yes, look at the 'win32wnet' module provided with Mark Hammond's Win32
extensions:

http://starship.python.net/crew/mhammond/win32/Downloads.html

If I recall correctly, there is an example therein.

-Steve

--
Steve Purcell, Technical Director, Inkontact
Get in touch at http://www.inkontact.com/
Get testing at http://pyunit.sourceforge.net/
"Life must be simple if I can do it" -- Me


Alex Martelli

unread,
Jul 28, 2000, 3:00:00 AM7/28/00
to
<lsh...@my-deja.com> wrote in message news:8lr9nc$tdf$1...@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


Lance Shuler

unread,
Jul 28, 2000, 3:00:00 AM7/28/00
to
Thanks all,

Two line examples always do the trick. While the NetUseAdd title makes sense
for "net use", the documentation is not as descriptive (no reflection on Mark,
the words look like they match MSDN). I looked at NetUseAdd and then movde on
by thinking it wasn't what I needed.

Yes, I have been using the Win32 extensions (including COM) for over a year
and I own "Python Programming on Win32". I find it very useful. The example
below (including NetUseAdd) is not addressed in "Python Programming on Win32".

Alex, regarding "local". I avoid mapping to a specific drive letter and just
always use "\\server\share\dir1\dir2\file". This works when moving between
machines, without worrying about whether "K:" is in use or not. I only use
"net use" to map a share in a different domain or as a different user. For
these cases, I will use USE_INFO_2.

Thanks again,
Lance

Alex Martelli

unread,
Jul 28, 2000, 3:00:00 AM7/28/00
to
"Lance Shuler" <lance....@intel.com> wrote in message
news:3981D157...@intel.com...
[snip]

> Yes, I have been using the Win32 extensions (including COM) for over a
year
> and I own "Python Programming on Win32". I find it very useful. The
example
> below (including NetUseAdd) is not addressed in "Python Programming on
Win32".

Not exactly, no. I do think its omission is a bug in Appendix B.


> Alex, regarding "local". I avoid mapping to a specific drive letter and
just
> always use "\\server\share\dir1\dir2\file". This works when moving between
> machines, without worrying about whether "K:" is in use or not. I only use

True. I probably fell into the habit of always mapping to a drive
because of old applications that still do not support UNC (this may
have changed, but there were a few among the tools in the SDK...).


Alex


Mark Hammond

unread,
Jul 29, 2000, 3:00:00 AM7/29/00
to
> > and I own "Python Programming on Win32". I find it very useful. The
> example
> > below (including NetUseAdd) is not addressed in "Python Programming on
> Win32".
>
> Not exactly, no. I do think its omission is a bug in Appendix B.

Actually, Appendix B is only supposed to address the API functions
discussed in the book. Otherwise it would be too large.

Also, FYI: win32net does not work on Win9x. There is a module named
win32wnet which does, and also provides other useful Netbios etc
functions. However, this doesnt have any documentation in the help files
(which makes it pretty useless). Im going to fix this now.

There is a function WNetAddConnection2 - example usage is:

win32wnet.WNetAddConnection2(win32netcon.RESOURCETYPE_DISK, "Z:",
"\\\\brat\\c", None, None, None)

The params are:
Integer Type, // RESOURCETYPE_DISK, RESOURCETYPE_PRINT, or
RESOURCETYPE_ANY
String LocalName, // String or None
String RemoteName, // String (required to be in network format
String ProviderName,// String or None
String Username,
String Password)

Mark.


kel...@my-deja.com

unread,
Jul 31, 2000, 3:00:00 AM7/31/00
to
It would be nice to be able to do permanent connections. The reference
for WNetAddConnection2 says the last parameter is a flag to indicate
whether or not the connection is to be permanent. The win32wnet source
looks like it's hard coded to temporary.

Kelly

In article <sUug5.15076$4p3.1...@news-server.bigpond.net.au>,


"Mark Hammond" <Ma...@ActiveState.com> wrote:
> win32wnet.WNetAddConnection2(win32netcon.RESOURCETYPE_DISK, "Z:",
> "\\\\brat\\c", None, None, None)
>
> The params are:
> Integer Type, // RESOURCETYPE_DISK, RESOURCETYPE_PRINT, or
> RESOURCETYPE_ANY
> String LocalName, // String or None
> String RemoteName, // String (required to be in network format
> String ProviderName,// String or None
> String Username,
> String Password)
>
> Mark.

Mark Hammond

unread,
Aug 1, 2000, 3:00:00 AM8/1/00
to
<kel...@my-deja.com> wrote in message news:8m4ovo$g1s$1...@nnrp1.deja.com...

> It would be nice to be able to do permanent connections. The reference
> for WNetAddConnection2 says the last parameter is a flag to indicate
> whether or not the connection is to be permanent. The win32wnet source
> looks like it's hard coded to temporary.

good point - I've added a new optional param to handle this.

[Note that a patch would also have been acceptable ;-]

Mark.


Lucas Machado

unread,
Mar 5, 2005, 12:46:57 PM3/5/05
to

Alex Martelli wrote:

> 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).

I looked through the MSDN and was not able to find much information on
how to properly use the NetUseAdd function. I searched for the
function and looked through the results but all it showed was some data
structure, but i was actually looking for a list of all possible
arguments and which arguments were/were not required.

my problem with the above NetUseAdd example is that I would rather not
have to specify a device. In the script I am writing the user may
choose to map multiple shares so I need to be able to map to the next
available device:

net use * \\some_server\share_name

instead of:

net use k: \\server\share

Thanks for the help in advance. Also, if anyone could provide a link
to good windows api docs for python that would be great.

Cheers,
--Lucas Machado

mensa...@aol.com

unread,
Mar 5, 2005, 12:59:27 PM3/5/05
to

Lucas Machado wrote:
> Alex Martelli wrote:
>
> > 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).
>
> I looked through the MSDN and was not able to find much information
on
> how to properly use the NetUseAdd function.

C:\>net help use

> I searched for the
> function and looked through the results but all it showed was some
data
> structure, but i was actually looking for a list of all possible
> arguments and which arguments were/were not required.

The syntax of this command is:

NET USE
[devicename | *] [\\computername\sharename[\volume] [password | *]]
[/USER:[domainname\]username]
[/USER:[dotted domain name\]username]
[/USER:[username@dotted domain name]
[/SMARTCARD]
[/SAVECRED]
[[/DELETE] | [/PERSISTENT:{YES | NO}]]

NET USE {devicename | *} [password | *] /HOME

NET USE [/PERSISTENT:{YES | NO}]


NET USE connects a computer to a shared resource or disconnects a
computer from a shared resource. When used without options, it lists
the computer's connections.

devicename Assigns a name to connect to the resource or specifies
the device to be disconnected. There are two kinds of
devicenames: disk drives (D: through Z:) and printers
(LPT1: through LPT3:). Type an asterisk instead of a
specific devicename to assign the next available
devicename.
\\computername Is the name of the computer controlling the shared
resource. If the computername contains blank
characters,
enclose the double backslash (\\) and the computername
in quotation marks (" "). The computername may be from
1 to 15 characters long.
\sharename Is the network name of the shared resource.
\volume Specifies a NetWare volume on the server. You must
have
Client Services for Netware (Windows Workstations)
or Gateway Service for Netware (Windows Server)
installed and running to connect to NetWare servers.
password Is the password needed to access the shared resource.
* Produces a prompt for the password. The password is
not displayed when you type it at the password prompt.
/USER Specifies a different username with which the
connection
is made.
domainname Specifies another domain. If domain is omitted,
the current logged on domain is used.
username Specifies the username with which to logon.
/SMARTCARD Specifies that the connection is to use credentials on
a smart card.
/SAVECRED Specifies that the username and password are to be
saved.
This switch is ignored unless the command prompts for
username
and password. This option is not available on Windows
XP
Home Edition and will be ignored.
/HOME Connects a user to their home directory.
/DELETE Cancels a network connection and removes the
connection
from the list of persistent connections.
/PERSISTENT Controls the use of persistent network connections.
The default is the setting used last.
YES Saves connections as they are made, and restores
them at next logon.
NO Does not save the connection being made or subsequent
connections; existing connections will be restored at
next logon. Use the /DELETE switch to remove
persistent connections.
NET HELP command | MORE displays Help one screen at a time.

Lucas Machado

unread,
Mar 5, 2005, 1:17:40 PM3/5/05
to
I have already seen the "net help use" and i know how to manage samba
shares from a command prompt. What i need help with is using the win32
api for python to manage shares....

--Lucas

Roger Upole

unread,
Mar 5, 2005, 2:22:14 PM3/5/05
to
You could use win32api.GetLogicalDriveStrings to list
the drive letters currently in use, and find the next free
letter. net use * probably does something like that under
the covers.

hth
Roger

"Lucas Machado" <LMac...@gmail.com> wrote in message
news:1110044817.2...@o13g2000cwo.googlegroups.com...

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Lucas Machado

unread,
Mar 5, 2005, 2:45:35 PM3/5/05
to
Roger Upole wrote:
> You could use win32api.GetLogicalDriveStrings to list
> the drive letters currently in use, and find the next free
> letter. net use * probably does something like that under
> the covers.

I went and took your advice and this is where I am now:

>>> import win32api
>>> a = win32api.GetLogicalDriveStrings()
>>> a
'A:\\\x00C:\\\x00D:\\\x00E:\\\x00Z:\\\x00'
>>> print a
A:\ C:\ D:\ E:\ Z:\

So I think this function will work great since it gives me a string
that i can simply strip away what i don't need and then split it into a
list and traverse the list. However, when I try to split it, the
following occurs:

>>> b = a.strip(r'\\\x00')
>>> b
'A:\\\x00C:\\\x00D:\\\x00E:\\\x00Z:\\\x00'
>>> b = a.split(r'\\\x00')
>>> b
['A:\\\x00C:\\\x00D:\\\x00E:\\\x00Z:\\\x00']

I'm a bit of a novice at python (even more so of the win32 api), but
I've used the split and strip functions before (for example to get rid
of '\n' from strings) so it is unclear to me why this does not work.

Thanks
--LM

Roger Upole

unread,
Mar 5, 2005, 3:07:24 PM3/5/05
to
The split should work fine if you remove the r
(raw string) prefix.
>>> win32api.GetLogicalDriveStrings().split('\\\x00')
['A:', 'C:', 'D:', 'E:', 'F:', 'G:', 'H:', 'J:', 'K:', 'Y:', 'Z:', '']

Roger

"Lucas Machado" <LMac...@gmail.com> wrote in message

news:1110051935.5...@z14g2000cwz.googlegroups.com...

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----

Diez B. Roggisch

unread,
Mar 5, 2005, 3:12:11 PM3/5/05
to
>>>> b = a.strip(r'\\\x00')
>>>> b
> 'A:\\\x00C:\\\x00D:\\\x00E:\\\x00Z:\\\x00'
>>>> b = a.split(r'\\\x00')
>>>> b
> ['A:\\\x00C:\\\x00D:\\\x00E:\\\x00Z:\\\x00']
>
> I'm a bit of a novice at python (even more so of the win32 api), but
> I've used the split and strip functions before (for example to get rid
> of '\n' from strings) so it is unclear to me why this does not work.

The string you get ist actually a list of null-terminated strings. And the
byte 0 that you want to have as delimiter for splitting is written

'\x00'

So do this:

b = a.split('\x00')

Read the python docs about strings and raw strings and escaping of
characters to understand the subtle details here.

--
Regards,

Diez B. Roggisch

0 new messages