0

I'm coding a small application which can be seen here that executes a net.exe command in cmd to add a network location as a drive. (I'm only linking my coding question incase it relates somehow and someone wants to contribute over on stackoverflow.)

net use z: \\PCName\d$\Folder1\Folder2

I'm currently having issues with the command executing through my application not behaving as it should. By that I mean cmd indicates the command executed as it should have but not action has actually taken place:

indicates the command executed as it should

However when I enter my command manually it behaves as you would expect:

it behaves as you would expect

When I add drive through my application nothing is added and when I enter the command myself through cmd the drive appears in Windows Explorer.

At first I thought it might be a coding issue but after struggling with the problem I think my application is rather sound. Now I am on the train of thought that it might be Windows or another application behaving oddly.

It might be worth noting that I am at work where there is a lot of network policy and security however I do have permission to access and drives.

Could it be Windows, an application or my network causing this behavior or should that not be the case?

2 Answers2

1

Windows is working as designed and your code is fine. The trick is that your program is running as administrator, but Explorer is not running elevated. When your program maps the network drive, the mapping is made available to processes in the same logon session - not user account. From MSDN:

...connections made using Microsoft LAN Manager are visible only to applications running in the same logon session as the application that made the connection. (To include the connection in the enumeration, it is not sufficient for the application to be running in the user account that created the connection.)

If you open another administrative command prompt, you'll see the drive that your program mapped. If your program was not running as admin, you would see its mapped drives in Explorer.

To make non-admin and admin applications see the same set of mappings, create a DWORD called EnableLinkedConnections set to 1 inside this key (source):

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System

Once you reboot, your elevated program will make shares that your non-elevated other applications can see.

Ben N
  • 42,308
0

According to your screenshots, your program is running in elevated mode ("as Administrator"), where as the command prompt you ran it manually from isn't.

When you run things "As administrator" it uses a different user context than your normal user, so mapped drives created in one context are not available to the other.

See this existing related SuperUser question for additional info/guidance: