MCPcopy Create free account
hub / github.com/ModOrganizer2/modorganizer / IContextMenu_GetCommandString

Function IContextMenu_GetCommandString

src/envshell.cpp:96–158  ·  view source on GitHub ↗

adapted from https://devblogs.microsoft.com/oldnewthing/20040928-00/?p=37723

Source from the content-addressed store, hash-verified

94// https://devblogs.microsoft.com/oldnewthing/20040928-00/?p=37723
95//
96HRESULT IContextMenu_GetCommandString(IContextMenu* pcm, UINT_PTR idCmd, UINT uFlags,
97 UINT* pwReserved, LPWSTR pszName, UINT cchMax)
98{
99 // Callers are expected to be using Unicode.
100 if (!(uFlags & GCS_UNICODE)) {
101 return E_INVALIDARG;
102 }
103
104 // Some context menu handlers have off-by-one bugs and will
105 // overflow the output buffer. Let�s artificially reduce the
106 // buffer size so a one-character overflow won�t corrupt memory.
107 if (cchMax <= 1) {
108 return E_FAIL;
109 }
110
111 cchMax--;
112
113 // First try the Unicode message. Preset the output buffer
114 // with a known value because some handlers return S_OK without
115 // doing anything.
116 pszName[0] = L'\0';
117
118 HRESULT hr = pcm->GetCommandString(idCmd, uFlags, pwReserved, (LPSTR)pszName, cchMax);
119
120 if (SUCCEEDED(hr) && pszName[0] == L'\0') {
121 // Rats, a buggy IContextMenu handler that returned success
122 // even though it failed.
123 hr = E_NOTIMPL;
124 }
125
126 if (FAILED(hr)) {
127 // try again with ANSI � pad the buffer with one extra character
128 // to compensate for context menu handlers that overflow by
129 // one character.
130 LPSTR pszAnsi = (LPSTR)LocalAlloc(LMEM_FIXED, (cchMax + 1) * sizeof(CHAR));
131
132 if (pszAnsi) {
133 pszAnsi[0] = '\0';
134
135 hr = pcm->GetCommandString(idCmd, uFlags & ~GCS_UNICODE, pwReserved, pszAnsi,
136 cchMax);
137
138 if (SUCCEEDED(hr) && pszAnsi[0] == '\0') {
139 // Rats, a buggy IContextMenu handler that returned success
140 // even though it failed.
141 hr = E_NOTIMPL;
142 }
143
144 if (SUCCEEDED(hr)) {
145 if (MultiByteToWideChar(CP_ACP, 0, pszAnsi, -1, pszName, cchMax) == 0) {
146 hr = E_FAIL;
147 }
148 }
149
150 LocalFree(pszAnsi);
151
152 } else {
153 hr = E_OUTOFMEMORY;

Callers 1

onMenuSelectMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected