/
StackManager.h
105 lines (83 loc) · 3.37 KB
/
StackManager.h
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
#pragma once
#include "pch.h"
#include <functional>
#pragma region CoreDLLs::Reference
extern HRESULT(WINAPI* vpfnCbsCoreInitialize)(IMalloc*, int(WINAPI*)(int), void (*)(), void (*)(), void (*)(), void (*)(), void (*)(), IClassFactory**);
extern HRESULT(WINAPI* vpfnCbsCoreSetState)(int state, PROC value);
extern HRESULT(WINAPI* vpfnCbsCoreFinalize)();
extern HRESULT(WINAPI* vpfnSetTestMode)(int mode);
extern HRESULT(WINAPI* vpfnSxsStoreInitialize)(IMalloc*, int(WINAPI*)(int), void (*)(), void (*)(), void (*)(), void (*)(), IClassFactory**);
extern HRESULT(*vpfnSxsStoreFinalize)();
extern HRESULT(WINAPI* vpfnWdsSetupLogMessageA)(LPVOID pMsg, enum WdsLogSource source, const char*, const char*, ULONG, const char* file, const char* func, void* CurIP, ULONG, void*, UINT);
extern LPVOID(WINAPI* vpfnConstructPartialMsgVA)(enum WdsLogLevel level, const char* fmtMsg, va_list va);
extern LPVOID(*vpfnCurrentIP)();
extern int(WINAPI* vpfnGetSystemStore)(unsigned int, const struct _GUID&, struct IUnknown**);
extern const GUID CLSID_CbsSession;
extern const GUID IID_ICbsSession;
extern const GUID IID_ICbsSessionPrivate;
extern const GUID IID_ICbsSession9;
extern const GUID IID_IServicingQuerier;
extern const GUID IID_ISxSStore;
extern const GUID IID_ICSIStore;
extern const GUID IID_ICbsUIHandler;
extern const GUID IID_IStore;
extern const GUID IID_IStore2;
extern const GUID IID_ICSISmartInstaller;
#pragma endregion
int NilFunc1(int);
void NilFunc2();
class StackManager {
bool bStackFound = false,
bCbsCoreLoaded = false,
bSxSStoreLoaded = false,
bSSShimLoaded = false,
bCbsCoreInited = false,
bSxSStoreInited = false,
bWdsCoreInited = false,
bWcpInited = false;
std::wstring ServicingStack;
std::wstring CbsCore;
std::wstring SxSStore;
IClassFactory* pCbsCoreFactory = nullptr;
IClassFactory* pSxSFactory = nullptr;
ICSIStore* pCSIStore = nullptr;
ICbsSession* pSess = nullptr;
HRESULT FindStackByReg();
HRESULT FindStackByPath(std::wstring path);
HRESULT FindStackBySSShim(std::wstring path);
// if you don't specify strBootDrive argument,
// the session will be online.
HRESULT ApplySess(CbsSessionOption opt,
const std::wstring& strClientId = L"CallCbsCore",
const std::wstring& strBootDrive = L"");
public:
// set them if you need, but null is valid.
struct Callbacks {
decltype(&NilFunc1) LockProcCB;
decltype(&NilFunc2) UnlockProcCB, InstCreatedCB, InstDestroyedCB,
ReqShutdownNowCB, ReqShutdownProcessingCB;
} CoreEvent = {};
[[nodiscard]] ICbsSession* GetActiveSess() const;
HRESULT GetNewSess(CbsSessionOption opt = CbsSessionOption::None);
HRESULT SubmitSess();
HRESULT FindStack();
HRESULT LoadCbsCore();
HRESULT LoadSxSStore();
HRESULT LoadSSShim();
HRESULT InitCbsCore();
HRESULT InitSxSStore();
HRESULT InitWdsCore();
HRESULT InitWcp();
// opt: seems must be 0, iid = IID_ICSIExternalTransformerExecutor or IID_ICSIStore
HRESULT GetSystemStore(UINT opt, const _GUID& iid, IUnknown** ppOut) const;
// Default Settings
HRESULT Load();
HRESULT Dispose();
template<class FuncT>
[[nodiscard]] static std::function<FuncT> GetFunction(const std::wstring module, const std::string name) {
return reinterpret_cast<FuncT*>(GetProcAddress(GetModuleHandle(module.c_str()), name.c_str()));
}
~StackManager() { Dispose(); }
};
extern StackManager g_mgr;
#define g_sess (g_mgr.GetActiveSess())