Issue with opening EXE in C++

I'm attempting to replace some super-old code embedded in our program with our own On-Screen Keyboard with a simple call to the Windows 7 one (%windir%\system32\osk.exe)

I wrote the below as an experiment to make sure I knew it worked:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    string command;

    command = "C:\\WINDOWS\\system32\\osk.exe";

    system(command.c_str());

	return 0;
}


I instead get the error message "Could Not Start On-Screen Keyboard".

Now I've verified that I can open it manually. I've attempted to run my visual studio and ithe built executable as an administrator directly (in case it mattered.) I've also verified that cmd.exe can be opened without additional "Run As Administrator" calls.

Any thoughts as to why it won't open specifically?
Last edited on
I'm guessing that it's because the "system" command is unsafe and Windows isn't letting you launch anything via it with it's required permissions. Try using the "ShellExecute" function.
Seems this is a semi common problem. I've found a few topics on it after attempting your solution (because of ShellExecute being part of my search :)

It turns out it doesn't matter if it's system or ShellExecute*; it matters that it's compiled in x64 bit. Link to stackoverflow explanation:

*This is to say, that the original code works if you compile it in x64

http://stackoverflow.com/questions/23116149/delphi-on-screen-keyboard-osk-exe-works-on-win32-but-fails-on-win64


I'll use the ShellExecute version due to your comment and it's just generally cleaner. Below is what i'm using. Thanks for the help!

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
#include "stdafx.h"
#include <windows.h>
#include <shellapi.h>
#include <atlstr.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    CString path = _T("C:\\WINDOWS\\system32\\");
    CString file = _T("osk.exe");

    SHELLEXECUTEINFO ShExecInfo;

    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = NULL;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = L"open";
    ShExecInfo.lpFile = file;
    ShExecInfo.lpParameters = NULL;
    ShExecInfo.lpDirectory = path;
    ShExecInfo.nShow = SW_SHOW;
    ShExecInfo.hInstApp = NULL;

    ShellExecuteEx(&ShExecInfo);

	return 1;
}
Last edited on
Topic archived. No new replies allowed.