如果您查看原始签名,该函数需要一个IntPtr
句柄、一个Guid
ID 和一个PropertyStore
将由数据填充的对象。
HRESULT SHGetPropertyStoreForWindow(
_In_ HWND hwnd,
_In_ REFIID riid,
_Out_ void **ppv
);
将其翻译成 c# 如下所示:
[DllImport("shell32.dll", SetLastError = true)]
static extern int SHGetPropertyStoreForWindow(
IntPtr handle,
ref Guid riid,
out IPropertyStore propertyStore);
您可以IPropertyStore
从PInvoke.net获取界面:
[ComImport, Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IPropertyStore
{
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void GetCount([Out] out uint cProps);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void GetAt([In] uint iProp, out PropertyKey pkey);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void GetValue([In] ref PropertyKey key, out object pv);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetValue([In] ref PropertyKey key, [In] ref object pv);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void Commit();
}
唯一剩下的就是实际实现PropertyStore
. .net 框架中的类似实现可以在例如PrintSystemObject中找到。
实现后,您应该能够简单地调用该方法并设置属性:
IPropertyStore store = new PropertyStore();
//your propery id in guid
var g = new Guid("32CE38B2-2C9A-41B1-9BC5-B3784394AA44");
SHGetPropertyStoreForWindow(this.Handle, ref g, out store);