Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

The Brotherhood 

CPU Monitoring Tool [C++ Source Code] - Scan CPU % Every X Seconds

[Image: header4.gif]


CPU Monitoring Tool
C++ Source Code


When it comes to keeping your computer safe from malware or other malicious software, the first thing is to detect that its there in the first place. There are many ways to do this, but one thing you can do is look for abnormal CPU usage. Of course, having hundreds of Chrome tabs, or opening a large Adobe file will eat through resources (RAM, CPU, GPU, etc). However, when your computer is truly idle, not updating, not refreshing in the background, your resource usage should be minimal.

For that reason, I asked GPT-4 to create a program using C++ that can find the current CPU % usage, via WMI query lookups (WQL).

Checking CPU % patterns can help identify if a software is running processes "secretly" on your computer.


[Image: Screenshot-2024-05-08-224709.png]



The full source code can be seen below, but first I will show you some highlights to give you a better idea of how this program works.


Code
hres = CoInitializeEx(0, COINIT_MULTITHREADED);

Here is a snippet about this function:
https://pubs.opengroup.org/onlinepubs/00...ialization
Quote:To use basic COM services, all COM threads of execution in clients and out-of-process servers must call either the CoInitialize() or the CoInitializeEx() function before calling any other COM function except memory allocation calls. CoInitializeEx() replaces the other function, adding a parameter that allows you to specify the threading model of the thread--either apartment-threaded or free-threaded. A call to CoInitialize() simply sets the threading model to apartment-threaded. For information on threading in clients and servers, refer to Chapter 6.



To use COM, you set the security levels before usage.

[Image: Screenshot-2024-05-08-222720.png]

https://learn.microsoft.com/en-us/window...zesecurity

Here are the security levels for the code I'm sharing:
Code
hres = CoInitializeSecurity(
    NULL,
    -1,
    NULL,
    NULL,
    RPC_C_AUTHN_LEVEL_DEFAULT,
    RPC_C_IMP_LEVEL_IMPERSONATE,
    NULL,
    EOAC_NONE,
    NULL
);

According to the docs, once we've established our COM variables, we can then continue on to connect to a WMI namespace. Again, still learning about this myself, but from what I understand, it's a "bridge" or a "proxy" to the core WMI database on your machine.

https://learn.microsoft.com/en-us/window...-namespace

Code
hres = CoCreateInstance(
    CLSID_WbemLocator,
    0,
    CLSCTX_INPROC_SERVER,
    IID_IWbemLocator, (LPVOID*)&pLoc);

// snipped code

hres = pLoc->ConnectServer(
    _bstr_t(L"ROOT\\CIMV2"),
    NULL,
    NULL,
    0,
    NULL,
    0,
    0,
    &pSvc
);

Finally, now that we have prepared our WMI connection, we can start querying!
Lets get the CPU load percentage:

Code
HRESULT hres = pSvc->ExecQuery(
    bstr_t("WQL"),
    bstr_t("SELECT LoadPercentage FROM Win32_Processor"),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    NULL,
    &pEnumerator
);

If you want to try out other WQL queries, here's more info:
https://learn.microsoft.com/en-us/window...-execquery
Code
HRESULT ExecQuery(
  [in]  const BSTR           strQueryLanguage,
  [in]  const BSTR           strQuery,
  [in]  long                 lFlags,
  [in]  IWbemContext         *pCtx,
  [out] IEnumWbemClassObject **ppEnum
);




[/align]


Source Code
Spoiler (Click to View)
I'm Stanley HF's helper bot. The OP (original poster) of this thread wants everyone to see this page and has elected to bump it to the top of the showthread listing. Since HF doesn't want a ton of LQP (Low Quality Posts) this is the best method of bumping your content. There is a small βyte fee for each bump. Any OP can bump any of their threads on HF from the linked icon on the top of their first post.

If you haven't already read the first post of this thread we encourage you to do so.

Thanks,
Stanley
I am the official HF Help Bot.  You might get an automated PM from me with some helpful information.
Please read our Help Docs.
(May 8, 2024 07:48 PM)Hackers Wrote:  
[Image: header4.gif]


CPU Monitoring Tool
C++ Source Code


When it comes to keeping your computer safe from malware or other malicious software, the first thing is to detect that its there in the first place. There are many ways to do this, but one thing you can do is look for abnormal CPU usage. Of course, having hundreds of Chrome tabs, or opening a large Adobe file will eat through resources (RAM, CPU, GPU, etc). However, when your computer is truly idle, not updating, not refreshing in the background, your resource usage should be minimal.

For that reason, I asked GPT-4 to create a program using C++ that can find the current CPU % usage, via WMI query lookups (WQL).

Checking CPU % patterns can help identify if a software is running processes "secretly" on your computer.


[Image: Screenshot-2024-05-08-224709.png]



The full source code can be seen below, but first I will show you some highlights to give you a better idea of how this program works.


Code
hres = CoInitializeEx(0, COINIT_MULTITHREADED);

Here is a snippet about this function:
https://pubs.opengroup.org/onlinepubs/00...ialization


To use COM, you set the security levels before usage.

[Image: Screenshot-2024-05-08-222720.png]

https://learn.microsoft.com/en-us/window...zesecurity

Here are the security levels for the code I'm sharing:
Code
hres = CoInitializeSecurity(
    NULL,
    -1,
    NULL,
    NULL,
    RPC_C_AUTHN_LEVEL_DEFAULT,
    RPC_C_IMP_LEVEL_IMPERSONATE,
    NULL,
    EOAC_NONE,
    NULL
);

According to the docs, once we've established our COM variables, we can then continue on to connect to a WMI namespace. Again, still learning about this myself, but from what I understand, it's a "bridge" or a "proxy" to the core WMI database on your machine.

https://learn.microsoft.com/en-us/window...-namespace

Code
hres = CoCreateInstance(
    CLSID_WbemLocator,
    0,
    CLSCTX_INPROC_SERVER,
    IID_IWbemLocator, (LPVOID*)&pLoc);

// snipped code

hres = pLoc->ConnectServer(
    _bstr_t(L"ROOT\\CIMV2"),
    NULL,
    NULL,
    0,
    NULL,
    0,
    0,
    &pSvc
);

Finally, now that we have prepared our WMI connection, we can start querying!
Lets get the CPU load percentage:

Code
HRESULT hres = pSvc->ExecQuery(
    bstr_t("WQL"),
    bstr_t("SELECT LoadPercentage FROM Win32_Processor"),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    NULL,
    &pEnumerator
);

If you want to try out other WQL queries, here's more info:
https://learn.microsoft.com/en-us/window...-execquery
Code
HRESULT ExecQuery(
  [in]  const BSTR           strQueryLanguage,
  [in]  const BSTR           strQuery,
  [in]  long                 lFlags,
  [in]  IWbemContext         *pCtx,
  [out] IEnumWbemClassObject **ppEnum
);




[/align]


Source Code
Spoiler (Click to View)

Do you have a repo anywhere for this?
hey bro u didn't need to block me and give me a -12, im new here and i thought this was a hacker forum, where you are allowed to ask anything.
(May 8, 2024 07:48 PM)Hackers Wrote:  
[Image: header4.gif]


CPU Monitoring Tool
C++ Source Code


When it comes to keeping your computer safe from malware or other malicious software, the first thing is to detect that its there in the first place. There are many ways to do this, but one thing you can do is look for abnormal CPU usage. Of course, having hundreds of Chrome tabs, or opening a large Adobe file will eat through resources (RAM, CPU, GPU, etc). However, when your computer is truly idle, not updating, not refreshing in the background, your resource usage should be minimal.

For that reason, I asked GPT-4 to create a program using C++ that can find the current CPU % usage, via WMI query lookups (WQL).

Checking CPU % patterns can help identify if a software is running processes "secretly" on your computer.


[Image: Screenshot-2024-05-08-224709.png]



The full source code can be seen below, but first I will show you some highlights to give you a better idea of how this program works.


Code
hres = CoInitializeEx(0, COINIT_MULTITHREADED);

Here is a snippet about this function:
https://pubs.opengroup.org/onlinepubs/00...ialization


To use COM, you set the security levels before usage.

[Image: Screenshot-2024-05-08-222720.png]

https://learn.microsoft.com/en-us/window...zesecurity

Here are the security levels for the code I'm sharing:
Code
hres = CoInitializeSecurity(
    NULL,
    -1,
    NULL,
    NULL,
    RPC_C_AUTHN_LEVEL_DEFAULT,
    RPC_C_IMP_LEVEL_IMPERSONATE,
    NULL,
    EOAC_NONE,
    NULL
);

According to the docs, once we've established our COM variables, we can then continue on to connect to a WMI namespace. Again, still learning about this myself, but from what I understand, it's a "bridge" or a "proxy" to the core WMI database on your machine.

https://learn.microsoft.com/en-us/window...-namespace

Code
hres = CoCreateInstance(
    CLSID_WbemLocator,
    0,
    CLSCTX_INPROC_SERVER,
    IID_IWbemLocator, (LPVOID*)&pLoc);

// snipped code

hres = pLoc->ConnectServer(
    _bstr_t(L"ROOT\\CIMV2"),
    NULL,
    NULL,
    0,
    NULL,
    0,
    0,
    &pSvc
);

Finally, now that we have prepared our WMI connection, we can start querying!
Lets get the CPU load percentage:

Code
HRESULT hres = pSvc->ExecQuery(
    bstr_t("WQL"),
    bstr_t("SELECT LoadPercentage FROM Win32_Processor"),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    NULL,
    &pEnumerator
);

If you want to try out other WQL queries, here's more info:
https://learn.microsoft.com/en-us/window...-execquery
Code
HRESULT ExecQuery(
  [in]  const BSTR           strQueryLanguage,
  [in]  const BSTR           strQuery,
  [in]  long                 lFlags,
  [in]  IWbemContext         *pCtx,
  [out] IEnumWbemClassObject **ppEnum
);




[/align]


Source Code
Spoiler (Click to View)

how is it different from https://www.alcpu.com/CoreTemp/


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Check-Up] by salad122424Oct 7, 2017 06:25 AM 6 321 Oct 7, 2017 06:25 AM
Last Post: 5chn4pp
by joshua00214Apr 25, 2015 07:14 AM 2 231 Apr 25, 2015 07:14 AM
Last Post: ⁠Cammy
by PrudeJan 24, 2015 07:12 PM 10 402 Jan 24, 2015 07:12 PM
Last Post: Prude