Created
November 12, 2017 08:55
Star
You must be signed in to star a gist
Intercepting the Open button
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <shobjidl.h> | |
#include <stdio.h> | |
void test_ifiledialog() | |
{ | |
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | | |
COINIT_DISABLE_OLE1DDE); | |
if (SUCCEEDED(hr)) | |
{ | |
IFileOpenDialog *pFileOpen; | |
// Create the FileOpenDialog object. | |
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, | |
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen)); | |
if (SUCCEEDED(hr)) | |
{ | |
// Show the Open dialog box. | |
hr = pFileOpen->Show(NULL); | |
// Get the file name from the dialog box. | |
if (SUCCEEDED(hr)) | |
{ | |
IShellItem *pItem; | |
hr = pFileOpen->GetResult(&pItem); | |
if (SUCCEEDED(hr)) | |
{ | |
PWSTR pszFilePath; | |
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); | |
// Display the file name to the user. | |
if (SUCCEEDED(hr)) | |
{ | |
wprintf(L"File Path: %ls\n", pszFilePath); | |
CoTaskMemFree(pszFilePath); | |
} | |
pItem->Release(); | |
} | |
} | |
pFileOpen->Release(); | |
} | |
CoUninitialize(); | |
} | |
} | |
HWND hDialog; | |
LRESULT CALLBACK CallWndProc(int code, WPARAM wParam, LPARAM lParam) | |
{ | |
PCWPSTRUCT data = (PCWPSTRUCT)lParam; | |
if (data->hwnd == hDialog && /* Open file dialog */ | |
data->message == WM_COMMAND && | |
data->wParam == 1 && /* Clicked */ | |
data->lParam == (LPARAM)GetDlgItem(hDialog, 1)) /* Open button */ | |
{ | |
printf("CallWndProc thread id: %x\n", GetCurrentThreadId()); | |
// User clicked the Open button, pressed enter, or used the keyboard shortcut. | |
char text[2048]; | |
UINT rv = GetDlgItemText(hDialog, 1148, text, 2047); | |
if (rv > 0) | |
{ | |
if (strstr(text, "://")) | |
{ | |
// User has inputted a filename that looks like a URI. | |
// Wipe the text box before a DNS proxy bypass occurs. | |
SetDlgItemText(hDialog, 1148, ""); | |
data->message = 0; | |
data->wParam = 0; | |
data->lParam = 0; | |
MessageBox(hDialog, "URIs are not permitted.", "Illegal file name", MB_OK); | |
} | |
} | |
} | |
return CallNextHookEx(0, code, wParam, lParam); | |
} | |
HHOOK hWndProcHook; | |
VOID CALLBACK WinEventProcCallback(HWINEVENTHOOK hWinEventHook, | |
DWORD dwEvent, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime) | |
{ | |
if (idObject == OBJID_WINDOW && dwEvent == EVENT_OBJECT_CREATE) | |
{ | |
if (IsWindow(hwnd) && GetDlgCtrlID(hwnd) == 1148) | |
{ | |
// We have the file dialog's filename text field. | |
printf("WinEventProcCallback thread id: %x\n", GetCurrentThreadId()); | |
hDialog = GetParent(hwnd); | |
DWORD threadID = GetWindowThreadProcessId(hDialog, NULL); | |
// Hook messages to the file dialog. | |
hWndProcHook = SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, NULL, threadID); | |
} | |
} | |
} | |
HWINEVENTHOOK test_wineventhook() | |
{ | |
// Hook creation of the Open File Dialog. | |
return SetWinEventHook( | |
EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE, | |
NULL, WinEventProcCallback, GetCurrentProcessId(), 0, | |
WINEVENT_OUTOFCONTEXT); | |
} | |
int main() | |
{ | |
printf("main thread id: %x\n", GetCurrentThreadId()); | |
HWINEVENTHOOK hHook = test_wineventhook(); | |
test_ifiledialog(); | |
UnhookWinEvent(hHook); | |
UnhookWindowsHookEx(hWndProcHook); | |
system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment