shellexecute.cs

来自「POCKET PC,照片管理系统!提供了真正意义上的目录打开功能」· CS 代码 · 共 60 行

CS
60
字号
using System;
using System.Runtime.InteropServices;
using Addot.Internals;

namespace Addot.Win32
{
	public class ShellExecute
	{
        private struct SHELLEXECUTEEX
        {
            public UInt32 cbSize; 
            public UInt32 fMask; 
            public IntPtr hwnd; 
            public IntPtr lpVerb; 
            public IntPtr lpFile; 
            public IntPtr lpParameters; 
            public IntPtr lpDirectory; 
	
            public int nShow; 
            public IntPtr hInstApp; 
            // Optional members 
            public IntPtr lpIDList; 
            public IntPtr lpClass; 
            public IntPtr hkeyClass; 
            public UInt32 dwHotKey; 
            public IntPtr hIcon; 
            public IntPtr hProcess; 
        }

        [DllImport("coredll")]
        private extern static int ShellExecuteEx( ref SHELLEXECUTEEX ex );

        public static void Execute(string fileName)
		{
            int size = fileName.Length * 2 + 2;
            IntPtr data = Memory.LocalAlloc(0x40, size);
            Marshal.Copy(System.Text.Encoding.Unicode.GetBytes(fileName), 0, data, size - 2);

            SHELLEXECUTEEX shellExe = new SHELLEXECUTEEX();
            shellExe.cbSize = (uint)Marshal.SizeOf(shellExe);
            shellExe.dwHotKey = 0;
            shellExe.fMask = 0;
            shellExe.hIcon = IntPtr.Zero;
            shellExe.hInstApp = IntPtr.Zero;
            shellExe.hProcess = IntPtr.Zero;;
            shellExe.lpClass = IntPtr.Zero;
            shellExe.lpDirectory = IntPtr.Zero;
            shellExe.lpIDList = IntPtr.Zero;
            shellExe.lpParameters = IntPtr.Zero;
            shellExe.lpVerb = IntPtr.Zero;
            shellExe.nShow = 0;
            shellExe.lpFile = data;

            ShellExecuteEx(ref shellExe);

            Memory.LocalFree(data);
        }
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?