简体   繁体   中英

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




        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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