1

It is probably me being stupid, but I am having problems detecting if a network drive is up, but only when running the program in the IDE – when running the program outside of the IDE, the network drive is correctly detected. The IDE works fine on Windows 7.

While the program in question is different, I can reproduce the issue by creating a new project and adding the following into the Forms OnActivate event:

var
  bRet: Boolean;
  LTemp2: string;
  LFreeSpace: Int64;
  LTotalSpace: Int64;
begin
  LTemp2 := 'T:\';
  bRet := GetDiskFreeSpaceEx(PAnsiChar(LTemp2), @LFreeSpace, @LTotalSpace, nil);
  ShowMessage('GetDiskFreeSpaceEx: Drive T: is up? '+BoolToStr(bRet, True));
end;

Assuming I have a networked drive T:, if I run the program in the IDE then the above always returns False, but if I run the built program from a desktop shortcut then it returns True. I get the same behaviour if I run it via a button click after the program starts. Doing DiskSize() and FindFirst() on the root directory give the same results.

It is a clean Windows 10 install, not an upgrade, with a clean install of CodeGear 2007 with all the patches applied. I have tried “Run as administrator” and all the Compatibility Modes back to Windows 7.

Am I doing something stupid?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Ciaran
  • 21
  • 4
  • Does `GetLogicalDrives()`/`GetLogicalDriveStrings()` report the `T:` drive? What about `WNetEnumResource()` and related query functions? – Remy Lebeau Mar 19 '21 at 03:11
  • Hi Remy, thanks for the reply. – Ciaran Mar 19 '21 at 15:35
  • I am not used to stackoverflow yet. Anyway, GetLogicalDrives shows the same problem, it returns 4 in the IDE (i.e. just drive C:) and 524292 outside the IDE (i.e. includes drive T:). I had not used WNetEnumResource() before, but its results are interesting: I have to dig further into enumerating the "Microsoft Windows Network" container because it returns ERROR_EXTENDED_ERROR. What is interesting is that enumerating into "Web Client Network" returns the error 1222, ERROR_NO_NETWORK, "The network is not present or not started". However, I don't know if I can force it to start from my code. – Ciaran Mar 19 '21 at 15:54
  • To which server is drive T: connected? – fpiette Mar 19 '21 at 16:05
  • 2
    Are you running Delphi with elevated privileges (because people tend to do that, based on advice to do this when running in to certain problems)? Because then network mappings are not available until you’ve accessed them using an explorer dialog (like file open) from within the IDE. – R. Hoek Mar 19 '21 at 18:18
  • Hi Francois, thanks for joining in. T: is mapped to a shared drive on a Windows 2008 server. – Ciaran Mar 19 '21 at 20:51
  • Hi, R Hoek. Brilliant: close enough. I will post an answer to clarify. – Ciaran Mar 19 '21 at 20:53
  • @R.Hoek Can you please explain why Notwork mappings are not available until accessing them through common dialog? What does opening of common dialog do to make Network mappings available? – SilverWarior Mar 20 '21 at 09:05
  • 1
    When you run an application as administrator, your basically running as a different user as the actual user. As such, there are no drive mappings available (just run a command prompt as admin, drive mappings are also not available). As soon as you use an explorer dialog (like file open) there is some ‘explorer magic’ which seems to known the actual user that invoked the admin privileges and will the automatically map network drives using that user as soon as you access them. Not sure how that works, but I suspect there are API’s to do this in other applications (or IDE components). – R. Hoek Mar 20 '21 at 10:15

1 Answers1

1

After R. Hoek's great insight, and while it does not fully sort out the issue, it is at least enough for a workaround. By the way, at least in my case, it does not matter if I launch Delphi 2007 with "Run as administrator" or not.

I added a TOpenDialog hooked up to a button: once I run this and select a file on T: then GetLogicalDrives() works fine.

So what I now suspect is that I installed Delphi 2007 as an administrator - not sure how, but I think I will re-install everything.

Even if that does not resolve it, and since it only occurs in the IDE and not in production usage, I can add code to pop up a TOpenDialog if GetLogicalDrives() returns 4.

Weird. Thank you all very much.

By the way, one aspect of this discussion may not have been clear: Delphi 2007 was running under the user account (and it could see the network drive without problems) - it is only the program which is being debugged that cannot see the networked drive. That is why using the TOpenDialog resolved the issue (until Delphi or the computer was restarted).

Ciaran
  • 21
  • 4
  • Installing as administrator will not force Delphi to always run as administrator. But are you getting a UAC dialog when you start Delphi? – R. Hoek Mar 20 '21 at 10:19
  • My understanding is the same as yours, at least for other applications. I just finished re-installing it but without "Run as administrator", and I ran the simple test program that I described above, and it now runs properly. I now believe the previous install was running as administrator always, because when I used to run the TOpenDialog and navigate to T:, there was a pause presumably when Windows was setting up the network. That pause does not occur now. So while we may not fully understand why, thanks again for getting me on the right track. I will post here if anything changes. – Ciaran Mar 20 '21 at 23:37