Using GetTempFileName api function to check write access to a directory
If so I can rely on it and just decide based on if the returned string is empty or not


No, you may use second version but do not check for string content. More about this later.


  On windows, to find out if I have write access to a directory,


Depends what you exactly mean with write access. Your method is not bad (especially if you do not need an high performance check) however note that you're checking for permissions to:


Read file/directory list for a given directory (FILE_LIST_DIRECTORY).
Create a file in a directory (FILE_ADD_FILE).


As you can imagine this is different from write data into a file. Permissions are a complex thing and in a complex secure scenario you may have, for example, these permissions:


Granted: create a new file.
Granted: full write data into an existing file you own.
Granted: append data into an existing file you're not owner of.
Denied: discover existing files.
Denied: overwrite any existing data.
Denied: delete files (it doesn't matter if you're owner or not).


I hope I highlighted what your problem is, your actual check will fail but you still have write permissions to that folder. What you actually have to do is:


Obtain information about file security using GetFileSecurity().
Obtain DACL information about that file (GetSecurityDescriptorDacl() and GetAclInformation()).
Enumerate ACEs of your ACL to check for required permission (using GetAce() to obtain ACCESS_ALLOWED_ACE to read ACCESS_MASK field). Do not forget to interpolate these data with GetSecurityDescriptorOwner() and GetSecurityDescriptorGroup().


Funny enough to do it you need READ_CONTROL permission but they actually coincides.

What to do then? If you do need a fast implementation you may do the above (checking/skipping if insufficient permissions) and then try to write a test file (not just create it). Note that you may not have DELETE permission to remove this test file (and it may be a problem in production environments). Note that directory traverse in advanced scenarios is much more complex than you may imagine (see also FILE_TRAVERSE and BYPASS_TRAVERSE_CHECKING stuff).

Fortunately there are some ATL helper classes to simplify all these things, you may want to read The Windows Access Control Model: Part 2, in particular Do I Have Access? section.



If what you need is a simple check and you do not have a complex security setup then you may go with your own code (also second version if directory list permission is granted). Just few notes about it:


After each call to CString.GetBuffer() you must call CString.ReleaseBuffer() before object is usable again. Do it just before you call tmp_file_name.IsEmpty().
You actually do not need to check tmp_file_name content (which is actually an implementation detail even subject to change). To answer your answer then yes you may (see later) use second version but check GetTempFileName() return value, if it's zero then it failed (and you may check with GetLastError() if it's an unauthorized access error). Do not rely on output parameter, its content in case of errors is undefined.
I do not see any reason your code will throw exceptions, a catch-all statement may hide other (and more subtle) issues than write permission. I'd remove it.
You may want to use your first solution also writing something to that file if to check if you have FILE_APPEND_DATA permission. If file creation and writing are separate things (in your scenario) then first solution is the only one working.