Trouble enumerating/selecting a server on the LAN

  • Thread starter Thread starter Ken Allen
  • Start date Start date
K

Ken Allen

I am relatively new to Windows API programming, and even more new to .Net
programming. I like most of what I see, but there are still some parts that
Microsoft seems to have gone out of their way to make as complicated as
possible.

All I want to do is present a list of the servers on the LAN that are
running a copy of my .Net service and permit the user to select one of those
servers. THis basically breaks down into two parts: generating/displaying
the list of servers on the network; and determining which of those servers
is running my service (there may be zero or more, even all of them).

Some friends came across the following code that is an excerpt from some
older C++ code (that is still working) that is used to display a dialog that
permits the user to select a specific server on the LAN, regardless of the
domains. This uses a supplied Microsoft dialog from the Shell32.DLL
component to display the sub-tree of the standard file explorer.
Unfortunately, neither they nor I are clear on how this could be converted
in C# (one of our goals is to avoid unmanaged code if at all possible,
although unsafe code is considered acceptable; and we want all code written
in C# if at all possible). Can someone suggest how this could be translated
to C#? Some of this involves COM objects (pFolder in this code), and I have
barely started to figure out how to deal with this level of communication.

//-------------------------------------
BROWSEINFO browseInfo = {0};
LPITEMIDLIST pidlRoot = NULL;
LPITEMIDLIST pidlSelected= NULL;
LPMALLOC pMalloc = NULL;
SHGetMalloc(&pMalloc);
CComPtr<IShellFolder> pFolder;
SHGetDesktopFolder(&pFolder);
pFolder->ParseDisplayName(NULL, NULL,
L"::{208D2C60-3AEA-1069-A2D7-08002B30309D}\\EntireNetwork", NULL, &pidlRoot,
NULL);
browseInfo.hwndOwner = m_hWnd;
browseInfo.pidlRoot = pidlRoot;
browseInfo.pszDisplayName = ComputerName;
browseInfo.lpszTitle = _T("Please choose the server to be connected");
browseInfo.ulFlags = BIF_BROWSEFORCOMPUTER | BIF_EDITBOX | BIF_VALIDATE;
browseInfo.lpfn = BrowseCallbackProc;
browseInfo.lParam = 0;
if(pidlRoot)
pidlSelected = SHBrowseForFolder(&browseInfo);
if(!pidlSelected) ////user selected Cancel in Browse Dialog
ComputerName[0] = 0;
if(pidlRoot)
pMalloc->Free(pidlRoot);
pMalloc->Release();
//-------------------------------------

Of course, I would prefer to simply retrieve a list of all servers on the
LAN, possibly prefixed by the domain since we run more than one domain, and
then permit the user to select from my list, but I cannot fund any other
method for enumerating the servers on the LAN. If I could do this, then the
next step would be to determine whether my service is running on each of
those systems, thus permitting me to present a list to the user that
contains only those systems that are appropriate. This would really be a
nice touch, and I am willing to live with the selection of a server system
and then let the user know whether it was a bad selection or not.

-ken
 
Ken,

The SHBrowseForFolder API is wrapped in the managed
FolderBrowserDialog component.

But it only exposes a subset of the SHBrowseForFolder features. I
don't think can get it to browse for a computer
(BIF_BROWSEFORCOMPUTER) or set the root node to Entire Network without
at least calling some unmanaged code.



Mattias
 
So close, and yet....

There are references in the MSDN to something called the FolderNameEditor,
which seems to contain the definitions of the type I am seeking, but it is
marked as not being for use from client code. I am not certain what this is
used for or how to get access to it, thus I am left attempting to figure out
how to map the original code I posted.

-Ken
 
Back
Top