Adem Aygun
Adem Aygun

Reputation: 582

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.

Upvotes: 0

Views: 818

Answers (2)

Carsten Hansen
Carsten Hansen

Reputation: 1678

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.

Upvotes: 1

Trevor Balcom
Trevor Balcom

Reputation: 3898

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.");

Upvotes: 1

Related Questions