简体   繁体   中英

How to kill process on Windows Mobile 6.5

I use ProcessCE .

It works fine on Windows mobile 6.1. But on Windows Mobile 6.5 -> When I kill with Terranova.API TerminateProcess throws exception with error 6 = ERROR_INVALID_HANDLE.

internal static void KillProcess(IntPtr pid)
    {

        IntPtr process_handle = OpenProcess(PROCESS_TERMINATE, false, (int)pid);

        if (process_handle == (IntPtr)INVALID_HANDLE_VALUE)
            throw new Win32Exception(Marshal.GetLastWin32Error(), "OpenProcess failed.");

        try
        {
            bool result = TerminateProcess(process_handle, 0);

            if (result == false)
                throw new Win32Exception(Marshal.GetLastWin32Error(), "TerminateProcess failed."); //THROW EXCEPTION on Windows Mobile 6.5

        }
        finally
        {
            CloseHandle(process_handle);
        }
    }

Please help.

The code is incorrectly checking OpenProcess() for failure. The documentation for OpenProcess states the return value when the function fails is NULL . In C, NULL is just a macro that expands to 0 , so in C# you should use IntPtr.Zero in place of NULL when referencing Win32 API.

If a process did indeed have the handle of INVALID_HANDLE_VALUE then this code would throw an exception when there was no error condition.

Win32 API uses HANDLE inconsistently. In some cases the function returns NULL on failure, in most cases the function returns INVALID_HANDLE_VALUE on error. This is one of the cases where the function returns NULL rather than INVALID_HANDLE_VALUE to indicate failure.

if (process_handle == IntPtr.Zero)
            throw new Win32Exception(Marshal.GetLastWin32Error(), "OpenProcess failed.");

One reason why the HANDLE returned from OpenProcess is invalid might be that the fdwAccess parameter is not supported in WM6.5 and should be set to 0 .

Other than that, check the return value for NULL (not INVALID_HANDLE_VALUE), and verify that the pid is valid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM