-
Notifications
You must be signed in to change notification settings - Fork 6
/
winappid.c
126 lines (101 loc) · 2.87 KB
/
winappid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// winappid, adapted by Thomas Wolff
// and ported to cygwin C from a Windows C++ program.
// link options -L/usr/lib/w32api -lole32 -luuid
//
// Win7AppId
// Author: David Roe (didroe)
// The code's a bit rough and ready but it does the job
//
// Compile with VS 2010 express using command:
// cl /EHsc /D _UNICODE Win7AppId.cpp /link ole32.lib
#include <windows.h>
#include <propkey.h>
#include <shlobj.h>
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#include <sys/cygwin.h>
typedef void * * voidrefref;
void die(char * message) {
printf("Error: %s\n", message);
exit(1);
}
void doOrDie(HRESULT hr, char * message) {
if (!SUCCEEDED(hr)) {
die(message);
}
}
int main(int argc, char * argv[])
{
setlocale (LC_CTYPE, "");
if (argc <= 1) {
char * bn = strrchr(argv[0], '/');
if (bn)
bn++;
else
bn = argv[0];
printf("Usage: %s SHORTCUT [APPID]\n", bn);
printf("\n");
printf("Display the Windows AppID of a shortcut or set an AppID\n");
exit(0);
}
doOrDie(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED),
"Failed to initialise COM");
IShellLink * link;
doOrDie(CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
&IID_IShellLink, (voidrefref) &link),
"Failed to create ShellLink object");
IPersistFile * file;
doOrDie(
link->lpVtbl->QueryInterface(link, &IID_IPersistFile,
(voidrefref) &file),
"Failed to obtain PersistFile interface");
char * fn = cygwin_create_path(CCP_POSIX_TO_WIN_W, argv[1]);
if (!fn)
die("Path conversion failed");
if (argc > 2) {
doOrDie(file->lpVtbl->Load(file, (LPCOLESTR)fn, (DWORD)STGM_READWRITE),
"Failed to load shortcut file");
} else {
doOrDie(file->lpVtbl->Load(file, (LPCOLESTR)fn, STGM_READ | STGM_SHARE_DENY_NONE),
"Failed to load shortcut file");
}
IPropertyStore * store;
doOrDie(
link->lpVtbl->QueryInterface(link, &IID_IPropertyStore,
(voidrefref) &store),
"Failed to obtain PropertyStore interface");
PROPVARIANT pv;
doOrDie(store->lpVtbl->GetValue(store, &PKEY_AppUserModel_ID, &pv),
"Failed to retrieve AppId");
if (pv.vt != VT_EMPTY) {
if (pv.vt != VT_LPWSTR) {
printf("Type: %d\n", pv.vt);
die("Unexpected property value type");
}
printf("Current AppId: %ls\n", pv.pwszVal);
} else {
printf("No current AppId\n");
}
PropVariantClear(&pv);
if (argc > 2) {
wchar_t appid[MAX_PATH];
mbstowcs(appid, argv[2], MAX_PATH);
printf("New AppId: %ls\n", appid);
pv.vt = VT_LPWSTR;
pv.pwszVal = appid;
doOrDie(store->lpVtbl->SetValue(store, &PKEY_AppUserModel_ID, &pv),
"Failed to set AppId");
// Not sure if we need to do this
pv.pwszVal = NULL;
PropVariantClear(&pv);
doOrDie(store->lpVtbl->Commit(store),
"Failed to commit AppId property");
doOrDie(file->lpVtbl->Save(file, NULL, TRUE),
"Failed to save shortcut");
}
store->lpVtbl->Release(store);
file->lpVtbl->Release(file);
link->lpVtbl->Release(link);
return 0;
}