forked from 0x7c13/Notepads
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
73 lines (62 loc) · 2.19 KB
/
main.cpp
File metadata and controls
73 lines (62 loc) · 2.19 KB
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
#include "pch.h"
#include "iostream"
#ifndef _DEBUG
#define AUMID L"19282JackieLiu.Notepads-Beta_echhpq9pdbte8!App"
#else
#define AUMID L"Notepads-Dev_ezhh5fms182ha!App"
#endif
#define OPEN L"open"
#define PRINT L"print"
#define NOTEPADPRINTCOMMAND L"/p"
#define NOTEPADOPENWITHANSIENCODINGCOMMAND L"/a"
#define NOTEPADOPENWITHUTF16ENCODINGCOMMAND L"/w"
using namespace std;
using namespace winrt;
#ifndef _DEBUG
INT APIENTRY wWinMain(_In_ HINSTANCE /* hInstance */, _In_opt_ HINSTANCE /* hPrevInstance */, _In_ LPWSTR /* lpCmdLine */, _In_ INT /* nCmdShow */)
#else
INT main()
#endif
{
init_apartment();
LPWSTR* szArglist = NULL;
INT nArgs;
szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
if (szArglist && nArgs > 1)
{
LPCTSTR verb = OPEN;
if (wcscmp(NOTEPADPRINTCOMMAND, szArglist[1]) == 0) verb = PRINT;
bool isOpenRequested = (wcscmp(OPEN, verb) == 0);
INT ct = nArgs - isOpenRequested ? 1 : 2;
HRESULT hr = E_OUTOFMEMORY;
com_ptr< IShellItemArray> ppsia = NULL;
PIDLIST_ABSOLUTE* rgpidl = new(std::nothrow) PIDLIST_ABSOLUTE[ct];
if (rgpidl)
{
hr = S_OK;
INT cpidl;
for (cpidl = 0; SUCCEEDED(hr) && cpidl < ct; cpidl++)
{
hr = SHParseDisplayName(szArglist[cpidl + isOpenRequested ? 1 : 2], NULL, &rgpidl[cpidl], 0, NULL);
}
if (cpidl > 0 && SUCCEEDED(SHCreateShellItemArrayFromIDLists(cpidl, rgpidl, ppsia.put())))
{
com_ptr<IApplicationActivationManager> appActivationMgr = NULL;
if (SUCCEEDED(CoCreateInstance(CLSID_ApplicationActivationManager, NULL, CLSCTX_LOCAL_SERVER, __uuidof(appActivationMgr), appActivationMgr.put_void())))
{
DWORD pid = 0;
appActivationMgr->ActivateForFile(AUMID, ppsia.get(), verb, &pid);
}
appActivationMgr.~com_ptr();
}
for (INT i = 0; i < cpidl; i++)
{
CoTaskMemFree(rgpidl[i]);
}
}
ppsia.~com_ptr();
delete[] rgpidl;
}
LocalFree(szArglist);
uninit_apartment();
}