Home:ALL Converter>check a name of a child and close and open it not working

check a name of a child and close and open it not working

Ask Time:2021-09-22T05:27:03         Author:Soup Man

Json Formatter

I'm trying to build a program that detects a specific name of a child process. After it does that, I want it to close and open that whole .exe again. But I'm getting a few build errors:

warning C4244: '=': conversion from 'int' to 'float', possible loss of data

error C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

warning C4244: 'argument': conversion from 'wchar_t' to 'const _Elem', possible loss of data

Please help.

#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <fstream>
#include <Psapi.h>

int main()
{    
    time_t rawtime; //creates and object of the built in time function
    struct tm* timeinfo; //no idea what this do

    time(&rawtime); //gets the time from the computer
    timeinfo = localtime(&rawtime); //store that time here

    for (; ;)
    {
        static HWND Program_hwnd = nullptr;
        static float last_hwnd_time = 0.f;

        int text_width = 0;

        if ((!Program_hwnd || Program_hwnd == INVALID_HANDLE_VALUE) && last_hwnd_time < timeinfo->tm_sec) {
            for (HWND hwnd = GetTopWindow(0); hwnd; hwnd = GetWindow(hwnd, GW_HWNDNEXT)) {

                last_hwnd_time = timeinfo->tm_sec ;

                if (!(IsWindowVisible)(hwnd))
                    continue;

                int length = (GetWindowTextLengthW)(hwnd);
                if (length == 0)
                    continue;

                WCHAR filename[300];
                DWORD pid;
                (GetWindowThreadProcessId)(hwnd, &pid);

                const auto Program_handle = (OpenProcess)(PROCESS_QUERY_INFORMATION, FALSE, pid);
                (K32GetModuleFileNameExW)(Program_handle, nullptr, filename, 300);

                std::wstring sane_filename{ filename };

                (CloseHandle)(Program_handle);

                if (sane_filename.find((L"Program.exe")) != std::string::npos)
                    Program_hwnd = hwnd;
            }
        }
        else if (Program_hwnd && Program_hwnd != INVALID_HANDLE_VALUE) {
            WCHAR title[300];

            if (!(GetWindowTextW)(Program_hwnd, title, 300)) {
                Program_hwnd = nullptr;
            }
            else {
                std::wstring sane_title{ title };
                std::string Title = " ";
                std::string Song(sane_title.begin(), sane_title.end());
                Title += Song;
                if (sane_title.find((L"EX")) != std::string::npos) {
                    system("taskkill /IM Program.exe /F");
                    system("start C:\\Program.exe");
                }
            }
        }
    }
}

Author:Soup Man,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69275739/check-a-name-of-a-child-and-close-and-open-it-not-working
yy