如何在Windows Mobile 6.5

时间:2015-08-10 14:54:07

标签: c# c++ windows-mobile compact-framework windows-mobile-6

我使用ProcessCE

在Windows Mobile 6.1上运行正常。 但在Windows Mobile 6.5上 - >当我使用Terranova.API终止时,TerminateProcess抛出异常,错误为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);
        }
    }

请帮忙。

2 个答案:

答案 0 :(得分:1)

代码错误地检查OpenProcess()是否有失败。 OpenProcess的文档说明函数失败时的返回值是NULL。在C中,NULL只是一个扩展为0的宏,因此在引用Win32 API时,您应该使用IntPtr.Zero代替NULL

如果某个进程确实具有INVALID_HANDLE_VALUE的句柄,则此代码会在没有错误条件时抛出异常。

Win32 API使用HANDLE不一致。在某些情况下,函数在失败时返回NULL,在大多数情况下,函数在出错时返回INVALID_HANDLE_VALUE。这是函数返回NULL而不是INVALID_HANDLE_VALUE来指示失败的情况之一。

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

答案 1 :(得分:1)

从OpenProcess返回的HANDLE无效的一个原因可能是WM6.5和should be set to 0不支持fdwAccess参数。

除此之外,检查NULL的返回值(不是INVALID_HANDLE_VALUE),并验证pid是否有效。