x86としてコンパイルされたac#アプリケーションがあるので、Windows7x64で32ビットアプリケーションとして実行されます。アプリケーションの実行中に、アクティブなウィンドウの実行可能ファイル名を検出する必要があります。Winodws XPでは、次のコードが正常に機能しました(アクティブなウィンドウハンドルからプロセスファイル名を取得します)。x64では、32ビットプロセスの名前のみが報告されます(返されたデータをチェックしていないため、他のプロセスのガベージが返されます)。GetForegroundWindowAPIで取得したアクティブウィンドウのハンドルを渡します。
public static string GetProcessPathFromWindowHandle(IntPtr hWnd) {
string filename = string.Empty;
uint pid=0;
Unmanaged.GetWindowThreadProcessId(hWnd, out pid);
//error in Win64: returns strange characters for Win64 files
const int nChars = 1024;
StringBuilder filenameBuffer = new StringBuilder(nChars);
IntPtr hProcess = Unmanaged.OpenProcess(1040, 0, pid);
Unmanaged.GetModuleFileNameEx(hProcess, IntPtr.Zero, filenameBuffer, nChars);
Unmanaged.CloseHandle(hProcess);
filename = filenameBuffer.ToString();
//Get the name of the Windows
int length = Unmanaged.GetWindowTextLength(hWnd);
StringBuilder sb = new StringBuilder(length + 1);
Unmanaged.GetWindowText(hWnd, sb, sb.Capacity);
Logger.Main.LogMessage("Window Title is: " + sb);
Logger.Main.LogMessage("Process filename is: " + filename);
return filename;
}
その情報を64ビット環境で32ビットプロセスから取得できますか?ありがとう。アンドレア