Тема: hwnd --> pid
Показать сообщение отдельно
Старый 31.08.2016, 20:42   #1  
Logger is offline
Logger
Участник
Лучший по профессии 2015
Лучший по профессии 2014
 
3,972 / 3268 (116) ++++++++++
Регистрация: 12.10.2004
Адрес: Москва
Записей в блоге: 2
hwnd --> pid
Привет всем.

Есть метод
X++:
static container GetWindowThreadProcessId(HWND _hwnd)
{
    container   ret = [0.0];
    DLL         _DLL             = new DLL('USER32');
    DLLFunction dllFunction      = new DLLFunction(_DLL, 'GetWindowThreadProcessId');
    Binary      lpdwProcessId    = new Binary(#SizeOfInt);
    int         pid;
    ;
    dllFunction.returns(ExtTypes::DWord); // DWORD WINAPI
    dllFunction.arg(ExtTypes::DWord);     // _In_      HWND    hWnd,
    dllFunction.arg(ExtTypes::Pointer);   // _Out_opt_ LPDWORD lpdwProcessId

    pid = dllFunction.call(_hwnd, lpdwProcessId);
    if (pid)
    {
        ret = [pid, lpdwProcessId.dWord(0)];
    }
    return ret;
}
/*
[url]https://msdn.microsoft.com/en-us/library/windows/desktop/ms633522%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396[/url]

DWORD WINAPI GetWindowThreadProcessId(
  _In_      HWND    hWnd,
  _Out_opt_ LPDWORD lpdwProcessId
);

Parameters

hWnd [in]

    Type: HWND

    A handle to the window.
lpdwProcessId [out, optional]

    Type: LPDWORD

    A pointer to a variable that receives the process identifier. If this parameter is not NULL, GetWindowThreadProcessId copies the identifier of the process to the variable; otherwise, it does not.

Return value

Type:

Type: DWORD

The return value is the identifier of the thread that created the window.
*/


Он позволяет по hwnd окна определить pid.
Мы его используем для определения pid процесса excel c которым работаем через com или .net (com объект возвращает hwnd через который мы данным методом получаем pid). Соответственно если есть куча Excel то можно найти концы - понять какой com объект породил проблему и оставил зависший Excel после построения отчета.

Хочется переписать его на .Net
Может кто-нибудь подскажет как этот код переписать так, чтобы он работал на сервере?