Search code examples
javascriptgoogle-chromegoogle-chrome-extensionwindow-handles

How to get window handle (int) from chrome extension?


I got a chrome extension and in the event of a new tab I want to get the window handle in windows if the current window.

On the event I got the tab object and I got chrome's internal window id but this is not the handle in windows.

chrome.tabs.onCreated.addListener(
        function (tab)
        {
            var intMainWindowHwnd = 0; // how to get it? not tab.windowId…
        });

Thanks!


Solution

  • Well, if anyone encounter the same problem, i solved it using a NPAPI Plugin in C++ that get access to win32api...

    In Invoke method i've checked for my method (GetProcessId) and got the parent process (since the addon is in a different process):

    ULONG_PTR MyAddon::GetParentProcessId() // By Napalm @ NetCore2K
    {
     ULONG_PTR pbi[6];
     ULONG ulSize = 0;
     LONG (WINAPI *NtQueryInformationProcess)(HANDLE ProcessHandle, ULONG ProcessInformationClass,
      PVOID ProcessInformation, ULONG ProcessInformationLength, PULONG ReturnLength); 
     *(FARPROC *)&NtQueryInformationProcess = 
      GetProcAddress(LoadLibraryA("NTDLL.DLL"), "NtQueryInformationProcess");
     if(NtQueryInformationProcess){
      if(NtQueryInformationProcess(GetCurrentProcess(), 0,
        &pbi, sizeof(pbi), &ulSize) >= 0 && ulSize == sizeof(pbi))
         return pbi[5];
     }
     return (ULONG_PTR)-1;
    }
    

    Then i got the main hwnd of this process and return it to my js addon.