Home > Software engineering >  C# .NET FW v4.5.2: How to open *.msc property dialogues for particular objects?
C# .NET FW v4.5.2: How to open *.msc property dialogues for particular objects?

Time:03-28

I am searching for the possibility to open default property dialogues for particular Windows objects, like:

  • the property dialogue for a particular service in services.msc
  • the property dialogue for a particular scheduled taks in taskschd.msc
  • etc.

I do not want to interact with that dialogues or change any of the properties. I just want to open them to give the user direct access to a single items properties (instead of opening the listings (by calling the *.msc executables) in which the user has to search the object again).

I have already partially copied the dialogues functions into own forms and code for other purposes, by the way, but I want to give the user the option to open the default ones and make any changes directly.

Now, I have found some hints but I am stuck as there is always some crucial information missing:

1. Using so-called SnapIns of MMC (Microsoft Management Console)

There is this relatively new answer which uses VB code but I have no clue how I could use the MMC Automation Object Model in C# .NET Framework.

Furthermore, there is no clean and easy example/explanation of how to simply call an existing .msc process/list/window by usage of the Microsoft.ManagementConsole. Instead, there are several horrifying complex tutorials how to implement SnapIns into C#.

To be clear here: What I want to do is to reference a dll, go through some list (if necessary) and just call the properties dialogue.

2. COM invoke of old API

There is this old answer where someone recommends using invoke on an outdated ITaskScheduler class which does not solve the general dialogue call but at least the one for scheduled tasks. Perhaps it is also possible to use something similar for services, etc. - but, again, there is no clear example or explanation of how to implement this approach.

CodePudding user response:

Here's documentation on how to use the MMC SDK. It's a Win32 API, so you'll have to use COM, PInvoke, or other interop to use it.

https://docs.microsoft.com/en-us/previous-versions/windows/desktop/mmc/microsoft-management-console-start-page#developer-audience

The C examples are probably more informative than the VB ones. The .h files are part of the windows sdk so you should be able to find the clsid and other constants that you need in there: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/mmc/using-c-with-the-mmc-2-0-automation-object-model

CodePudding user response:

It's relatively simple.

For example:

Note: setting mmcApp.UserControl = 1; leaves the Console open, otherwise it would close.

using MMC20; 
// [...]

MMC20.Application mmcApp = new MMC20.Application();
mmcApp.UserControl = 1;
mmcApp.Load("services.msc");

var doc = mmcApp.Document;
var view = doc.ActiveView;
var node = view.ListItems.OfType<Node>().FirstOrDefault(n => n.Name == "Base Filtering Engine");

view.Select(node);
view.DisplaySelectionPropertySheet();

To enumerate the ListItems, use a standard loop or an extension method as shown above:

var nodes = objView.ListItems;

foreach (MMC20.Node node in nodes) {
    Console.WriteLine(node);
}
  • Related