C#中的PInvoke.Net,PostMessage方法不接受FindWindow(IntPtr hWnd)处理指针

时间:2015-01-06 21:22:56

标签: c# console-application pinvoke

我需要找出一种安全的方法来编写代码来查找文件夹并关闭它。我在PostMessage方法上收到错误,因为无法将IntPtr分配给HandleRef对象。我对如何解决这个问题毫无头绪。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);


        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool PostMessage(HandleRef hWnd, uint msg, IntPtr wParam, IntPtr lParam);


        private const int WM_CLOSE = 0xF060;

        void PostMessageSafe(HandleRef hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            bool returnValue = PostMessage(hWnd, msg, wParam, lParam);
            if (!returnValue)
            {

                Console.WriteLine("Some error occurred.");

            }
        }    
        static void Main(string[] args)
        {

            var path = @"C:\deleteme\";
            Process.Start(path);

            IntPtr hWnd = FindWindow(null, "deleteme");

            if (hWnd != IntPtr.Zero)
            {
                PostMessageSafe(hWnd, WM_CLOSE, 0, 0); // Error
                Console.Write("Success!\n");
                Console.ReadLine();

            }
            else
            {
                Console.Write("Error Folder not found!\n");
                Console.ReadLine();
            }


        }
    }
}

1 个答案:

答案 0 :(得分:1)

感谢Idle_mind,您的建议有效。这是更新的代码。

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace ConsoleApplication2
{
    class Program
    {

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);


        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

        private const int WM_CLOSE = 0x10;

        static void PostMessageSafe(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            bool returnValue = PostMessage(hWnd, msg, wParam, lParam);
            if (!returnValue)
            {

                Console.WriteLine("Some error occurred.");

            }
        }    
        static void Main()
        {

            var path = @"C:\deleteme\";
            Process.Start(path);

            Thread.Sleep(5000); // Give the folder some time to load within the OS
            IntPtr hWnd = FindWindow(null, "deleteme");

            if (hWnd != IntPtr.Zero)
            {
                PostMessageSafe(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
                Console.Write("Success!\n");
                Console.ReadLine();

            }
            else
            {
                Console.Write("Error Folder not found!\n");
                Console.ReadLine();
            }




        }
    }
}