Felipe Suman
Felipe Suman

Reputation: 421

How to create an cpp program that when executed it doesn't show any windows

I'm running this code, it's a simple code to lock Windows 8 screen on a Dell that has a button that can be assigned to a .exe, but it shows a cmd window before locking, how can I launch the .exe without showing the window?

Compiling using Visual Studio Dev Command Prompt

Command Line:

cl lockscreen.cpp

Code:

#include <string>
#include <Windows.h>

using namespace std;

int APIENTRY  WinMain(HINSTANCE hInstance,
        HINSTANCE hPrevInstance, 
        LPTSTR    lpCmdLine, 
        int       cmdShow)
{

    system("rundll32 user32.dll,LockWorkStation");

    return 0;
}

Upvotes: 1

Views: 119

Answers (3)

David Heffernan
David Heffernan

Reputation: 613262

You don't need to compile any code at all. Just connect your special keyboard button to

rundll32.exe user32.dll,LockWorkStation

Upvotes: 2

Dabbler
Dabbler

Reputation: 9873

Basically I agree with WhozCraig, but if there is a good reason to make the call via an external exe, try calling it with start /b.

Upvotes: 2

WhozCraig
WhozCraig

Reputation: 66234

Link against user32.dll (user32.lib actually, the import library, but you should already be doing that if this is a stock win32 project) and just invoke LockWorkstation directly. If you must, LoadLibrary() + GetProcAddress() + etc.. There is no need for you to invoke a rundll call for this to work.

Upvotes: 3

Related Questions