C++ to find Save As window & Enter Save As Name ?

I am trying to write a program that finds the "Save As" window when a user clicks save as. Then automatically type in the file name and hit enter. This is what I have so far

/* HWND = "Window Handle" */
HWND saveWindow = FindWindow(0, "Save Workspace As");
/* This is a function to simplify usage of sending keys */
void GenerateKey(int vk, BOOL bExtended) {

KEYBDINPUT kb = {0};
INPUT Input = {0};

/* Generate a "key down" */
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

/* Generate a "key up" */
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

return;
}
int main() {
SetForegroundWindow(saveWindow);
GenerateKey(VK_CAPITAL, TRUE);
GenerateKey('T', FALSE);
GenerateKey(VK_CAPITAL, TRUE);
GenerateKey('E', FALSE);
GenerateKey('S', FALSE);
GenerateKey('T', FALSE);
GenerateKey('1', FALSE);
//Enter key
GenerateKey(0x0D, TRUE);
return 0;

But this finds the window, but doesn't enter anything in nor press enter. What am I doing wrong?
According to the MSDN about SendInput():

"This function is subject to UIPI. Applications are permitted to inject input only into applications that are at an equal or lesser integrity level."

"This function fails when it is blocked by UIPI. Note that neither GetLastError nor the return value will indicate the failure was caused by UIPI blocking."

Maybe that's your problem.
Have you tried SendMessage() or PostMessage() ?
Since you already have the window handle that shouldn't be too hard.

BTW: you shouldn't execute FindWindow() outside any function. It's executed BEFORE main() which makes me cringe (somehow).
Last edited on
Topic archived. No new replies allowed.