简体   繁体   English

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

[英]PInvoke.Net in C#, PostMessage method not accepting FindWindow (IntPtr hWnd) handle Pointer

I need to figure out a safe way to write code to find a folder and close it. 我需要找出一种安全的方法来编写代码以查找文件夹并关闭它。 I am getting an error on PostMessage method because IntPtr cannot be assigned to HandleRef object. 我在PostMessage方法上遇到错误,因为IntPtr无法分配给HandleRef对象。 I am clueless on how to fix this issue. 对于如何解决此问题,我一无所知。

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


        }
    }
}

Thanks Idle_mind, your suggestion worked. 感谢Idle_mind,您的建议有效。 Here is the updated code. 这是更新的代码。

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




        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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