V
V
Vladislav2020-09-20 19:15:28
css
Vladislav, 2020-09-20 19:15:28

LPCWSTR how to make it work?

SPOILER CPP

#include "ProcessCatcher.h"


char process_path[MAX_PATH];
DWORD getProcessPath(DWORD processId) {
    HANDLE handle = NULL;
    DWORD str;
    handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, processId);
    if (handle != NULL)
    {
        str = GetModuleFileNameExA(handle, NULL, process_path, MAX_PATH);
        CloseHandle(handle);
        return str;
    }
    return false;
}

std::wstring GetShellPropStringFromPath(LPCWSTR pPath, PROPERTYKEY const& key)
{
    // Use CComPtr to automatically release the IShellItem2 interface when the function returns
    // or an exception is thrown.
    CComPtr<IShellItem2> pItem;
    HRESULT hr = SHCreateItemFromParsingName(pPath, nullptr, IID_PPV_ARGS(&pItem));
    if (FAILED(hr))
        throw std::system_error(hr, std::system_category(), "SHCreateItemFromParsingName() failed");

    // Use CComHeapPtr to automatically release the string allocated by the shell when the function returns
    // or an exception is thrown (calls CoTaskMemFree).
    CComHeapPtr<WCHAR> pValue;
    hr = pItem->GetString(key, &pValue);
    if (FAILED(hr))
        throw std::system_error(hr, std::system_category(), "IShellItem2::GetString() failed");

    // Copy to wstring for convenience
    return std::wstring(pValue);
}
void StartSearcher(void* pParams)
{
    while (true)
    {
        bool step1 = false;
        bool step2 = false;
        bool step3 = false;

        PROCESSENTRY32 peProcessEntry;
        TCHAR szBuff[1024];
        DWORD dwTemp;
        HANDLE CONST hSnapshot = CreateToolhelp32Snapshot(
            TH32CS_SNAPPROCESS, 0);
        if (INVALID_HANDLE_VALUE == hSnapshot) {
            return;
        }

        peProcessEntry.dwSize = sizeof(PROCESSENTRY32);
        Process32First(hSnapshot, &peProcessEntry);
        do {
            if (0 == lstrcmpW(peProcessEntry.szExeFile, L"File.exe"))
            {
                step1 = true;
            }
            getProcessPath(peProcessEntry.th32ProcessID);
            
            std::cout << process_path << "\n"; // выводит норм.
            try {
                std::wstring file_desc = GetShellPropStringFromPath(process_path, PKEY_FileDescription);
                std::wstring file_corp = GetShellPropStringFromPath((LPCWSTR)process_path, PKEY_Software_ProductName);
                if (file_desc == L"File description") {
                    step2 = true;
                }
            }
            catch (std::system_error const& e) {
                std::wcout << L"ERROR: " << e.what() << L"\nError code: " << e.code() << std::endl;
                continue;
            }
        } while (Process32Next(hSnapshot, &peProcessEntry));

        CloseHandle(hSnapshot);

        if (step1 || step2 || step3)
        {
            std::cout << "Process found!";
        }

        Sleep(1000);
    }
}


SPOILER .h

#pragma once
#include <ShlObj.h>    // Shell API
#include <Propkey.h>   // PKEY_* constants
#include <atlbase.h>   // CComPtr, CComHeapPtr
#include <string>
#include <iostream>
#include <psapi.h>
#include <Windows.h>
#include <io.h>
#include <fcntl.h>
#include <system_error>
#include <WtsApi32.h>
#include <tlhelp32.h>
DWORD getProcessPath(DWORD processId);
std::wstring GetShellPropStringFromPath(LPCWSTR pPath, PROPERTYKEY const& key);
void StartSearcher(void* pParams);

Tell me please.
My task: I need to go through all the processes, check its name.exe and description, if at least one matches, then write what I found!
step1 runs on the process name it is looking for.
And step2 does not work, getting description throws an exception (GetShellPropStringFromPath)
As I understand it, this is due to the fact that the types do not match.

In the example, the author generally indicated the path to the file like this, it won’t work for me, because my function returns the file path in DWORD:
LPCWSTR path = LR"(C:\Program Files\WindowsApps\Microsoft.Windows.Photos_2018.18061.17410.0_x64__8wekyb3d8bbwe\Microsoft.Photos.exe)";


I don't know what to do anymore.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
Dmitry, 2015-11-02
@FenixGnom

the correct option would probably be to use an image
here I made the option without the image
jsfiddle.net/soledar10/xwwpnyv4

L
L0k1, 2015-11-02
@L0k1

right _ _

D
Denis Ineshin, 2015-11-02
@IonDen

2 attached pictures.

D
Denis Bochkov, 2015-11-03
@DENCREAT

Use a wonderful propertyborder-image

S
Sumor, 2020-09-20
@cr1gger

You have char and wchar_t mixed up.
Then:
wchar_t process_path[MAX_PATH];
otherwise you get an array of null pointers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question