Skip to content

Commit

Permalink
Merge pull request #1382 from dmitrya1979/master
Browse files Browse the repository at this point in the history
Changed type of property ToastActivatorCLSID in shortcut to VT_CLSID
  • Loading branch information
anaisbetts committed Nov 15, 2018
2 parents 3e70309 + cef09f6 commit 783565b
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions src/Squirrel/ShellFile.cs
Expand Up @@ -69,6 +69,44 @@ public static PropVariant FromString(string str)

return pv;
}

public static PropVariant FromGuid(Guid guid)
{
byte[] bytes = guid.ToByteArray();
var pv = new PropVariant() {
variantType = 72, // VT_CLSID
pointerValue = Marshal.AllocCoTaskMem(bytes.Length),
};
Marshal.Copy(bytes, 0, pv.pointerValue, bytes.Length);

return pv;
}

/// <summary>
/// Called to properly clean up the memory referenced by a PropVariant instance.
/// </summary>
[DllImport("ole32.dll")]
private extern static int PropVariantClear(ref PropVariant pvar);

/// <summary>
/// Called to clear the PropVariant's referenced and local memory.
/// </summary>
/// <remarks>
/// You must call Clear to avoid memory leaks.
/// </remarks>
public void Clear()
{
// Can't pass "this" by ref, so make a copy to call PropVariantClear with
PropVariant tmp = this;
PropVariantClear(ref tmp);

// Since we couldn't pass "this" by ref, we need to clear the member fields manually
// NOTE: PropVariantClear already freed heap data for us, so we are just setting
// our references to null.
variantType = (short)VarEnum.VT_EMPTY;
Reserved1 = Reserved2 = Reserved3 = 0;
pointerValue = IntPtr.Zero;
}
}

[StructLayout(LayoutKind.Sequential)]
Expand Down Expand Up @@ -885,11 +923,30 @@ public void SetAppUserModelId(string appId)
/// Sets the ToastActivatorCLSID
/// </summary>
public void SetToastActivatorCLSID(string clsid)
{
var guid = Guid.Parse(clsid);
SetToastActivatorCLSID(guid);
}

/// <summary>
/// Sets the ToastActivatorCLSID
/// </summary>
public void SetToastActivatorCLSID(Guid clsid)
{
var propStore = (IPropertyStore)linkW;

var pkey = PROPERTYKEY.PKEY_AppUserModel_ToastActivatorCLSID;
var str = PropVariant.FromString (clsid);
propStore.SetValue(ref pkey, ref str);

var varGuid = PropVariant.FromGuid(clsid);
try {
int errCode = propStore.SetValue(ref pkey, ref varGuid);
Marshal.ThrowExceptionForHR(errCode);

errCode = propStore.Commit();
Marshal.ThrowExceptionForHR(errCode);
} finally {
varGuid.Clear();
}
}

/// <summary>
Expand Down

0 comments on commit 783565b

Please sign in to comment.