Get information about the currently extended GPO - c#

We are currently developing an MMC SnapIn Extension, that extends multiple group policy objects (GPOs), like you can see it in that image below:
Group Policy Management Editor - Extended
Thecnology is MMC 3.0, .NET 3.5, C#, VS2013. The extension is available on all GPOs.
The main class of the extension looks like this (in an anonimized way)
[SnapInSettings("{3B6F64DC-4572-4A64-957A-E8A9E2FEBD54}", DisplayName = "DisplayName", Description = "MMC Snap-In")]
[ExtendsNodeType("{8FC0B739-A0E1-11D1-A7D3-0000F87571E3}")]
[ExtendsNodeType("{D37CB93E-0DDC-4204-AA04-C3D70B01A7D8}")]
public class ExtensionSnapIn : NamespaceExtension
{
}
Everything works fine except I cannot determine which GPO is the parent of the currently opened extension. I would like to know this because I have to write into the registry to the following path: HKLM\Software\Company\Product\Client Group Policy\[GPOID]\
I would like to have this ID dynamically: I'm looking look for the highlighted GUID
I have tried a lot of things with no result at all:
In the ExtensionSnapIn class there is an inherited PrimaryScopeNode property which has a NodeType property which is a GUID. Unfortunately, this property always returns 00000000-000-0000-0000-000000000000. However the documentation says that about the PublishesNodeTypeAttribute:
If a node does not have this attribute, it cannot be registered as an
extensible node.
Whenever I'm trying to read from the PrimaryNode.SharedData like this:
Sample:
//private const string shdata = "CCF_SCE_GPT_UNKNOWN";
//private const string shdata = "CCF_SNAPIN_CLASSID";
//private const string shdata = "CCF_DISPLAY_NAME";
//private const string shdata = "CCF_NODETYPE";
//private const string shdata = "CCF_SZNODETYPE";
//private const string shdata = "CCF_SNAPIN_CLASSID";
//private const string shdata = "CCF_DISPLAY_NAME";
//private const string shdata = "CFSTR_DSOBJECTNAMES";
private const string shdata = "CCF_SCE_GPT_UNKNOWN";
public ExtensionSnapIn()
{
this.PrimaryNode.SharedData.Add(new SharedDataItem(shdata));
}
protected override void OnInitialize()
{
SharedDataItem sharedDataItem = this.PrimaryNode.SharedData.GetItem(shdata);
}
I'm always getting an exception. For CCF_NODETYPE, CCF_SZNODETYPE, CCF_SNAPIN_CLASSID, CCF_DISPLAY_NAME I'm getting this:
The following clipboard format is reserved for use by MMC:
CCF_DISPLAY_NAME. Specify another clipboard format.
The rest of the clipboard formats just give a PrimarySnapInDataException with this stack trace:
at Microsoft.ManagementConsole.SharedDataItem.GetData() at
Balabit.SyslogNgAgent.MMC.ExtensionSnapIn.OnInitialize() in
c:\Source\Bergholz\Client\Client.Product\Main\src\MMC\ExtensionSnapIn.cs:line
65 at
Microsoft.ManagementConsole.Advanced.NamespaceExtension.ProcessNotification(Notification
notification) at
Microsoft.ManagementConsole.Internal.SnapInClient.Microsoft.ManagementConsole.Internal.IMessageClient.ProcessNotification(Notification
notification) at
Microsoft.ManagementConsole.Executive.SnapInInitializationOperation.OnStart()
at
Microsoft.ManagementConsole.Executive.RunningOperationsTable.EnqueueOperation(Operation
operation) at
Microsoft.ManagementConsole.Executive.NamespaceExtensionComponentData.GetScopeNodeForExpand(IDataObject
dataObject, IntPtr hScopeItem) at
Microsoft.ManagementConsole.Executive.ComponentData.OnExpand(IDataObject
dataObject, Boolean isExpanding, IntPtr hScopeItem) at
Microsoft.ManagementConsole.Executive.ExpandMmcNotification.OnNotify(IntPtr
dataObject, IntPtr arg, IntPtr param) at
Microsoft.ManagementConsole.Executive.MmcNotifyTarget.Notify(IntPtr
dataObject, NotificationType eventType, IntPtr arg, IntPtr param)
I have googled this problem for days, and I have read tons of articles about MMC Extension SnapIns, and GPO, but I have not yet found any usable solutions for my problem.
Thanks for your help in advance.

Related

C# Path not recognized or utilized when invoking File.AppendAllText

I’m trying to use the File class to work with a text file in a console and winforms desktop app and getting the following exception:
The type initializer for '_Library.Logging' threw an exception
From what I’ve read here this error is typically caused by a problem in App.config for Winfoms apps but the Exception details seem to point elsewhere:
System.ArgumentNullException: Value cannot be null. Parameter name: path
at System.IO.File.AppendAllText(String path, String contents)
The MSDN examples for file manipulation all hard code the path parameter without any reference to using an App.confiig file so my presumption was it is possible to do this without involving ConfigurationManager.
This is the code I'm trying to use
// in calling method
class Program_Console
{
private static StringBuilder SB4log = new StringBuilder();
public static void Main(string[] tsArgs)
{
// Conditionals dealing with argumentts from Task Scheduler
Save2Log("Calling _UI.exe via \"Process.Start()\"", true);
// try-catch-finally for Process.Start
}
private static void Save2Log(string msgTxt, bool noTS)
{
SB4log.AppendLine($"{msgTxt}");
if (noTS) Logging.SaveLog(SB4log);
else Logging.SaveLog_TimeStamp(SB4log);
SB4log.Clear();
}
}
// saving app progression messages to a single log txt file
public static class Logging
{
private static String filePath = Connections.LogPath();
private static StringBuilder SB4log = new StringBuilder();
public static void SaveLog(StringBuilder logTxt)
{
File.AppendAllText(filePath, logTxt.ToString());
logTxt.Clear();
}
}
// class for DB connection and file paths
public static class Connections
{
private static StringBuilder SB4log = new StringBuilder();
public static string AppPath()
{
string appRoot;
try
{
string appDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
SaveLog($"->App Path: {appDir}", true); // culprit
int loc = appDir.IndexOf("BURS");
appRoot = appDir.Substring(0, loc + 5);
}
catch (Exception ex)
{
// Exception response and MessageBox
}
return appRoot;
}
public static string ConsolePath() {}
public static string UIPath() {}
public static string LogPath()
{
string appRoot = AppPath();
string wrkDir = #"_Library\Data\BURS_Log.Txt";
string fullDir = $"{appRoot}{wrkDir}";
SaveLog($"->Log Path: {fullDir}\n", true); // culprit
return fullDir;
}
}
In stepping through the code the code the variable containing the path -- filePath -- has the expected value: "D:\BURS_Library\Data\BURS_Log.Txt" (quotes used to show there re no unintended spaces needing to be trimmed). Acordinng to MSDN if it's a malformed path an exception will be thrown but the path looks valid to me.
Why isn’t the Path variable utilized?
Any help will be appreciated.
Edit: expanded code to show start-to-finish flow since the original abridged version seemed to be confusing. Have added the text "//culprit" to the two lines which caused the error as pointed out by the responders.
It's not clear what Connections is, but given Connections.LogPath(); it seems that you're calling LogPath(); to set the value for filePath which is a problem because that calls AppPath which has the following statement SaveLog($"->App Path: {appDir}", true);.
You haven't included a version of SaveLog that has 2 parameters, but assuming it's similar to the one you've posted, you're attempting to use filePath when the value hasn't been set yet - which causes an issue.

Dot net core C# open photo viewer with Process Class

I am going to open the photo viewer using .net core and this is my code
using System.Diagnostics;
namespace TestProcessForOpenPhoto
{
class Program
{
static void Main(string[] args)
{
var photoViewer = new Process();
photoViewer.StartInfo.FileName = #"C:\Program Files\Windows Photo Viewer\PhotoViewer.dll";
photoViewer.StartInfo.Arguments = #" C:\Users\XXXXX\Desktop\TestImage\abc.jpg";
photoViewer.StartInfo.UseShellExecute = false;
photoViewer.Start();
}
}
}
and I got this error message
System.ComponentModel.Win32Exception: 'The specified executable is not a valid application for this OS platform.'
Can anyone help me to fix this bug, thanks
After researching this I noticed folks using rundll32.exe to execute an export from PhotoViewer.dll to display a picture using Microsoft Photo Viewer application. So I think that's what OP was trying to do, they just forgot to use the rundll32.exe application.
So I thought I'd take a crack at this and not use the rundll32.exe and just call the export directly. I debugged it with x86dbg and saw that it's passing in 4 parameters: pointer, pointer, pointer (to wchar_t*), int. I don't know what the parameters do, so I just set them to NULL and made sure to pass in the path to the picture as the 3rd and it seems to work.
So this will do what you want it to do. I know that hard-coding system paths is bad practice, but maybe someone who has more time can make this more dynamic.
private static class WindowsPhotoViewer
{
private const string FilePath32 = #"c:\program files (x86)\Windows Photo Viewer\PhotoViewer.dll";
private const string FilePath64 = #"c:\program files\Windows Photo Viewer\PhotoViewer.dll";
[DllImport(FilePath32, CharSet = CharSet.Unicode, EntryPoint = "ImageView_FullscreenW")]
private static extern void ImageView_Fullscreen32(
IntPtr unknown1, IntPtr unknown2, string path, int unknown3);
[DllImport(FilePath64, CharSet = CharSet.Unicode, EntryPoint = "ImageView_FullscreenW")]
private static extern void ImageView_Fullscreen64(
IntPtr unknown1, IntPtr unknown2, string path, int unknown3);
public static bool ShowImage(FileInfo imageFile)
{
if ((IntPtr.Size == 8) && File.Exists(FilePath64) && imageFile.Exists)
{
ImageView_Fullscreen64(IntPtr.Zero, IntPtr.Zero, imageFile.FullName, 0);
return true;
}
else if ((IntPtr.Size == 4) && File.Exists(FilePath32) && imageFile.Exists)
{
ImageView_Fullscreen32(IntPtr.Zero, IntPtr.Zero, imageFile.FullName, 0);
return true;
}
return false;
}
}
Then you can call it as so:
if(!WindowsPhotoViewer.ShowImage(new FileInfo(#"c:\users\andy\desktop\test.jpg")))
{
Console.WriteLine("Failed to show image");
}

MethodAccessException: Attempt to access the method failed. (Windows phone 7)

I am working on a tool which reads the contents of an INI file. I am using kernel32 dll import in it. However, when I run the application on actual device, I get an exception like this, "MethodAccessException: Attempt to access the method failed." The device I am using is a Samsung Omnia (Windows Phone 7.1).
Apart from this in another app I am using core dll import and I am getting the same exception. How can this exception be removed?
public class IniFile
{
public string path;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public IniFile(string INIPath)
{
path = INIPath;
}
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
return temp.ToString();
}
}
Minimum supported client | Windows 2000 Professional [desktop apps only]
WritePrivateProfileString function (Windows)
Also check this links:
Win32 and COM for Windows Store apps (system) (Windows)
WritePrivateProfileString() Does Not Work If You Do Not Have Delete Access on the Folder

Obtaining path of users directory

How to get path of Users folder from windows service on MS Vista?
I think about path of C:\Users directory, but it may be different location depend on system localization.
Take a look at the Environment.SpecialFolder Enumeration, e.g.
Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory);
Adjust for the special folder you want. However, in reading another post found here, it looks like you may need to do a little manipulation of the string if you want exactly c:\users instead of c:\users\public, for example.
System.Environment.SpecialFolder will give you access to all these folders that you want, such as My Documents, Etc..
If you use the UserProfile SpecialFolder, that should give you the path to your profile under Users.
string userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
The best way as #Neil pointed out is to use SHGetKnownFolderPath() with the FOLDERID_UserProfiles. However, c# doesn't have that. But, it's not that hard to invoke it:
using System;
using System.Runtime.InteropServices;
namespace SOExample
{
public class Main
{
[DllImport("shell32.dll")]
static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr pszPath);
private static string getUserProfilesPath()
{
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd378457(v=vs.85).aspx#folderid_userprofiles
Guid UserProfilesGuid = new Guid("0762D272-C50A-4BB0-A382-697DCD729B80");
IntPtr pPath;
SHGetKnownFolderPath(UserProfilesGuid, 0, IntPtr.Zero, out pPath);
string path = System.Runtime.InteropServices.Marshal.PtrToStringUni(pPath);
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pPath);
return path;
}
static void Main(string[] args)
{
string path = getUserProfilesPath(); // C:\Users
}
}
}
I can't see that function exposed to .NET, but in C(++) it would be
SHGetKnownFolderPath(FOLDERID_UserProfiles, ...)
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)

ExtractAssociatedIcon returns null

I'm using the ExtractAssociatedIcon method to retrieve the icon for the file. My hope is to retrieve the same icon that a user would see in their explorer window.
public static Icon GetIcon(string fileName)
{
try
{
Icon icon = Icon.ExtractAssociatedIcon(fileName);
return icon;
}
catch
{
return null;
}
}
This works 99% of the time. However, if the user has linked to a file on a shared path, such as \\SOME_SERVER\my documents\this file.pdf it returns null. It falls through the "catch" with the error that the file path is not a valid path.
It is a valid URI (I've verified the file exists, is readable, etc.), but not a valid fully-qualified drive path with the X:\some\folder notation.
How can I get around this, if at all?
Thanks.
Re-UPDATE
Here's the solution I ended up with. It's much cleaner than the first update. Many thanks to Chris Haas, whose answer was a comment, and not a direct answer. If/when he makes it a direct answer, I will update this as such.
I still had to go down to a lower level and fetch the icon through C++ libraries, but the only library I needed is listed below:
#region Old-School method
[DllImport("shell32.dll")]
static extern IntPtr ExtractAssociatedIcon(IntPtr hInst,
StringBuilder lpIconPath, out ushort lpiIcon);
public static Icon GetIconOldSchool(string fileName)
{
ushort uicon;
StringBuilder strB = new StringBuilder(fileName);
IntPtr handle = ExtractAssociatedIcon(IntPtr.Zero, strB, out uicon);
Icon ico = Icon.FromHandle(handle);
return ico;
}
#endregion
Once I had defined the above method, the GetIcon() method becomes:
public static Icon GetIcon(string fileName)
{
try
{
Icon icon = Icon.ExtractAssociatedIcon(fileName);
return icon;
}
catch
{
try
{
Icon icon2 = GetIconOldSchool(fileName);
return icon2;
}
catch
{
return null;
}
}
}
(Comment turned into post - CTIP)
Check out the link here which eventually leads to P/Invoke.net with the following code:
[DllImport("shell32.dll")]
static extern IntPtr ExtractAssociatedIcon(IntPtr hInst, StringBuilder lpIconPath, out ushort lpiIcon);
[DllImport("shell32.dll")]
static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex);
_
ushort uicon;
StringBuilder strB = new StringBuilder(YOUR_FILE_PATH);
IntPtr handle = ExtractAssociatedIcon(this.Handle, strB, out uicon);
Icon ico = Icon.FromHandle(handle);
return ico.ToBitmap();

Categories