Why doesn't my program recognize mapped UNC paths?
Asked Answered
M

1

6

I have some problems writing to a mapped network drive (P:) in Windows 7 from my Delphi program. When I try, for example, ForceDirectories('P:\test\folder'), I get an error (path not found).

I have tried typing in the UNC path in the code (ForceDirectories('\\computername\share\test\folder')) and that works. However, ExpandUNCFileName('P:\') does not seem to work; it returns 'P:\'. On Windows XP, ExpandUNCFileName('P:\') returns the UNC path.

How do I get the UNC path in Delphi on Windows 7, or otherwise write to a mapped network drive?

Upon further investigation, it is as if I'm missing some kind of initialization in Windows. I have another application (app2) that uses a TcxShellComboBox (a DevExpress component). After having navigated to P: in that combobox, in app2, calls to ExpandUNCFileName work correctly in the first application. Same with FileExists on files under P:, returns False before navigating to P: in app2, returns true after and until computer restart.

Meilen answered 6/10, 2011 at 12:0 Comment(9)
Is this by any chance a service? The behaviour you describe has nothing to do with Delphi and occurs because the drive mapping is not setup for the process.Beneficence
Verify that you have the drive letter mapped to the share. I was able to reproduce your problem only if the drive letter was not mapped (Delphi 7, 2007 XE).Harday
It is not a service, it is a VCL Forms application.Meilen
I'd be astonished if this was anything other than drive mapping related. Can you open files under P:?Beneficence
I can open files under ´P:´from windows explorer.Meilen
Could you be using a ForceDirectories from a library other than SysUtils? There is a version in JclFileUtils and the archived JvFileUtils and JvUtils.Harday
Using ForceDirectories in FileCtrl, which calls SysUtils. Calls to ForceDirectories also work after navigating to P: in app2, both with UNC path and with mapped path.Meilen
Note that if your Delphi app is "elevated" (UAC) and you mapped the drive via non-elevated explorerer, or vice versa, the drive map won't be available as elevated and non-elevated are treated as two separate users in this aspect.Preceptor
@Preceptor I am running Delphi with "Run this program as an administrator". I tried to uncheck it and the the program could look up the UNC name. Is there some function I can call to be able to use mapped drives when elevated? Some parts of the program requires elevated privileges.Meilen
P
12

According to Microsoft KB Article, if User Account Control is enabled, and if you map a network drive from Windows Explorer (non-elevated), then elevated programs won't have access to that drive. Quote:

If a user is logged on to Windows Vista or to Windows 7, and if User Account Control is enabled, a program that uses the user’s filtered access token and a program that uses the user’s full administrator access token can run at the same time. Because LSA created the access tokens during two separate logon sessions, the access tokens contain separate logon IDs.

When network shares are mapped, they are linked to the current logon session for the current process access token. This means that, if a user uses the command prompt (Cmd.exe) together with the filtered access token to map a network share, the network share is not mapped for processes that run with the full administrator access token.

Since you mentioned in comments that you run Delphi "As Administrator" (elevated), this is your problem.

Solutions:

  1. Don't run Delphi elevated if you don't need to. If you do need an elevation in your software, separate it into two parts (elevated and non-elevated), and access mapped network drive from the first part. Then access the other part using an elevated COM object, or simply by executing a separate executable.

  2. Map a network drive from an elevated network prompt, so the mapped network drive will be available to elevated user:

    a. Open elevated command prompt (run "cmd.exe" as Administrator)

    b. Type net use p: \\computername\share\test\folder

Preceptor answered 6/10, 2011 at 15:32 Comment(1)
if you choose the second solution, you'll need to map it a second time from your regular user(without elevation) if you usually need it...Bortz

© 2022 - 2024 — McMap. All rights reserved.