2011-04-29 8 views
1

일부 Java 코드를 작성했습니다.JNA를 사용하여 크롬을 실행하는 방법?

Windows (32 비트)에서 JNA를 사용하여 크롬을 실행하는 방법.

나는 그걸 알기를 좋아한다.

아시겠지만 FindWindow는 간단한 해결책이지만 크롬이 실행되지 않으면 작동하지 않습니다. 같은 코드 아래

FindWindow example

가능할까요?

HWND hwnd = User32.CreateProcess(...); 
코드 공개 크롬 아래

. 크기 조정, 최대화가 작동하지 않습니다.


public interface Kernel32 extends StdCallLibrary { 
    Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class); 

    boolean CreateProcessA(
      String lpApplicationName 
      , String lpCommandLine 
      , Structure lpProcessAttributes 
      , Structure lpThreadAttributes 
      , boolean bInheritHandles 
      , int dwCreationFlags 
      , Structure lpEnvironment 
      , String lpCurrentDirectory 
      , Structure lpStartupInfo 
      , Structure lpProcessInformation); 
} 

import com.sun.jna.Pointer; 
import com.sun.jna.Structure; 

public class ProcessInformation extends Structure { 
    public Pointer hProcess; 
    public Pointer hThread; 
    public int dwProcessId; 
    public int dwThreadId; 
} 

import com.sun.jna.Pointer; 
import com.sun.jna.Structure; 
import com.sun.jna.WString; 

public class StartupInfoA extends Structure { 
    public int cb; 
    public WString lpReserved; 
    public WString lpDesktop; 
    public WString lpTitle; 
    public int dwX; 
    public int dwY; 
    public int dwXSize; 
    public int dwYSize; 
    public int dwXCountChars; 
    public int dwYCountChars; 
    public int dwFillAttribute; 
    public int dwFlags; 
    public short wShowWindow; 
    public short cbReserved2; 
    public Pointer lpReserved2; 
    public Pointer hStdInput; 
    public Pointer hStdOutput; 
    public Pointer hStdError; 
} 

public class Test { 
    public static void main(String[] args) { 

     int STARTF_USEPOSITION = 0x00000004; 
     int STARTF_USESIZE = 0x00000002; 
     int STARTF_USESHOWWINDOW = 0x00000001; 

     ProcessInformation processInformation = new ProcessInformation(); 
     StartupInfoA startupInfo = new StartupInfoA(); 
     startupInfo.dwX = 100; 
     startupInfo.dwY = 100; 
     startupInfo.dwXSize = 100; 
     startupInfo.dwYSize = 100;  
     startupInfo.wShowWindow = (short) SW_MAXIMIZE; 
     startupInfo.dwFlags = STARTF_USEPOSITION | STARTF_USESIZE; 

     Kernel32.INSTANCE.CreateProcessA(new String("C:\\Users.....\\Google\\Chrome\\Application\\chrome.exe") 
       , null 
       , null 
       , null 
       , true 
       , 0 
       , null 
       , null 
       , startupInfo 
       , processInformation); 
    } 
} 

답변

2

크롬이 실행되고 있지 않은 경우, 당신은 물론 그 윈도우의 핸들을 얻을 같은 이유로 수 없습니다 창은 존재하지 않습니다. 이 같은 것을 다음, ProcessBuilder를 같은 것을 사용하여, 크롬을 실행 호출 할 수 있습니다 : 물론

user32.EnumWindows(new WndEnumProc() 
{ 

    @SuppressWarnings ("AssignmentToMethodParameter") 
    public boolean callback (int hWnd, int lParam) 
    { 
     if (user32.IsWindow(hWnd)) 
     { 
      if (user32.IsWindowVisible(hWnd)) 
      { 
       RECT r = new RECT(); 
       user32.GetWindowRect(hWnd, r); 
       // if (r.left > -32000) // is not minimized 
       //{ 
       String windowTitle = getWindowParentName(hWnd); 
       String windowClass = getWindowParentClassName(hWnd); 
       hWnd = user32.GetAncestor(hWnd, 3); 
       if (!windowTitle.toLowerCase().equals("program manager") && !windowClass.toLowerCase().equals("progman") && !windowTitle.equals("") && !windowClass.toLowerCase().equals("shell_traywnd")) 
       { 
        listOfWindows.put(hWnd, getWindowParentRectangle(hWnd)); 
       } 
       // } 
      } 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
}, 0); 

, 내 응용 프로그램과 관련된 몇 가지 조건을 가지고이 이미 작동하고 코드를. 그러나 주요 아이디어는에 EnumWindows를 호출하여 WndEnumProc()을 호출하여 찾은 모든 윈도우를 컬렉션 (내 경우 HashMap)에 배치합니다. EnumWindows가 돌아 오면 listOfWindows 변수에 창 모음이 생기고 EnumWindows가 호출되는 동안 실행 중이면 Chrome의 hwnd를 가져올 수 있습니다.

당신이처럼 USER32 인스턴스에서 EnumWindows를 정의해야합니다

/** 
* Enumerates all top-level windows on the screen by passing the handle to each window, in turn, to an application-defined callback function. 
* @param wndenumproc A pointer to an application-defined callback function. 
* @param lParam An application-defined value to be passed to the callback function. 
* @return if the function succeeded. 
* <a href="http://msdn.microsoft.com/en-us/library/ms633497(v=VS.85).aspx"> <b>Microsoft Reference</b></a><br> 
*/ 
public boolean EnumWindows (WndEnumProc wndenumproc, int lParam); 

또한 (광산 구조를 지명되었다)이 같은 클래스에 WndEnumProc 구조를 정의한다 :

public static interface WndEnumProc extends StdCallLibrary.StdCallCallback 
    { 

     boolean callback (int hWnd, int lParam); 
    } 

희망 도움이됩니다. 모든 마술을하는 동안 Chrome이 실행 중이어야합니다. 내가 처음에 언급 한 바와 같이, 그것을 실행하는 ProcessBuilder를을 사용하여 비교적 간단해야한다 또는 당신이 많이 귀찮게하지 않으려는 크롬 경로에있는 경우, 당신은 크롬을 시작

System.getRuntime().exec("chrome.exe")

를 사용할 수 있습니다.

관련 문제