PInvoke.Net 在 C# 中,PostMessage 方法不接受 FindWindow (IntPtr hWnd

本文关键字:FindWindow 不接受 IntPtr hWnd 方法 PostMessage Net PInvoke | 更新日期: 2023-09-27 18:36:25

我需要找出一种安全的方式来编写代码来查找文件夹并关闭它。我在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();
            }

        }
    }
}

PInvoke.Net 在 C# 中,PostMessage 方法不接受 FindWindow (IntPtr hWnd

谢谢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();
            }


        }
    }
}