Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9093989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:14:29+00:00 2026-06-16T23:14:29+00:00

I have the following code (it gets all processes then search for a regex

  • 0

I have the following code (it gets all processes then search for a regex pattern in them, code for a larger personal project for malware detection), the code does what I want but the only problem it is using 100% of CPU, what do I do wrong? Bad memory allocation? I compiled it with MS Visual Studio 2010 (cl.exe /EHsc mycode.cpp)

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <tlhelp32.h>
#include <psapi.h>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <regex>

using namespace std;

#pragma comment(lib, "psapi.lib") 

void PrintProcessNameAndID(DWORD);
void find_locs(HANDLE process);
void ListProcesses();

int main(int argc, char **argv) {
    ListProcesses();
}

void find_locs(HANDLE process) {

    unsigned char *p = NULL;
    MEMORY_BASIC_INFORMATION info;

    for ( p = NULL;
        VirtualQueryEx(process, p, &info, sizeof(info)) == sizeof(info);
        p += info.RegionSize )
    {
        std::string buffer;

        if (info.State == MEM_COMMIT &&
            (info.Type == MEM_MAPPED || info.Type == MEM_PRIVATE))
        {
            DWORD bytes_read;

            buffer.resize(info.RegionSize);
            ReadProcessMemory(process, p, &buffer[0], info.RegionSize, &bytes_read);
            buffer.resize(bytes_read);

            const std::tr1::regex rx("([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})");
            std::tr1::match_results<std::string::const_iterator> res; 
            std::tr1::regex_search(buffer, res, rx);

            ofstream myfile;
            myfile.open ("proc.txt", ios::app);

            for (unsigned int i=0; i<res.size(); ++i)
            {
                std::cout << res[i] << std::endl;
                myfile << res[i] << "\n";
            }

            myfile.close();
        }
    }
}

void ListProcesses()
{
    DWORD aProcesses[1024];
    DWORD cbNeeded;
    DWORD cProcesses;
    unsigned int i;

    if (!EnumProcesses(aProcesses,sizeof(aProcesses),&cbNeeded))
        return;

    cProcesses = cbNeeded / sizeof(DWORD);

    for ( i = 0; i < cProcesses; i++ )
    {
        PrintProcessNameAndID(aProcesses[i]);
    }
}

void PrintProcessNameAndID(DWORD processID)
{
    TCHAR szProcessName[MAX_PATH]; // = TEXT("<unknown>");

    HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);

    if (NULL != hProcess)
    {
        HMODULE hMod;
        DWORD cbNeeded;

        if (EnumProcessModules(hProcess, &hMod, sizeof(hMod),
             &cbNeeded))
        {
            GetModuleBaseName(hProcess, hMod, szProcessName,
                               sizeof(szProcessName)/sizeof(TCHAR));
        }
    }
    _tprintf(TEXT("pid: %u file: %s\n"), processID, szProcessName);
    find_locs(hProcess);
    CloseHandle(hProcess);
}

Thanks for help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:14:30+00:00Added an answer on June 16, 2026 at 11:14 pm

    There’s nothing wrong with a program taking up 100% of the processor… (I don’t know how I can expand this answer beyond this)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which gets all news: private function get_news() { $news
I have the following code that gets the MAC address of an interface: static
In PHP I have the following code that gets a file submitted through CGI:
I have the following php code that gets a serialized array from a mysql
I have just started using jQuery and although following code gets the job done,
I have the following code in a Runnable that gets passed to a thread
When MyView gets pushed on to the NavigationController, I have the following code public
I have a line of code that gets the following error when run through
I have the following code that gets called after the view loads. It works
I have following code in HomeController ; it gets latest date and shows songs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.