• entries
    707
  • comments
    1173
  • views
    434144

Win32 process management

Sign in to follow this  

95 views

So, I decided to investigate something that I plan on using on a side project for the future and it turned out to be a huge problem (actually, I'm pretty sure I spent more time making it into a DLL than I did actually coming up with the actual code.)

Anyway, what I wanted to do was be able to send messages to a process that is already running, but the project will only know what the process' exe filename is.

Thanks to Ra, I got my heading and took off. The ending result is my ProcessManager class. Here's an example that enumerates the running processes and adds them to a list box in my window:

#include
#include "ProcessManager.h"
#include "crtdbg.h"

HWND WindowHandle = 0, ListBoxHandle = 0;
Process::EntryList Processes;
Process::Manager ProcessMgr;

LRESULT CALLBACK WindowProcedure(HWND WindowHandle, unsigned int Message, __w64 unsigned int Param1, __w64 long Param2)
{
switch(Message)
{
case WM_CLOSE:
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(WindowHandle, Message, Param1, Param2);
}

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
#ifdef _DEBUG
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
_CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
#endif

if(!ProcessMgr.Create())
return 0;

WNDCLASS WndClass;
ZeroMemory(&WndClass, sizeof(WNDCLASS));
WndClass.hInstance = GetModuleHandle(0);
WndClass.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
WndClass.hIcon = LoadIcon(0, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(0, IDC_ARROW);
WndClass.lpfnWndProc = WindowProcedure;
WndClass.lpszClassName = "DBeals Task Manager";
if(!RegisterClass(&WndClass))
return 0;

WindowHandle = CreateWindowEx(0, WndClass.lpszClassName, "DBeals Task Manager", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 5, 5, 407, 318, 0, 0, WndClass.hInstance, 0);
if(!WindowHandle)
return 0;

ListBoxHandle = CreateWindowEx(0, "listbox", "", WS_CHILD | WS_VISIBLE | WS_VSCROLL, 0, 0, 400, 300, WindowHandle, (HMENU)1001, WndClass.hInstance, 0);
if(!ListBoxHandle)
return 0;

Process::EntryList Entries;
Entries = ProcessMgr.EnumerateProcesses();

for(Process::EntryList::iterator Itor = Entries.begin(); Itor != Entries.end(); ++Itor)
{
SendMessage(ListBoxHandle, LB_ADDSTRING, 0, (LPARAM)(*Itor)->szExeFile);
}

MSG Msg;
ZeroMemory(&Msg, sizeof(MSG));
while(GetMessage(&Msg, 0, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return 0;
}



ProcessManager contains 6 functions at the time (I was just investigating atm, I'll add more later):

EntryPointer GetProcessByName(const StringType &Name);
EntryPointer GetNextProcessByName(const StringType &Name, EntryPointer Last);
EntryList GetProcessesByName(const StringType &Name, EntryList &Output);
EntryList EnumerateProcesses();
HWND GetWindowFromProcessID(DWORD ProcessID);
HWND GetWindowFromProcessEntry(EntryPointer Entry);



I'll upload the header and dll file later if anyone is interested. I need to fix it right now; I'm using smart pointers in the code and it's causing issues when I free the library (obviously, but I wasn't planning on turning it into a dll when I started it.



For those of you that are using the newer versions of Microsoft Visual C++ and are wanting to make DLL files that export C++ functions without DEF files, extern "C", etc:

#define EXPORT comment(linker, "/EXPORT:"__FUNCTION__"="__FUNCDNAME__)

__declspec(dllexport) void FunctionToExport(/*Arguments Here*/)
{
#pragma EXPORT
// function body here
}





I'm getting somewhere with SuperRPG, but at the moment I'm not quite sure where "somewhere" is lol. Just wanted to let you guys know I am still working on it and I'm spending most of my time coding working on it. The above is actually the first time I've deviated from it in a while.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now