home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Troubleshooting Netware Systems
/
CSTRIAL0196.BIN
/
attach
/
msj
/
v10n05
/
dqa0595.exe
/
HVM.C
next >
Wrap
C/C++ Source or Header
|
1995-05-01
|
4KB
|
159 lines
/*
* Windows program that uses an MS-DOS app running in a hidden VM
* to help out with its chores.
*/
#include <windows.h>
#include <toolhelp.h>
#define ID_BUTTON 100
long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
BOOL FAR PASCAL NotifyCallBackProc (WORD, DWORD);
HWND hwndGlobal;
/*
* Function WinMain.
*/
int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
static char szAppName[] = "HVM";
WNDCLASS wc;
HWND hwnd;
MSG msg;
if (!hPrevInstance) {
wc.style = 0;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = COLOR_WINDOW + 1;
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
RegisterClass (&wc);
}
hwnd = CreateWindow (szAppName, szAppName, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
HWND_DESKTOP, NULL, hInstance, NULL);
hwndGlobal = hwnd;
ShowWindow (hwnd, nCmdShow);
UpdateWindow (hwnd);
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
/*
* WndProc processes messages to the main window.
*/
long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
{
TASKENTRY te;
static HTASK hTask;
static LPFNNOTIFYCALLBACK lpfnNotifyProc;
static HINSTANCE hInstance, hvm;
BOOL bResult;
switch (message) {
case WM_CREATE:
//
// Save the instance handle and create a push button.
//
hInstance = ((LPCREATESTRUCT) lParam)->hInstance;
CreateWindow ("button", "Do It!", WS_CHILD | WS_VISIBLE |
BS_PUSHBUTTON, 16, 16, 144, 80, hwnd, ID_BUTTON, hInstance, NULL);
return 0;
case WM_COMMAND:
if (wParam == ID_BUTTON) {
//
// Register to receive ToolHelp notifications.
//
lpfnNotifyProc = (LPFNNOTIFYCALLBACK) MakeProcInstance
(NotifyCallBackProc, hInstance);
if (!NotifyRegister (NULL, lpfnNotifyProc, NF_NORMAL))
MessageBox (hwnd, "NotifyRegister failed", "Error", MB_OK);
//
// Launch the MS-DOS program from a PIF file. (Be sure to
// specify that the program will be run in a window.)
//
if ((hvm = (HINSTANCE) WinExec
("c:\\samples\\upcase.pif c:\\samples\\anyfile.c",
SW_HIDE)) < 32) {
NotifyUnRegister (NULL);
FreeProcInstance (lpfnNotifyProc);
MessageBox (hwnd, "WinExec failed", "Error", MB_OK);
return 0;
}
//
// Use the instance handle returned by WinExec to get
// the task handle of the program that was just launched.
//
te.dwSize = sizeof (te);
bResult = TaskFirst (&te);
while (bResult) {
if (te.hInst == hvm) {
hTask = te.hTask;
return 0;
}
bResult = TaskNext (&te);
}
MessageBox (hwnd, "Task not found", "Error", MB_OK);
return 0;
}
break;
case WM_USER:
//
// See if the MS-DOS app is done.
//
if (wParam == (WPARAM) hTask) {
NotifyUnRegister (NULL);
FreeProcInstance (lpfnNotifyProc);
MessageBox (hwnd, "Done!", "Notification Received", MB_OK);
return 0;
}
break;
case WM_DESTROY:
//
// Terminate the program.
//
PostQuitMessage (0);
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
/*
* NotifyCallBackProc processes ToolHelp notifications.
*/
BOOL FAR PASCAL NotifyCallBackProc (WORD wID, DWORD dwData)
{
if (wID == NFY_EXITTASK)
PostMessage (hwndGlobal, WM_USER, (WPARAM) GetCurrentTask (), 0);
return FALSE;
}