Search code examples
winapiwindows-authenticationsmb

Log in to a windows network share (SMB) for this process only


It's easy to log into a Windows network share (SMB) for the whole user session (e.g. net use, WNetAddConnection2() etc).

Is there any similar way that will only impact my current process?

Some kind of token system, maybe?

Whole session auth is okay if there's really no alternative, but I would rather avoid a "global solution to a local problem".


Solution

  • Network sessions are handled by the windows kernel as part of the smb client driver, as such they are managed per windows workstation (not the user actually), meaning that you can't access the same share with two different credentials, and you can't actually create a new network connection per process as per the limitation imposed by the windows smb client. the reason for the limitation is that Microsoft implemented share access as a filter driver, each UNC path (\share\test) to share access is stored as a shared resource to make your live as a developer and user easier. (makes share access seamless)  

    a quick example of it is running net use in an elevated (run as administrator) command prompt and trying to access it in an unelevated context.

    you can go either way:

    1. use samba/another userland smb implementation (there are some python based) in your software
    2. create a new winstation for each of your processes (this has a big overhead)