2013-10-27 7 views
0

나는 몇 시간 동안 인터넷 검색을하고 찾은 모든 것을 시도했지만 작동하지 않는 것 같습니다.창 파일 이름 가져 오기가 작동하지 않습니다.

내 코드는 다음과 같습니다

private static long _i; 
private static readonly System.Timers.Timer Timer = new System.Timers.Timer(1000); 

[DllImport("user32.dll")] 
private static extern IntPtr GetForegroundWindow(); 

[DllImport("user32.dll")] 
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); 

[DllImport("user32.dll")] 
private static extern int GetWindowModuleFileName(IntPtr hWnd, StringBuilder text, int count); 

static void Main() 
{ 
    Timer.Elapsed += Timer_Elapsed; 
    Timer.AutoReset = true; 
    Timer.Start(); 
    while (Timer.Enabled) 
     Console.ReadLine(); 
} 

static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
{ 
    Console.WriteLine("-|- - " + _i + " - -|-"); 
    Console.WriteLine("WindowHandle: " + GetForegroundWindow()); 
    var buff = new StringBuilder(2048); 
    GetWindowModuleFileName(GetForegroundWindow(), buff, 1024); 
    Console.WriteLine("ModuleName: " + Path.GetFileName(buff.ToString())); 
    Console.WriteLine("-|- - | - -|-"); 
    _i++;  
} 

이 코드의 출력은 항상이 같은 것입니다 :

-|- - 1 - -|- 
WindowHandle: 8128962 
ModuleName: ConsoleApplication.vshost.exe 
-|- - | - -|- 

하지만 크롬 또는 피들러와 같은 다른 응용 프로그램을 대상으로하는 경우에도, 항상 출력합니다 내 실행 파일 이름 또는 파일 이름 없음. 누락 된 문제가 있습니까?

+0

의 C#을 구현하면 경로 등 무엇처럼 ... 더 많은 코드를 줄 수있다? – Alker

+0

@Alker 경로가 무엇을 의미합니까? 그것은 모든 관련 코드 여야합니다. – therealtbs

+0

오, 내 비주얼 스튜디오가 도청했다 ... – Alker

답변

1

를 사용합니다. foregoundwindow에 속한 프로세스를 얻어야합니다. 이 작업을 수 있습니다

[Flags] 
enum ProcessAccessFlags : uint 
{ 
    All = 0x001F0FFF, 
    Terminate = 0x00000001, 
    CreateThread = 0x00000002, 
    VMOperation = 0x00000008, 
    VMRead = 0x00000010, 
    VMWrite = 0x00000020, 
    DupHandle = 0x00000040, 
    SetInformation = 0x00000200, 
    QueryInformation = 0x00000400, 
    Synchronize = 0x00100000 
} 

    [DllImport("user32.dll")] 
    private static extern IntPtr GetForegroundWindow(); 

    [DllImport("user32.dll")] 
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); 

    [DllImport("user32.dll")] 
    private static extern int GetWindowModuleFileName(IntPtr hWnd, StringBuilder text, int count); 

    [DllImport("user32.dll")] 
    private static extern IntPtr GetTopWindow(IntPtr hWnd); 

    [DllImport("user32.dll")] 
    private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint pid); 

    [DllImport("kernel32.dll")] 
    static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwProcessId); 

    [DllImport("psapi.dll")] 
    static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In] [MarshalAs(UnmanagedType.U4)] int nSize); 


    [DllImport("kernel32.dll", SetLastError=true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool CloseHandle(IntPtr hObject); 

    static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
    { 
     Console.WriteLine("-|- - " + _i + " - -|-"); 
     Console.WriteLine("WindowHandle: " + GetForegroundWindow()); 
     uint pid; 
     GetWindowThreadProcessId(GetForegroundWindow(), out pid); 
     IntPtr proc = OpenProcess(ProcessAccessFlags.VMRead | ProcessAccessFlags.QueryInformation, true, pid); 
     var buff = new StringBuilder(2048); 
     GetModuleFileNameEx(proc, (IntPtr) 0, buff, 1024); 
     Console.WriteLine("ModuleName: " + Path.GetFileName(buff.ToString())); 
     Console.WriteLine("-|- - | - -|-"); 
     _i++; 
     CloseHandle(proc); 
    } 

이 기본적으로 this C answer

1

당신이 하나 System.Management에 대한 참조를 추가해야합니다 - 그것은 당신이 하나 추가 단계를 확인해야합니다 WMI

using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.IO; 
using System.Linq; 
using System.Management; 
using System.Runtime.InteropServices; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     private static long _i; 
     private static readonly System.Timers.Timer Timer = new System.Timers.Timer(1000); 
     [DllImport("user32.dll")] 
     private static extern IntPtr GetForegroundWindow(); 

     [DllImport("user32.dll")] 
     private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); 

     [DllImport("user32.dll")] 
     private static extern int GetWindowModuleFileName(IntPtr hWnd, StringBuilder text, int count); 

     [DllImport("user32.dll", SetLastError = true)] 
     public static extern int GetWindowThreadProcessId(IntPtr hwnd, ref int lpdwProcessId); 

     static void Main() 
     { 


     Timer.Elapsed += Timer_Elapsed; 
     Timer.AutoReset = true; 
     Timer.Start(); 
     while (Timer.Enabled) 
      Console.ReadLine(); 
     } 

     static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
     { 
     Console.WriteLine("-|- - " + _i + " - -|-"); 
     Console.WriteLine("WindowHandle: " + GetForegroundWindow()); 

     string name = GetProcessEXENameUsingWMI(GetProcess(GetForegroundWindow())); 

     Console.WriteLine("ModuleName: " + Path.GetFileName(name)); 

     Console.WriteLine("-|- - | - -|-"); 
     _i++; 

     } 

     private static string GetProcessEXENameUsingWMI(Process p) 
     { 
     ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT ExecutablePath FROM Win32_Process WHERE ProcessId=" + p.Id.ToString()); 
     ManagementObjectCollection moc = mos.Get(); 

     foreach (ManagementObject mo in moc) 
     { 
      string executablePath = mo[ "ExecutablePath" ].ToString(); 
      return executablePath; 
     } 

     return ""; 
     } 

     public static Process GetProcess(IntPtr hwnd) 
     { 
     int intID = 0; 
     GetWindowThreadProcessId(hwnd, ref intID); 
     return Process.GetProcessById(intID); 
     } 

    } 
} 
관련 문제