Search code examples
c#.netshellwinapiwindows-shell

How to open "Enter Network Credentials" in Windows 10


I'm using the Windows Shell API to access Network machines and their folders.

shellFolder.EnumObjects(hwnd, grfFlags, out enumIdList);

....

int result = shellEnum.Next(celt, out itemPidl, out numFetched);

I'm using the IShellFolder.EnumObjects() method, which works great and I can get a list of machines in my Network.

However, when I try to get folders from these networks, it works only when there is no need of authentication to the machine. If there is a need of authentication, the method returns 0x80004005 (E_FAIL).

Windows File Explorer shows a prompt to "Enter Network Credentials" for these machines. How can I show the same prompt from my WPF program and continue based on user's credentials?


Solution

  • In most of the Shell API, a HWND handle can be passed, but it's optional, and it's also a way to say "disable UI".

    This is the case for the IShellFolder::EnumObjects method

    HRESULT EnumObjects(HWND hwnd, SHCONTF grfFlags, IEnumIDList **ppenumIDList);
    

    hwnd parameter official documentation:

    If user input is required to perform the enumeration, this window handle should be used by the enumeration object as the parent window to take user input. An example would be a dialog box to ask for a password or prompt the user to insert a CD or floppy disk. If hwndOwner is set to NULL, the enumerator should not post any messages, and if user input is required, it should silently fail.