4

TPath.GetTempFileName (which wraps the WinAPI GetTempFileName) replies "The directory name is invalid" when called from an application run by a user who is logged into a domain.

if they use a login that isn't using the domain, it works.

The customer having a problem is in another country and I am also not familiar with how a domain controller's configuration could be changed to avoid this problem.

I assume that since my application is the one that isn't working correctly, I should be getting a temporary file name in a different way.

"run as administrator" doesn't help.

I have directed them to ensure they have full control over the folders mentioned in the TEMP & TMP system environment variables and apparently they do but it still gives the same error.

My application as a Win32 Delphi desktop application but since Windows is the source of the error, I assume this information to be of limited usefulness.

Windows 10 is the OS.

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
X-Ray
  • 2,816
  • 1
  • 37
  • 66
  • Also, what do you mean by `GetTempFileName replies "The directory name is invalid"`? Is this an error message returned from Windows after calling this function? – Jerry Dodge Aug 24 '16 at 15:31
  • TPath.GetTempFileName fails and I don't understand why. Will add some extra information to the question. They're on the other side of the world and I can't create a similar situation here. It seems smarter to ask for some kind of suggestion what I'm doing wrong or have failed to consider. Have used TPath.GetTempFileName for quite a few years without difficulty. – X-Ray Aug 24 '16 at 15:31
  • TPath.GetTempFileName raises an exception "The directory name is invalid". I have tried to generalize the question since it would seem obvious that Windows' GetTempFileName( ) is the one returning an error, causing the exception. – X-Ray Aug 24 '16 at 15:32
  • Oh so you're using it through `TPath`, and here I was looking into the direct API call. So that was time I just wasted. You should put that in your question, that you're using `TPath`. Because such an API call cannot raise such an exception by itself, I was puzzled what you meant. – Jerry Dodge Aug 24 '16 at 15:35
  • I can see TPath directly calls GetTempFileName( ) and the stack tracer (MadExcept) says the exception is in TPath.GetTempFileName( ) not in TPath.GetTempPath( ) so therefore, GetTempFileName( ) must be returning the error that raises the exception. In my estimation, I provide better information to anyone looking at this question by looking deeper... – X-Ray Aug 24 '16 at 15:38
  • Yes, it wraps it, but with more additional code. I wasn't even aware such a call existed in `TPath` in the first place. Based on your question, I went straight to MSDN docs to see if it's even possible for this API function to raise an exception, because WinAPI calls are not supposed to raise exceptions themselves. – Jerry Dodge Aug 24 '16 at 15:40
  • Regardless, now that I know it's a problem in Delphi's `TPath`, +1. – Jerry Dodge Aug 24 '16 at 15:59
  • 3
    Your program is correct and you should not change it. Your user needs to fix their broken system. – David Heffernan Aug 24 '16 at 16:13
  • @JerryDodge It isn't a problem with `TPath`. That code is correct. – David Heffernan Aug 24 '16 at 16:14
  • @David I figured it *should* check the result of `GetTempPath`. My point is that now I understand there's nothing wrong with OP's code. At first, I thought OP was calling the WinAPI function directly, in which case the problem could have been in code we couldn't see. – Jerry Dodge Aug 24 '16 at 16:18
  • I investigated this a little, and came across this link: http://serverfault.com/questions/574724/what-are-the-pros-cons-of-blocking-a-program-from-running-in-appdata-temp I'd be curious if this user is permitted to even install software while logged into a domain. – Jerry Dodge Aug 24 '16 at 17:04
  • 1
    Actually there is an error in my view. GetTempPath should raise an OS error if the API call fails. Instead it returns empty string. That's what is happening here. But GetTempPath should fail hard. – David Heffernan Aug 24 '16 at 18:23
  • Thank you all. I'm having them run a test application that will reveal what the windows API functions are themselves returning. Will advise. – X-Ray Aug 25 '16 at 01:35

1 Answers1

10

TPath.GetTempFileName calls TPath.GetTempPath at the very beginning but does not check it before calling Winapi.Windows.GetTempFileName using the returned path.

It is very likely that the call to TPath.GetTempPath returns an empty or invalid path.

MSDN says:

The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:

  1. The path specified by the TMP environment variable.
  2. The path specified by the TEMP environment variable.
  3. The path specified by the USERPROFILE environment variable.
  4. The Windows directory.

If it would return the Windows directory, the call wouldn't fail with the said message. So, probably, there is a wrong path in one of those three environment variables.

Your customer should check these variables and validate them, in terms of existence. You say, that the paths are "apparently" okay.

Experience taught me to doubt what customers say they checked... You could make a call to TPath.GetTempPath on your own before calling TPath.GetTempFileName to check if it exists. Alternatively you can call it in case of failure as part of handling the raised exception and add the path to the error message.

René Hoffmann
  • 2,766
  • 2
  • 20
  • 43
  • 2
    If `TPath.GetTempPath()` returns a blank string, you can use `GetLastError()` to find out why `Windows.GetTempPath()` failed. But do be aware that `TPath.GetTempPath()` can [return a *truncated* path](http://stackoverflow.com/questions/3440492/). Internally, it (mis-)uses [`Windows.GetLongPathName()`](https://msdn.microsoft.com/en-us/library/windows/desktop/aa364980.aspx) but does not check for a conversion failure. – Remy Lebeau Aug 24 '16 at 19:59
  • Thank you Remy; you are correct. The problem I have seen is http://qc.embarcadero.com/wc/qcmain.aspx?d=92006 . I would be pleased to mark your comment as the answer. – X-Ray Aug 29 '16 at 15:23