Getting the 3ds Max Main Window

Prior to 3ds Max 2018, external applications could obtain the HWND of the 3ds Max main window by looking for an active window with the class name of 3DSMAX. As of 3ds Max 2018, the main window is a Qt window, and requires a different method to access.

The maxsdk\include\Process_Data_3DSMAX.h exposes the FindProcessMainWindow() function you can use to get the main window handle for the first running process with the specified version number. Here is an example of how to use this function:

// Utility function to get the process id of a running session of 3ds Max.
// Finds the first 3ds Max session that has the specified max_version number, unless
// max_version is 0 in which the first 3ds Max session is used.
// The main HWND of the 3ds Max session is returned through max_hwnd.
// This function only works on sessions of 3ds Max 2018 and higher.
//
DWORD Get3dsMaxProcessHandle(DWORD max_version, HWND &max_hwnd)
{
    PROCESSENTRY32 entry;
    entry.dwSize = sizeof(PROCESSENTRY32);

    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    DWORD result = 0;

    if (Process32First(snapshot, &entry) == TRUE) {
        while (Process32Next(snapshot, &entry) == TRUE) {
            if (_wcsicmp(entry.szExeFile, L"3dsmax.exe") == 0) {
                DWORD process_max_version = 0;
                HWND process_max_hwnd = MaxSDK::Util::FindProcessMainWindow(entry.th32ProcessID, process_max_version);
                if (process_max_hwnd && (max_version == 0 || max_version == process_max_version))
                {
                    max_hwnd = process_max_hwnd;
                    result = entry.th32ProcessID;
                    break;
                }
            }
        }
    }

    CloseHandle(snapshot);
    return result;
}