-3

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");
                }
            }
        }
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Why are you wrapping (almost) every Win32 API function name in parenthesis? That is not necessary. `(IsWindowVisible)(hwnd)` -> `IsWindowVisible(hwnd)`, `(GetWindowTextLengthW)(hwnd)` -> `GetWindowTextLengthW(hwnd)`, `(GetWindowThreadProcessId)(hwnd, &pid)` -> `GetWindowThreadProcessId(hwnd, &pid)`, etc – Remy Lebeau Sep 22 '21 at 00:28

1 Answers1

0

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

You are assigning timeinfo->tm_sec (an int) to last_hwnd_time (a float). Not all int values can be represented exactly in float. See int to float conversion produces a warning?

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

localtime() is deprecated in favor of a newer and safer localtime_s(). See Security Features in the CRT.

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

The way you are converting sane_title (a std::wstring) to Song (a std::string) is wrong. You are simply truncating each wchar_t as-is to char values. You need an actual data conversion instead, such as with std::wstring_convert, WideCharToMultiByte(), etc. However, this is moot, since you aren't using Title for anything, so you can just remove the conversion completely.

Also, when calling taskkill.exe, if there are multiple Program.exe processes running then there is no guarantee which process it will kill. Since you have already determined earlier which process you want to kill, you should save that process ID and use it in the /PID parameter of taskkill.exe, eg:

system(("taskkill /PID " + to_string(pid) + " /F").c_str());
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770