Programmatically get DISPLAY number

The following code fragment in C++ will search for the first 20 display numbers. A mutex is created for each display named XWin32-display-###


int main(int argc, char **argv)
int i;
for (i = 0; i < 20; i++) { HANDLE mutex; char temp[1024]; /* * Using the "Local" address space restricts the search to the * active terminal server session. "Global" will find servers * from other sessions too. */ //snprintf(temp, sizeof(temp), "Global\\XWin32-display-%d", i); snprintf(temp, sizeof(temp), "Local\\XWin32-display-%d", i); mutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, temp); if (mutex != NULL) { CloseHandle(mutex); printf("Found display %d\n", i); break; } } }