I have very strange problem with hiding CheckBoxes in a TreeView. Everything works as it should when TreeView is on the first TabPage in the Window but when TreeView in on the second TabPage then the first TreeNode always has checkbox.
The code I'm using to hide some checkboxes looks like this:
private const int TVIF_STATE = 0x8;
private const int TVIS_STATEIMAGEMASK = 0xF000;
private const int TV_FIRST = 0x1100;
private const int TVM_SETITEM = TV_FIRST + 63;
public struct TVITEM {
public int mask;
public IntPtr hItem;
public int state;
public int stateMask;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpszText;
public int cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public IntPtr lParam;
}
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
private void TurnOff(TreeNode node) {
TVITEM tvi = new TVITEM();
tvi.hItem = node.Handle;
tvi.mask = TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = 0;
IntPtr lparam = Marshal.AllocHGlobal(Marshal.SizeOf(tvi));
Marshal.StructureToPtr(tvi, lparam, false);
SendMessage(this.Handle, TVM_SETITEM, IntPtr.Zero, lparam);
Marshal.FreeHGlobal(lparam);
}
If someone knows solution for this problem, please share.
Working solution can be found here: http://dotnetfollower.com/wordpress/2011/05/winforms-treeview-hide-checkbox-of-treenode/
Related
In my WPF application I am using ShellExecuteEx to show the properties window of a shortcut (.lnk file):
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SHELLEXECUTEINFO
{
public int cbSize;
public uint fMask;
public IntPtr hwnd;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpVerb;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpFile;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpParameters;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpDirectory;
public int nShow;
public IntPtr hInstApp;
public IntPtr lpIDList;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpClass;
public IntPtr hkeyClass;
public uint dwHotKey;
public IntPtr hIcon;
public IntPtr hProcess;
}
private const int SW_SHOW = 5;
private const uint SEE_MASK_INVOKEIDLIST = 12;
public static bool ShowFileProperties(string fileName)
{
SHELLEXECUTEINFO info = new SHELLEXECUTEINFO();
info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
info.lpVerb = "properties";
info.lpFile = fileName;
info.nShow = SW_SHOW;
info.fMask = SEE_MASK_INVOKEIDLIST;
return ShellExecuteEx(ref info);
}
When I compare the properties window shown by my application with the one from the windows context menu, the "Target" field is different:
I suspect it has something to do with the app being x64 but not sure how to fix this.
I am facing issue while creating SSL certificate using crypt api in C#, CertCreateSelfSignCertificate is returning null with error code 87(Incorrect paramter).
Not sure what is missed.
Win32Native.CRYPTOAPI_BLOB SubjectIssuerBlob = new Win32Native.CRYPTOAPI_BLOB();
GCHandle asnNameHandle = GCHandle.Alloc(pbEncoded, GCHandleType.Pinned);
// SubjectIssuerBlob.pbData = Marshal.AllocHGlobal(pbEncoded.Length);
//Marshal.Copy(pbEncoded, 0, SubjectIssuerBlob.pbData, pbEncoded.Length);
SubjectIssuerBlob.pbData = asnNameHandle.AddrOfPinnedObject();
SubjectIssuerBlob.cbData = cbEncoded;
Win32Native.CRYPT_KEY_PROV_INFO providerInfo = new Win32Native.CRYPT_KEY_PROV_INFO();
providerInfo.pwszContainerName = hostname;
providerInfo.pwszProvName = null;
providerInfo.dwProvType = PROV_RSA_FULL;
providerInfo.dwFlags = CRYPT_MACHINE_KEYSET;
providerInfo.cProvParam = 0;
providerInfo.rgProvParam = IntPtr.Zero;
providerInfo.dwKeySpec = AT_SIGNATURE;
Win32Native.CRYPT_ALGORITHM_IDENTIFIER algorithmID = new Win32Native.CRYPT_ALGORITHM_IDENTIFIER();
algorithmID.pszObjId = "1.2.840.113549.1.1.5"; //szOID_RSA_SHA1RSA
pContext = Win32Native.CertCreateSelfSignCertificate(IntPtr.Zero, ref SubjectIssuerBlob, 0, ref providerInfo, ref algorithmID, null, endtime, IntPtr.Zero);
win32api call,
[DllImport("Crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern IntPtr CertCreateSelfSignCertificate(
IntPtr providerHandle,
ref CRYPTOAPI_BLOB subjectIssuerBlob,
uint flags,
ref CRYPT_KEY_PROV_INFO pKeyProvInfo,
ref CRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm,
SystemTime startTime,
SystemTime endTime,
IntPtr extensions);
A test with CertCreateSelfSignCertificate on Windows 10, VS 2015 =>
Dialog box (french) :
//Guid guidContainerName = Guid.NewGuid();
Guid guidContainerName = Guid.Empty;
int nRet = UuidCreate(out guidContainerName);
if (nRet == 0)
{
IntPtr pContainerName = new IntPtr();
nRet = UuidToString(ref guidContainerName, ref pContainerName);
string sContainerName;
sContainerName = Marshal.PtrToStringUni(pContainerName);
string sProviderName = MS_DEF_PROV;
IntPtr hProv = IntPtr.Zero;
bool bRet = CryptAcquireContext(ref hProv, sContainerName, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET);
IntPtr hKey = IntPtr.Zero;
bRet = CryptGenKey(hProv, AT_KEYEXCHANGE, RSA1024BIT_KEY | CRYPT_EXPORTABLE, ref hKey);
string sSubject = "CN=TEST,OU=TESTOU";
byte[] encodedSubjectName = null;
uint nNameLenght = 0;
if (CertStrToName(X509_ASN_ENCODING, sSubject, CERT_X500_NAME_STR, IntPtr.Zero, null, ref nNameLenght, IntPtr.Zero))
{
encodedSubjectName = new byte[nNameLenght];
CertStrToName(X509_ASN_ENCODING, sSubject, CERT_X500_NAME_STR, IntPtr.Zero, encodedSubjectName, ref nNameLenght, IntPtr.Zero);
}
CERT_NAME_BLOB subjectblob = new CERT_NAME_BLOB();
subjectblob.pbData = Marshal.AllocHGlobal(encodedSubjectName.Length);
Marshal.Copy(encodedSubjectName, 0, subjectblob.pbData, encodedSubjectName.Length);
subjectblob.cbData = encodedSubjectName.Length;
CRYPT_KEY_PROV_INFO pKeyProvInfo = new CRYPT_KEY_PROV_INFO();
pKeyProvInfo.pwszContainerName = sContainerName;
pKeyProvInfo.pwszProvName = sProviderName;
pKeyProvInfo.dwProvType = PROV_RSA_FULL;
pKeyProvInfo.rgProvParam = IntPtr.Zero;
pKeyProvInfo.dwKeySpec = AT_KEYEXCHANGE;
IntPtr pcCertContext = CertCreateSelfSignCertificate(hProv, ref subjectblob, 0, ref pKeyProvInfo, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
if (pcCertContext != IntPtr.Zero)
{
X509Certificate cert = new X509Certificate(pcCertContext);
// Add reference to System.Security
X509Certificate2UI.DisplayCertificate(new X509Certificate2(cert));
CertFreeCertificateContext(pcCertContext);
}
else
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
Marshal.FreeHGlobal(subjectblob.pbData);
CryptDestroyKey(hKey);
CryptReleaseContext(hProv, 0);
RpcStringFree(ref pContainerName);
}
with declarations =>
[DllImport("Crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr CertCreateSelfSignCertificate(IntPtr hCryptProvOrNCryptKey, ref CERT_NAME_BLOB pSubjectIssuerBlob, int dwFlags, ref CRYPT_KEY_PROV_INFO pKeyProvInfo,
IntPtr pSignatureAlgorithm, IntPtr pStartTime, IntPtr pEndTime, IntPtr pExtensions);
[DllImport("Crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CertFreeCertificateContext(IntPtr pCertContext);
[StructLayout(LayoutKind.Sequential)]
public struct CERT_NAME_BLOB
{
public int cbData;
public IntPtr pbData;
}
[DllImport("Rpcrt4.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int UuidCreate(out Guid Uuid);
[DllImport("rpcrt4.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int UuidToString(ref Guid uuid, ref IntPtr str);
[DllImport("rpcrt4.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int RpcStringFree(ref IntPtr str);
[DllImport("Advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CryptAcquireContext(ref IntPtr phProv, string szContainer, string szProvider, int dwProvType, uint dwFlags);
[DllImport("Advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CryptReleaseContext(IntPtr hProv, uint dwFlags);
[DllImport("Advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CryptGenKey(IntPtr hProv, uint Algid, int dwFlags, ref IntPtr phKey);
[DllImport("Advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CryptDestroyKey(IntPtr hKey);
public const int RSA1024BIT_KEY = 0x04000000;
public const int CRYPT_EXPORTABLE = 0x00000001;
[DllImport("Crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CertStrToName(uint dwCertEncodingType, string pszX500, uint dwStrType, IntPtr pvReserved,
[In, Out] byte[] pbEncoded, ref uint pcbEncoded, IntPtr ppszError);
public const int CRYPT_ASN_ENCODING = 0x00000001;
public const int CRYPT_NDR_ENCODING = 0x00000002;
public const int X509_ASN_ENCODING = 0x00000001;
public const int X509_NDR_ENCODING = 0x00000002;
public const int PKCS_7_ASN_ENCODING = 0x00010000;
public const int PKCS_7_NDR_ENCODING = 0x00020000;
public const string MS_DEF_PROV = "Microsoft Base Cryptographic Provider v1.0";
public const int PROV_RSA_FULL = 1;
public const uint CRYPT_VERIFYCONTEXT = 0xF0000000;
public const uint CRYPT_NEWKEYSET = 0x00000008;
public const uint CRYPT_DELETEKEYSET = 0x00000010;
public const uint CRYPT_MACHINE_KEYSET = 0x00000020;
public const uint CRYPT_SILENT = 0x00000040;
public const int CERT_SIMPLE_NAME_STR = 1;
public const int CERT_OID_NAME_STR = 2;
public const int CERT_X500_NAME_STR = 3;
public const int CERT_XML_NAME_STR = 4;
public const int CERT_NAME_STR_SEMICOLON_FLAG = 0x40000000;
public const int CERT_NAME_STR_NO_PLUS_FLAG = 0x20000000;
public const int CERT_NAME_STR_NO_QUOTING_FLAG = 0x10000000;
public const int CERT_NAME_STR_CRLF_FLAG = 0x08000000;
public const int CERT_NAME_STR_COMMA_FLAG = 0x04000000;
public const int CERT_NAME_STR_REVERSE_FLAG = 0x02000000;
public const int CERT_NAME_STR_FORWARD_FLAG = 0x01000000;
public const int CERT_NAME_STR_DISABLE_IE4_UTF8_FLAG = 0x00010000;
public const int CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG = 0x00020000;
public const int CERT_NAME_STR_ENABLE_UTF8_UNICODE_FLAG = 0x00040000;
public const int CERT_NAME_STR_FORCE_UTF8_DIR_STR_FLAG = 0x00080000;
public const int CERT_NAME_STR_DISABLE_UTF8_DIR_STR_FLAG = 0x00100000;
public const int CERT_NAME_STR_ENABLE_PUNYCODE_FLAG = 0x00200000;
[StructLayout(LayoutKind.Sequential)]
public struct CRYPT_KEY_PROV_INFO
{
[MarshalAs(UnmanagedType.LPWStr)] public string pwszContainerName;
[MarshalAs(UnmanagedType.LPWStr)] public string pwszProvName;
public int dwProvType;
public int dwFlags;
public int cProvParam;
public IntPtr rgProvParam;
public int dwKeySpec;
}
public const int AT_KEYEXCHANGE = 1;
public const int AT_SIGNATURE = 2;
I would like to know how I can show the list of files in a certain directory as highlighted or selected. I followed this article Programmatically select multiple files in windows explorer
Here's what I tried, But still i am unable to see the files as highlighted:
ViewModel.cs
List<string> ListOfFiles = new List<string>();
private void ShowFolderAndSeeHighlightedFiles()
{
//Open the folder path through NativeMethods class
NativeMethods.OpenFolderAndSelectFiles(sLogFolderPath, ListOfFiles.ToArray());
}
NativeMethods.cs
public static class NativeMethods
{
[DllImport("shell32.dll", ExactSpelling = true)]
public static extern int SHOpenFolderAndSelectItems(
IntPtr pidlFolder,
uint cidl,
[In, MarshalAs(UnmanagedType.LPArray)] IntPtr[] apidl,
uint dwFlags);
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr ILCreateFromPath([MarshalAs(UnmanagedType.LPTStr)] string pszPath);
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214F9-0000-0000-C000-000000000046")]
public interface IShellLinkW
{
[PreserveSig]
int GetPath(StringBuilder pszFile, int cch, [In, Out] ref WIN32_FIND_DATAW pfd, uint fFlags);
[PreserveSig]
int GetIDList([Out] out IntPtr ppidl);
[PreserveSig]
int SetIDList([In] ref IntPtr pidl);
[PreserveSig]
int GetDescription(StringBuilder pszName, int cch);
[PreserveSig]
int SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
[PreserveSig]
int GetWorkingDirectory(StringBuilder pszDir, int cch);
[PreserveSig]
int SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
[PreserveSig]
int GetArguments(StringBuilder pszArgs, int cch);
[PreserveSig]
int SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
[PreserveSig]
int GetHotkey([Out] out ushort pwHotkey);
[PreserveSig]
int SetHotkey(ushort wHotkey);
[PreserveSig]
int GetShowCmd([Out] out int piShowCmd);
[PreserveSig]
int SetShowCmd(int iShowCmd);
[PreserveSig]
int GetIconLocation(StringBuilder pszIconPath, int cch, [Out] out int piIcon);
[PreserveSig]
int SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
[PreserveSig]
int SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, uint dwReserved);
[PreserveSig]
int Resolve(IntPtr hwnd, uint fFlags);
[PreserveSig]
int SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}
[Serializable, StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode), BestFitMapping(false)]
public struct WIN32_FIND_DATAW
{
public uint dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName;
}
public static void OpenFolderAndSelectFiles(string folder, params string[] filesToSelect)
{
IntPtr dir = ILCreateFromPath(folder);
var filesToSelectIntPtrs = new IntPtr[filesToSelect.Length];
for (int i = 0; i < filesToSelect.Length; i++)
{
filesToSelectIntPtrs[i] = ILCreateFromPath(filesToSelect[i]);
}
SHOpenFolderAndSelectItems(dir, (uint)filesToSelect.Length, filesToSelectIntPtrs, 0);
ReleaseComObject(dir);
ReleaseComObject(filesToSelectIntPtrs);
}
private static void ReleaseComObject(params object[] comObjs)
{
foreach (object obj in comObjs)
{
if (obj != null && Marshal.IsComObject(obj))
Marshal.ReleaseComObject(obj);
}
}
}
Thank you so much #Simon Mourier. I followed your suggestion to send the full file paths and replace Marshal.ReleaseComObject with Marshal.FreeCoTaskMem. Problem got solved apparently.
I'm trying to develop an application that can show you the content of directories like the Windows Explorer (with file name and icon). I'm currently using this code:
public class IconManager
{
public static ImageSource GetIcon(string path, bool smallIcon, bool isDirectory)
{
uint flags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES;
if (smallIcon)
flags |= SHGFI_SMALLICON;
uint attributes = FILE_ATTRIBUTE_NORMAL;
if (isDirectory)
attributes |= FILE_ATTRIBUTE_DIRECTORY;
SHFILEINFO shfi;
if (0 != SHGetFileInfo(path, attributes, out shfi, (uint)Marshal.SizeOf(typeof(SHFILEINFO)), flags))
{
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(shfi.hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
return null;
}
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
[DllImport("shell32")]
private static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbFileInfo, uint flags);
private const uint FILE_ATTRIBUTE_READONLY = 0x00000001;
private const uint FILE_ATTRIBUTE_HIDDEN = 0x00000002;
private const uint FILE_ATTRIBUTE_SYSTEM = 0x00000004;
private const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
private const uint FILE_ATTRIBUTE_ARCHIVE = 0x00000020;
private const uint FILE_ATTRIBUTE_DEVICE = 0x00000040;
private const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
private const uint FILE_ATTRIBUTE_TEMPORARY = 0x00000100;
private const uint FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200;
private const uint FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400;
private const uint FILE_ATTRIBUTE_COMPRESSED = 0x00000800;
private const uint FILE_ATTRIBUTE_OFFLINE = 0x00001000;
private const uint FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000;
private const uint FILE_ATTRIBUTE_ENCRYPTED = 0x00004000;
private const uint FILE_ATTRIBUTE_VIRTUAL = 0x00010000;
private const uint SHGFI_ICON = 0x000000100;
private const uint SHGFI_DISPLAYNAME = 0x000000200;
private const uint SHGFI_TYPENAME = 0x000000400;
private const uint SHGFI_ATTRIBUTES = 0x000000800;
private const uint SHGFI_ICONLOCATION = 0x000001000;
private const uint SHGFI_EXETYPE = 0x000002000;
private const uint SHGFI_SYSICONINDEX = 0x000004000;
private const uint SHGFI_LINKOVERLAY = 0x000008000;
private const uint SHGFI_SELECTED = 0x000010000;
private const uint SHGFI_ATTR_SPECIFIED = 0x000020000;
private const uint SHGFI_LARGEICON = 0x000000000;
private const uint SHGFI_SMALLICON = 0x000000001;
private const uint SHGFI_OPENICON = 0x000000002;
private const uint SHGFI_SHELLICONSIZE = 0x000000004;
private const uint SHGFI_PIDL = 0x000000008;
private const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
}
I use this code to get the files and directories with their icons:
foreach (string d in Directory.GetDirectories(Path))
{
string name = "";
foreach (string p in d.Split('\\'))
name = p;
ImageSource icon = IconManager.GetIcon(d + "\\", false, true);
colDesktopItems.Add(new DesktopItem() { ItemName = name, ItemPath = d });
}
foreach (string f in Directory.GetFiles(Path))
{
if (File.GetAttributes(f) != FileAttributes.Hidden && File.GetAttributes(f) != FileAttributes.System)
{
string name = "";
foreach (string p in f.Split('\\'))
if (p.Contains("."))
name = p;
ImageSource icon = IconManager.GetIcon(f, false, false);
if (name != "desktop.ini" && name != "Thumbs.db")
colDesktopItems.Add(new DesktopItem() { ItemName = name.Split('.')[0], ItemPath = f, ItemIcon = icon });
}
}
I also use a DispatcherTimer for automatic refresh (interval = 5 seconds):
private void dpt_Tick(object sender, EventArgs e)
{
if (dPath != null)
{
if (GetContent(dPath).ToString() != diCollection.ToString())
{
diCollection.Clear();
foreach (DesktopItem i in GetContent(dPath))
diCollection.Add(i);
}
}
}
diCollection is the collection that contains the elements to display and dPath is the path of the directory from which the files will be displayed.
But there are two problems:
When I set isDirectory to true, it just returns an empty icon.
After a while, it throws an exception:
The value may not be NULL.
The exception happens in this line:
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(shfi.hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
i use the same methodology. Here is how i have mine set up in my personal library.
public static class ImageUtilities
{
public static System.Drawing.Icon GetRegisteredIcon(string filePath)
{
var shinfo = new SHfileInfo();
Win32.SHGetFileInfo(filePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
return System.Drawing.Icon.FromHandle(shinfo.hIcon);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct SHfileInfo
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
internal sealed class Win32
{
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; // large
public const uint SHGFI_SMALLICON = 0x1; // small
[System.Runtime.InteropServices.DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHfileInfo psfi, uint cbSizeFileInfo, uint uFlags);
}
Usage:
var icon = ImageUtilites.GetRegisteredIcon(string path)
the Extension method i use to make create an ImageSource for WPF:
public static System.Windows.Media.ImageSource ToImageSource(this System.Drawing.Bitmap bitmap, int width, int height)
{
var hBitmap = bitmap.GetHbitmap();
System.Windows.Media.ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
System.Windows.Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromWidthAndHeight(width, height));
if (!DeleteObject(hBitmap))
{
throw new System.ComponentModel.Win32Exception();
}
return wpfBitmap;
}
I forgot to add this, I don't know if you are utilizing it..
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);
I need to implement an hdd partitioning program using c#.
Below is part of my code.
I think it works because it returns true value from DeviceIOcontrol with no errors, but the problem is my program doesn't show anything.
I got a lot of help from Medo's home page, MSDN, and pinvoke site.
public static bool partitionDisk(string path)
{
var signature = new byte[4];
System.Security.Cryptography.RandomNumberGenerator.Create().GetBytes(signature);
using (SafeFileHandle handle = NativeMethods2.CreateFile(path, (NativeMethods2.FILE_SHARE_READ|NativeMethods.GENERIC_READ ) | (NativeMethods.GENERIC_WRITE | NativeMethods2.FILE_SHARE_WRITE), 0, IntPtr.Zero, NativeMethods2.OPEN_EXISTING, 0, IntPtr.Zero))
{
if (handle.IsInvalid) { throw new Win32Exception(); }
var newdi = new NativeMethods2.DRIVE_LAYOUT_INFORMATION_EX();
newdi.PartitionStyle = NativeMethods2.PARTITION_STYLE.PARTITION_STYLE_MBR;
newdi.PartitionCount = 4;
newdi.DriveLayoutInformaiton.Mbr.Signature = BitConverter.ToInt32(signature, 0);
newdi.PartitionEntry = new NativeMethods2.PARTITION_INFORMATION_EX[0x16];
newdi.PartitionEntry[0] = new NativeMethods2.PARTITION_INFORMATION_EX();
newdi.PartitionEntry[0].PartitionStyle = NativeMethods2.PARTITION_STYLE.PARTITION_STYLE_MBR;
newdi.PartitionEntry[0].StartingOffset = 1048576; // check by diskpart
newdi.PartitionEntry[0].PartitionLength = 0xFFFFFFFFFFfffff; //int64 max
newdi.PartitionEntry[0].PartitionNumber = 1;
newdi.PartitionEntry[0].RewritePartition = true;
newdi.PartitionEntry[0].DriveLayoutInformaiton.Mbr.BootIndicator = false;
newdi.PartitionEntry[0].DriveLayoutInformaiton.Mbr.HiddenSectors = 0; //sector size
newdi.PartitionEntry[0].DriveLayoutInformaiton.Mbr.PartitionType = 0x07;// PARTITION_IFS (NTFS partition or logical drive)
newdi.PartitionEntry[0].DriveLayoutInformaiton.Mbr.RecognizedPartition = true;
for (int k = 2; k < newdi.PartitionCount; k++)
{
newdi.PartitionEntry[k] = new NativeMethods2.PARTITION_INFORMATION_EX();
newdi.PartitionEntry[k].DriveLayoutInformaiton.Mbr.BootIndicator = false;
newdi.PartitionEntry[k].DriveLayoutInformaiton.Mbr.HiddenSectors = 0;
newdi.PartitionEntry[k].PartitionLength = 0;
newdi.PartitionEntry[k].PartitionNumber = k;
newdi.PartitionEntry[k].DriveLayoutInformaiton.Mbr.PartitionType = 0;
newdi.PartitionEntry[k].DriveLayoutInformaiton.Mbr.RecognizedPartition = false;
newdi.PartitionEntry[k].RewritePartition = true;
newdi.PartitionEntry[k].StartingOffset = 0;
}
Int32 bytesOut = 0;
if (NativeMethods2.DeviceIoControl(handle, NativeMethods2.IOCTL_DISK_SET_DRIVE_LAYOUT, ref newdi, Marshal.SizeOf(newdi), IntPtr.Zero, 0, ref bytesOut, IntPtr.Zero) == false) { throw new Win32Exception(); }
}
}
private static class NativeMethods2
{
public const int GENERIC_READ = -2147483648;
public const int GENERIC_WRITE = 1073741824;
public const int OPEN_EXISTING = 3;
public const int FILE_SHARE_READ = 0x0000000001;
public const int FILE_SHARE_WRITE = 0x0000000002;
public const int IOCTL_DISK_UPDATE_PROPERTIES = 0x70140;
public const int IOCTL_DISK_SET_DRIVE_LAYOUT_EX = 0x7C054;
public enum PARTITION_STYLE
{
PARTITION_STYLE_MBR = 0,
PARTITION_STYLE_GPT = 1,
PARTITION_STYLE_RAW = 2,
}
[StructLayout(LayoutKind.Sequential)]
public struct DRIVE_LAYOUT_INFORMATION_EX
{
public PARTITION_STYLE PartitionStyle;
public int PartitionCount;
public DRIVE_LAYOUT_INFORMATION_UNION DriveLayoutInformaiton;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 0x16)]
public PARTITION_INFORMATION_EX[] PartitionEntry;
}
[StructLayout(LayoutKind.Sequential)]
public struct PARTITION_INFORMATION_EX
{
public PARTITION_STYLE PartitionStyle;
public long StartingOffset;
public long PartitionLength;
public int PartitionNumber;
public bool RewritePartition;
public PARTITION_INFORMATION_UNION DriveLayoutInformaiton;
}
[StructLayout(LayoutKind.Sequential)]
public struct PARTITION_INFORMATION_MBR
{
public byte PartitionType;
public bool BootIndicator;
public bool RecognizedPartition;
public Int32 HiddenSectors;
}
[StructLayout(LayoutKind.Sequential)]
public struct PARTITION_INFORMATION_GPT
{
public Guid PartitionType; //GUID
public Guid PartitionId; //GUID
public Int64 Attributes;
public char[] Name;
}
[StructLayout(LayoutKind.Sequential)]
public struct PARTITION_INFORMATION
{
public long StartingOffset;
public long PartitionLength;
public int HiddenSectors;
public int PartitionNumber;
public byte PartitionType;
[MarshalAs(UnmanagedType.I1)]
public bool BootIndicator;
[MarshalAs(UnmanagedType.I1)]
public bool RecognizedPartition;
[MarshalAs(UnmanagedType.I1)]
public bool RewritePartition;
}
[StructLayout(LayoutKind.Explicit)]
public struct DRIVE_LAYOUT_INFORMATION_UNION
{
[FieldOffset(0)]
public DRIVE_LAYOUT_INFORMATION_MBR Mbr;
[FieldOffset(0)]
public DRIVE_LAYOUT_INFORMATION_GPT Gpt;
}
[StructLayout(LayoutKind.Sequential)]
public struct DRIVE_LAYOUT_INFORMATION_MBR
{
public Int32 Signature;
}
[StructLayout(LayoutKind.Sequential)]
public struct DRIVE_LAYOUT_INFORMATION_GPT
{
public Guid DiskId;
public Int64 StartingUsableOffset;
public Int64 UsableLength;
public ulong MaxPartitionCount;
}
[StructLayout(LayoutKind.Explicit)]
public struct PARTITION_INFORMATION_UNION
{
[FieldOffset(0)]
public PARTITION_INFORMATION_MBR Mbr;
[FieldOffset(0)]
public PARTITION_INFORMATION_GPT Gpt;
}
[DllImportAttribute("kernel32.dll", EntryPoint = "CreateFileW", SetLastError = true)]
public static extern SafeFileHandle CreateFile([MarshalAsAttribute(UnmanagedType.LPWStr)] string lpFileName, Int32 dwDesiredAccess, Int32 dwShareMode, IntPtr lpSecurityAttributes, Int32 dwCreationDisposition, Int32 dwFlagsAndAttributes, IntPtr hTemplateFile);
[DllImportAttribute("kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
[return: MarshalAsAttribute(UnmanagedType.Bool)]
public static extern Boolean DeviceIoControl(SafeFileHandle hDevice, Int32 dwIoControlCode, ref DRIVE_LAYOUT_INFORMATION_EX lpInBuffer, int nInBufferSize, IntPtr lpOutBuffer, Int32 nOutBufferSize, ref Int32 lpBytesReturned, IntPtr lpOverlapped);
}
private static class NativeMethods
{
public const int GENERIC_READ = -2147483648;
public const int GENERIC_WRITE = 1073741824;
public const int OPEN_EXISTING = 3;
public const int IOCTL_DISK_CREATE_DISK = 0x7C058;
public enum PARTITION_STYLE
{
PARTITION_STYLE_MBR = 0,
PARTITION_STYLE_GPT = 1,
PARTITION_STYLE_RAW = 2,
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct CREATE_DISK
{
public PARTITION_STYLE PartitionStyle;
public CREATE_DISK_UNION_MBR_GPT MbrGpt;
}
[StructLayoutAttribute(LayoutKind.Explicit)]
public struct CREATE_DISK_UNION_MBR_GPT
{
[FieldOffset(0)]
public CREATE_DISK_MBR Mbr;
[FieldOffset(0)]
public CREATE_DISK_GPT Gpt;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct CREATE_DISK_MBR
{
public Int32 Signature;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct CREATE_DISK_GPT
{
public Guid DiskId;
public Int32 MaxPartitionCount;
}
[DllImportAttribute("kernel32.dll", EntryPoint = "CreateFileW", SetLastError = true)]
public static extern SafeFileHandle CreateFile([MarshalAsAttribute(UnmanagedType.LPWStr)] string lpFileName, Int32 dwDesiredAccess, Int32 dwShareMode, IntPtr lpSecurityAttributes, Int32 dwCreationDisposition, Int32 dwFlagsAndAttributes, IntPtr hTemplateFile);
[DllImportAttribute("kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
[return: MarshalAsAttribute(UnmanagedType.Bool)]
public static extern Boolean DeviceIoControl(SafeFileHandle hDevice, Int32 dwIoControlCode, ref CREATE_DISK lpInBuffer, int nInBufferSize, IntPtr lpOutBuffer, Int32 nOutBufferSize, ref Int32 lpBytesReturned, IntPtr lpOverlapped);
}
...and to call the function:
DiskIO.partitionDisk("//./PHYSICALDRIVE2")