This example program shows how a Windows application can receive a Windows job control notification from the LSF system.
Catching the notification messages involves:
Do not use DispatchMessage() to dispatch the message, since it is addressed to the thread, not the window. This program displays information in its main window, and waits for SIGTERM. Once SIGTERM is received, it posts a quit message and exits. A real program could do some cleanup when the SIGTERM message is received.
/* WINJCNTL.C */#include <windows.h>#include <stdio.h>#define BUFSIZE 512static UINT msgSigTerm;static int xpos;static int pid_ypos;static int tid_ypos;static int msg_ypos;static int pid_buf_len;static int tid_buf_len;static int msg_buf_len;static char pid_buf[BUFSIZE];static char tid_buf[BUFSIZE];static char msg_buf[BUFSIZE];LRESULT WINAPI MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){HDC hDC;PAINTSTRUCT ps;TEXTMETRIC tm;switch (msg) {case WM_CREATE:hDC = GetDC(hWnd);GetTextMetrics(hDC, &tm);ReleaseDC(hWnd, hDC);xpos = 0;pid_ypos = 0;tid_ypos = pid_ypos + tm.tmHeight;msg_ypos = tid_ypos + tm.tmHeight;break;case WM_PAINT:hDC = BeginPaint(hWnd, &ps);TextOut(hDC, xpos, pid_ypos, pid_buf, pid_buf_len);TextOut(hDC, xpos, tid_ypos, tid_buf, tid_buf_len);TextOut(hDC, xpos, msg_ypos, msg_buf, msg_buf_len);EndPaint(hWnd, &ps);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hWnd, msg, wParam, lParam);}return 0;}int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow){ATOM rc;WNDCLASS wc;HWND hWnd;MSG msg;/* Create and register a windows class */if (hPrevInstance == NULL) {wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;wc.lpfnWndProc = MainWndProc;wc.cbClsExtra = 0;wc.cbWndExtra = 0;wc.hInstance = hInstance;wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);wc.hCursor = LoadCursor(NULL, IDC_ARROW);wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);rc = RegisterClass(&wc);}/* Register the message we want to catch */msgSigTerm = RegisterWindowMessage("SIGTERM");/* Format some output for the main window */sprintf(pid_buf, "My process ID is: %d", GetCurrentProcessId());pid_buf_len = strlen(pid_buf);sprintf(tid_buf, "My thread ID is: %d", GetCurrentThreadId());tid_buf_len = strlen(tid_buf);sprintf(msg_buf, "Message ID is: %u", msgSigTerm);msg_buf_len = strlen(msg_buf);/* Create the main window */hWnd = CreateWindow("WinJCntlClass","Windows Job Control Demo App",WS_OVERLAPPEDWINDOW,0,0,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);ShowWindow(hWnd, nCmdShow);/* Enter the message loop, waiting for msgSigTerm. When we getit, just post a quit message */while (GetMessage(&msg, NULL, 0, 0)) {if (msg.message == msgSigTerm) {PostQuitMessage(0);} else {TranslateMessage(&msg);DispatchMessage(&msg);}}return msg.wParam;}