From 86ca5a388b5f8786b585037c95832bcf65e22e97 Mon Sep 17 00:00:00 2001
From: didroe <didroe@0a9a9e18-0b8b-11df-b730-f17d7b42d8b9>
Date: Wed, 27 Jan 2010 21:38:14 +0000
Subject: [PATCH] Initial commit

---
 Win7AppId.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)
 create mode 100755 Win7AppId.cpp

diff --git a/Win7AppId.cpp b/Win7AppId.cpp
new file mode 100755
index 0000000..9f8e38b
--- /dev/null
+++ b/Win7AppId.cpp
@@ -0,0 +1,88 @@
+// Win7AppId.cpp : Defines the entry point for the console application.
+//
+
+#include "stdafx.h"
+
+#include <windows.h>
+#include <shobjidl.h>
+#include <propkey.h>
+
+#include <iostream>
+
+using namespace std;
+
+EXTERN_C const PROPERTYKEY DECLSPEC_SELECTANY PKEY_AppUserModel_ID =
+ 	  	     { { 0x9F4C2855, 0x9F79, 0x4B39,
+	  	     { 0xA8, 0xD0, 0xE1, 0xD4, 0x2D, 0xE1, 0xD5, 0xF3, } }, 5 };
+
+void die(string message) {
+	cout << "Error: " << message.c_str() << endl;
+	exit(1);
+}
+
+void doOrDie(HRESULT hr, string message) {
+	if (!SUCCEEDED(hr)) {
+		die(message);
+	}
+}
+
+int _tmain(int argc, _TCHAR* argv[])
+{
+	doOrDie(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED),
+			"Failed to initialise COM");
+
+	IShellLink *link;
+	doOrDie(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&link)),
+			"Failed to create ShellLink object");
+	
+	IPersistFile* file; 
+    doOrDie(link->QueryInterface(IID_PPV_ARGS(&file)),
+			"Failed to obtain PersistFile interface"); 
+
+	doOrDie(file->Load(argv[1], STGM_READWRITE),
+		    "Failed to load shortcut file");
+
+	IPropertyStore* store; 
+    doOrDie(link->QueryInterface(IID_PPV_ARGS(&store)),
+			"Failed to obtain PropertyStore interface"); 
+
+	PROPVARIANT pv;
+	doOrDie(store->GetValue(PKEY_AppUserModel_ID, &pv),
+		    "Failed to retrieve AppId");
+	
+	if (pv.vt != VT_EMPTY) {
+		if (pv.vt != VT_LPWSTR) {
+			cout << "Type: " << pv.vt << endl;
+			die("Unexpected property value type");
+		}
+
+		wcout << "Current AppId: " << pv.pwszVal << endl;
+	} else {
+		cout << "No current AppId" << endl;
+	}
+	PropVariantClear(&pv);
+
+	if (argc > 2) {
+		wcout << "New AppId: " << argv[2] << endl;
+
+		pv.vt = VT_LPWSTR;
+		pv.pwszVal = argv[2];
+
+		doOrDie(store->SetValue(PKEY_AppUserModel_ID, pv),
+				"Failed to set AppId");
+		PropVariantClear(&pv);
+
+		doOrDie(store->Commit(),
+				"Failed to commit AppId property");
+
+		doOrDie(file->Save(NULL, TRUE),
+				"Failed to save shortcut");
+	}
+
+	store->Release();
+	file->Release();
+	link->Release();	
+
+	return 0;
+}
+
