-1

What I am trying to do is get a list of processes in an edit child window. I originally was gonna use a series of buttons with a scroll bar but I don't know how to do that (if you have a solution for that) and found no resources how to.

I tried wchar.h functions, and type casting.

HWND listProc = CreateWindow(L"Edit", L"", WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_HSCROLL,
            0, 0, 100, 300, process, NULL, NULL, NULL);
        LPWSTR procList = L'';
        Process32First(procSnap, &procEntry);
        while (Process32Next(procSnap, &procEntry))
        {
            wcsncat(procList, procEntry.szExeFile, MAX_PATH);
            wcsncat(procList, L"\n", 1);
        }
        SetWindowText(listProc, procList);
        CloseHandle(procSnap);

2 Answers2

1

First, your code will not compile:

LPWSTR procList = L'';

you cannot initialize a pointer with a single wide character.

Second, assuming you intended to initialize procList to an empty string, your calls to wcsncat will result in undefined behavior since that function expects the destination to point to enough space to perform the concatenation. It will not allocate additional memory.

I recommend you not use C-style strings at all, and use std::wstring instead. You can concatenate additional strings with the += operator, and additional memory will be allocated. You can pass the string contents to SetWindowText by using the c_str() function.

jkb
  • 2,376
  • 1
  • 9
  • 12
0
  1. As @jkb metioned, usestd::wstring;
  2. use std::stringstream.(or any other stream class)

In addition, there are other problems in your sample. If you don't want your output text to be one line and hard to read, you need to add the Exstyle of ES_MULTILINE | ES_AUTOVSCROLL to your edit control, and use "\r\n" to connect strings to Wrap text.

1). Sample:

   HWND listProc = CreateWindowEx(0, L"Edit", L"", WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_HSCROLL| ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
       0, 0, 100, 300, hWnd, NULL, NULL, NULL);

   HANDLE procSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

   std::wstring procList;
   PROCESSENTRY32 procEntry;
   procEntry.dwSize = sizeof(PROCESSENTRY32);
   Process32First(procSnap, &procEntry);
   procList += procEntry.szExeFile;
   procList += L"\r\n";
   while (Process32Next(procSnap, &procEntry))
   {
       procList += procEntry.szExeFile;
       procList += L"\r\n";
   }
   SetWindowText(listProc, procList.c_str());
   CloseHandle(procSnap);

2). Sample:

   HWND listProc = CreateWindowEx(0, L"Edit", L"", WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_HSCROLL| ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
       0, 0, 100, 300, process, NULL, NULL, NULL);

   HANDLE procSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

   std::wstringstream procList;
   PROCESSENTRY32 procEntry;
   procEntry.dwSize = sizeof(PROCESSENTRY32);
   Process32First(procSnap, &procEntry);
   procList += procEntry.szExeFile;
   procList += L"\r\n";
   while (Process32Next(procSnap, &procEntry))
   {
       procList << procEntry.szExeFile;
       procList << L"\r\n";
   }

   SetWindowText(listProc, procList.str().c_str());
   CloseHandle(procSnap);
Drake Wu
  • 6,927
  • 1
  • 7
  • 30
  • Thank you. You managed to answer my question and then read my mind with the new one I had. Not sure which to mark with acceptance so I chose a random one. –  Nov 18 '19 at 06:48
  • 2
    Also, you'll want to change your `while` loop into a `do/while` or you'll lose the info about the first process. – jkb Nov 18 '19 at 06:51