Solved

Using TerminateProcess()on a 16 Bit application

Posted on 1998-11-08
12
278 Views
Last Modified: 2013-12-03
I have an installation program that is written in win32 that needs to call a bunch of old win16 programs that will actually do the installation.  

I would like to give the user the ability to abort the intallation process as these win16 programs have been known to lock up.  But if I call TerminateProccess() on the 16 bit process handle it does not termiante the proccess.  The TerminateProccess Prcedure returns 0--indicating an error occured and GetLastError() returns 6--indicating that the handle is invalid.

However the handle is not invalid.  If I replace the 16 bit program EXEs with 32 bit "dummy" EXEs, then I can kill the 32 bit process fine (with the identical code--no need to even recompile.)  

Why can't I kill the 19 bit processes and is there a way to do so?

This was tested under NT only.  It needs to work in NT, 98, and 95.
0
Comment
Question by:nietod
  • 5
  • 5
  • 2
12 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 1415850
I'm using TerminateProcess to terminate 16bit application. But I'm doing it with win95/98. And, I'm using toolhelp functions to find a process called "winoa386".
So please try your solution on win95. If it doesn't work there - you'll have the wrong handle. Don't know why. Then please use toolhelp functions (or for NT use "psapi.dll")...

Regards, Madshi.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1415851
On NT, the best choice would be to terminate NTVDM.EXE - but as this also would kill all other 16bit apps, it is useless...
But what about spawning a 16bit 'terminator' that uses the good (?) ol' 16bit 'EnumTaskWindows()', 'IsTask()' and 'TerminateApp()' Win16 APIs (all you have to make sure is specifying 'CREATE_SHARED_VDM' whan calling 'CreateProcess()')? Just an idea...
0
 
LVL 22

Author Comment

by:nietod
ID: 1415852
Well this is for legacy stuff, so I'm not going to put too much work into it.  (And I don't even know if I could create a 16 bit app anymore.)  Do you know for sure that simple means like TerminateProcess() won't work?  (If so, answer and I'll call it quits, I'm not loosing sleep over this.)
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1415853
With Win95/98, TerminateProcess works (at least, if you have the right handle).
I don't know how it is with NT. I've something in mind that you can enumerate the dos processes with "psapi.dll", too. But I'm not so sure about it. However, I think, there are 32bit apis for this purpose in NT.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1415854
nietod, at least for NT there's a suitable solution for you (see, i'm a curious guy ;-)

/*
 *  compile: cl kill16.cpp /link kernel32.lib
 */

#include <windows.h>
#include <stdio.h>

long    main    (   int     argc,
                    char**  argv
                )
{
    BOOL                bRes;

    STARTUPINFO         si;
    PROCESS_INFORMATION pi;
    HANDLE              hProcess;

    ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

    si.cb   =   sizeof  (   STARTUPINFO);

    bRes    =   CreateProcess   (   NULL,
                                    "h:\\programs\\mahjongg\\mjwin.exe",
                                    NULL,
                                    NULL,
                                    FALSE,
                                    NORMAL_PRIORITY_CLASS | CREATE_SEPARATE_WOW_VDM,
                                    NULL,
                                    "h:\\programs\\mahjongg",
                                    &si,
                                    &pi
                                );

    if  (   bRes)
        {
            printf  (   "PH:0x%.8x TH:0x%.8x PID:%d TID:%d\n",
                        pi.hProcess,
                        pi.hThread,
                        pi.dwProcessId,
                        pi.dwThreadId
                    );

            Sleep   (   5000);

            if  (   !TerminateProcess   (   pi.hProcess,    0))
                {
                    printf  (   "Error:%d\n",   GetLastError    ());
                }
        }
     else   printf  (   "\n\tERROR creating process: %d\n\n",  
                        GetLastError    ()
                    );

    return  (   0);
}

NOTE that 'CREATE_SEPARATE_WOW_VDM' apparenty did the trick...

(well, found no other 16bit app than 'mahjongg' <lol>)
0
 
LVL 22

Author Comment

by:nietod
ID: 1415855
Great.  I will try that in an hour or two.  If it works (it should) I'll let know know...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 86

Expert Comment

by:jkr
ID: 1415856
And - does it work (well, it should...) ?
0
 
LVL 22

Author Comment

by:nietod
ID: 1415857
By an hour or two, I meant a programmer's hour or two.  I'll let you know by march...
0
 
LVL 86

Expert Comment

by:jkr
ID: 1415858
;-)
Fine - take you time, i'll leave for home now anyway ( it's almost 10pm here, which is late even for me <s>)
0
 
LVL 22

Author Comment

by:nietod
ID: 1415859
jkr, that did the trick!.  And look it only took me 5 (programmer) minutes to test it.

answer away
0
 
LVL 86

Accepted Solution

by:
jkr earned 70 total points
ID: 1415860
OK - glad it worked ;-)
0
 
LVL 22

Author Comment

by:nietod
ID: 1415861
Adjusted points to 75
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now