日期:2014-05-17  浏览次数:21063 次

c#调用API(GetModuleFileName)获取程序路径返回乱码问题
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
        public const int PROCESS_ALL_ACCESS = 0x000F0000 | 0x00100000 | 0xFFF;
        [DllImport("Kernel32.dll")]
        public extern static IntPtr OpenProcess(int fdwAccess, int fInherit, int IDProcess);
        [DllImport("kernel32.dll", EntryPoint = "GetModuleFileName")]
        private static extern uint GetModuleFileName(IntPtr hModule, [Out] StringBuilder lpszFileName, int nSize);

        //通过坐标获取程序句柄
         int x = Cursor.Position.X;
        int y = Cursor.Position.Y;
        Point p = new Point(x,y);
        //获取句柄
        winHandle = WindowFromPoint(p);
        //获取窗口进程ID
        GetWindowThreadProcessId(winHandle, out winProcessID);
        //通过进程ID获取进程句柄
         IntPtr processHandle = IntPtr.Zero;
        processHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, winProcessID);
        //通过进程句柄获取进程主程序路径
         winExePath = new StringBuilder(260);
        GetModuleFileName(processHandle, winExePath, winExePath.Capacity);

获取的程序路径信息在winExePath中,显示出来是乱码,求解决



------解决方案--------------------
try

 GetWindowThreadProcessId(winHandle, out winProcessID);
=>
winProcessID= GetWindowThreadProcessId(winHandle, out winProcessID);