Sunday, August 31, 2008

Win32Exception error code 267 "The directory name is invalid"

I'm currently working on an "Automated Run As" tool. Its goal is helping admins which, like me, have to give users a means to execute one or two programs as Administrator and would like to do so without having to surrender an admin's password.

So, I'm developing on Vista and I just whipped up a small proof of concept prototype, that'd run calc.exe as a different user, using ProcessStartInfo and Process. This worked fine when I executed it as myself (a rather pointless exercise, I must admit), but when I created a new user and tried to run it as him, I stumbled upon a Win32Exception complaining that the directory name is invalid, native error code 267. I was instsantly baffled, as I knew of no supplied directory name that could be invalid. I then tested the code on an XP machine and it worked!

I started googling on it to no avail, many reports of that error but no conclusive solution, or on different contexts. Finally, after a while it dawned on me, I wasn't specifying the WorkingDirectory property of the ProcessStartInfo class, as soon as I added the lines

FileInfo fileInfo = new FileInfo(path);
startInfo.WorkingDirectory = fileInfo.DirectoryName;

to my code, it was allowed to run code as different than logged in user.

As to why, I haven't looked deep enough yet, I think that it may have something to do with default values of the property or with default behavior when it is not specified, plus default permissions (which were tightened on Vista, if I'm not mistaken).

Do *you*, my non existent reader, know why?

9 comments:

Anonymous said...

You don't have any reader, so why bother write ? ;-)

Locivars Vonvik said...

I write to practice, and to leave a record of things that might interest me or anyone else later

Anonymous said...

You do have a reader, and a very appreciative one at that! I can't believe the solution, but it works :)

Bosske said...

Hi there, You won't believe it, but we are working on the same problem, and we have the same problem - UAC. We have found a solution to the problem You are having, but we encountered another issue - UAC. We are wondering if You had the same problem, and if You did, can You PLEASE tell us how You solved it, because it has been a big show stopper for us... You can find our code sample below:

Did you try to do it with a different user? I strugle with this for a days. It just does not elevate user rights.
Code Example:

ProcessStartInfo processInfo = new ProcessStartInfo();
SecureString securePassword = new SecureString();
foreach (char ch in “testpass”)
{
securePassword.AppendChar(ch);
} // foreach
processInfo.UserName = “testuser”;
processInfo.Password = securePassword;
processInfo.Verb = “runas”;
processInfo.FileName = @”c:\windows\system32\cmd.exe”;
processInfo.UseShellExecute = false;
Process.Start(processInfo);

testuser is administrator. Problem is that this code does not run the process as elevated.

Locivars Vonvik said...

Bosske, it does run as a different user with the proper privileges. My code does not use the Verb property of PSI though.

The property I'm using that you are not is the Domain property. In case of no domain you can supply a single dot character.

Anonymous said...

Found this page from a google search on error 267.

At first I thought "this doesn't apply to me" because I was just using runas from the command prompt.

Then I realized I was sitting inside the personal directory of the original user I was launching from. (I don't know if that line made any sense)

I changed my directory to one that both users could access easily and voila! it worked! I only knew to try that because of this post, so thank you very much.

Frederic Torres said...

Thank you for this post

Unknown said...
This comment has been removed by the author.
Anonymous said...

Thanks a lot