Best winapi questions in July 2011

How to correctly implement "As Administrator" or "Run As Administrator" in .NET application for selected operations?

6 votes

I'm looking for correct way to allow elevating privileges for selected operations.

Something similar many products offer.

For example if you have UAC turned on and you are using Total Commander (running with basic privileges) when browsing restricted folders you will get:

enter image description here

If you push As Administrator you will get common User Access Control dialog asking if you want to grant the process required permissions. Is it something similar possible in .NET application (even with Win32 support)?

  1. How should be such functionality correctly implemented?
  2. Do I need to run external process to get elevated privileges just for selected functionality?

I'm not looking for solution which will demand running the main process as administrator.

You're going to have to factor the target operation functionality into a separate component that will be run elevated. This can be either an executable that requires elevation or a COM component (which could presumably be a COM wrapper for a .NET component). Details are available at http://msdn.microsoft.com/en-us/library/bb756990.aspx, with the When to Add the Shield Icon to Your Application's User Interface being particularly pertinent.

Start external app with ShellExecuteEx and wait until it become initialized.

5 votes

I have an application which needs to run several other applications in chain. I am running them via ShellExecuteEx. The order of running each of the apps is very important cause they are dependant on each other. For example:

Start(App1);

If App1.IsRunning then
  Start(App2);
If App2.IsRunning then
  Start(App3);
.........................
If App(N-1).IsRunning then
  Start(App(N));

Everything works fine but there is a one possible problem: ShellExecuteEx starts the application, and return almost immediately. The problem might arise when for example App1 has started properly but has not finished some internal tasks, it is not yet ready to use. But ShellExecuteEx is already starting App2 which depends on the App1, and App2 won't start properly because it needs fully initialized App1.

Please note, that I don't want to wait for App(N-1) to finish and then start AppN.

I don't know if this is possible to solve with ShellExecuteEx, I've tried to use

SEInfo.fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_NOASYNC;

but without any effect.

After starting the AppN application I have a handle to the process. If I assume that the application is initialized after its main window is created (all of Apps have a window), can I somehow put a hook on its message queue and wait until WM_CREATE appears or maybe WM_ACTIVATE? In pressence of such message my Application would know that it can move on.

It's just an idea. However, I don't know how to put such hook. So if you could help me in this or you have a better idea that would be great:)

Also, the solution must work on Windows XP and above.

Thanks for your time.

Edited

@Cosmic Prund: I don't understand why did you delete your answer? I might try your idea...

You can probably achieve what you need by calling WaitForInputIdle() on each process handle returned by ShellExecute().

Waits until the specified process has finished processing its initial input and is waiting for user input with no input pending, or until the time-out interval has elapsed.

Delete a file opened for exclusive access in the same process

5 votes

My windows program receives information from another program via directory/file interface. That is the other program drops files into a special directory. My program periodically scans the directory, finds the files, processes and then deletes them.

I use CreateFile() function to open such files. To ensure that the other program has finished writing to the file and closed it, I set the dwShareMode parameter to 0. If CreateFile fails with a sharing error I just skip the file until the next itteration.

The problem is that DeleteFile() fails with the ERROR_SHARING_VIOLATION error while the file is opened by my program.

I could close the file before deleteing it, but I would like to avoid the possibility of some other program opening the file just before I delete the file.

I use this code to open files

CreateFile(filePath,DELETE|FILE_READ_DATA,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)

Is it possible to achieve what I want: open the file exclusively an then delete it, so that no other program can interfere between openning and deleteting the file.

Pass in FILE_SHARE_DELETE for dwShareMode. Note that this will still allow other applications (as well as your own) to call DeleteFile() while you are reading the file, but according to the documentation of DeleteFile() it won't be deleted until you call CloseHandle() to close your read handle.

The DeleteFile function marks a file for deletion on close. Therefore, the file deletion does not occur until the last handle to the file is closed. Subsequent calls to CreateFile to open the file fail with ERROR_ACCESS_DENIED.

Other applications will not be able to read or write the file as long as you don't specify FILE_SHARE_READ or FILE_SHARE_WRITE respectively. Although with FILE_SHARE_DELETE then can move the file, but that would be it.

what is User32.dll and How it is used in WPF?

2 votes

As Title describes, i am trying to find out what is User32.dll and Where/how it is used in WPF and Windows Forms?

User32.dll is a core windows dll used for windowing and other interactive user program tasks. WPF does not use it much.

Windows.Forms is mostly a .net wrapper around User32, and so is still based entirely on GDI,GDI+, and window handles.

WPF is a newer, and largely different framework that is not nearly so dependent on window messages and window handles (hWnd). It draws on the screen using Direct* and is thus more powerful and often performs better by offloading the graphics rendering to the video card.