/* class EnumWinApplet for IE browsers uses J/Direct with an inner class callback to enumerate top level windows using EnumWindows() API call. Also demonstrates closing a user-selected window using SendMessage() function M. Gallant 05/01/2002 */ import java.awt.*; import com.ms.security.*; import com.ms.wfc.util.*; import com.ms.win32.win; import java.awt.event.*; public class EnumWinApplet extends java.applet.Applet implements ActionListener{ private StringBuffer strb; private TextArea ta = new TextArea(15, 80) ; private Button closewindow = new Button("Close Window") ; private Button cleartext = new Button("Clear") ; private Button updatewins = new Button("Update") ; private TextField winhandle = new TextField(10) ; public void init() { try { if (Class.forName("com.ms.security.PolicyEngine") != null) // get permissions PolicyEngine.assertPermission(PermissionID.SYSTEM); doWinEnum() ; } catch (Throwable cnfe) { } this.setLayout(new BorderLayout()) ; add(ta, BorderLayout.CENTER) ; Panel pan = new Panel() ; pan.setLayout(new FlowLayout(FlowLayout.LEFT)) ; pan.add(new Label("Window Handle:", Label.RIGHT)) ; pan.add(winhandle); pan.add(closewindow) ; pan.add(new Label(" ")) ; // spacer pan.add(updatewins); pan.add(cleartext); add(pan, BorderLayout.SOUTH) ; closewindow.addActionListener(this) ; closewindow.setBackground(Color.red) ; updatewins.addActionListener(this) ; updatewins.setBackground(Color.green) ; cleartext.addActionListener(this) ; cleartext.setBackground(Color.white) ; } class NewEnumProc extends com.ms.dll.Callback { // inner class callback public boolean callback(int hwnd, int lparam) { int [] processID = new int[1] ; StringBuffer text = new StringBuffer(128); //max length window title is 128 characters. GetWindowText(hwnd, text, text.capacity()+1); int threadprocessid = GetWindowThreadProcessId(hwnd, processID) ; if (text.length() != 0) { strb.append("hwnd: " + Integer.toHexString(hwnd) +"h ProcID: " + processID[0] +" Text: " + text + "\r\n" ); } return true; // return TRUE to continue enumeration. } } // end inner class private void doWinEnum() { strb = new StringBuffer(1024) ; NewEnumProc mycallback = new NewEnumProc(); boolean results = false; results = EnumWindows(mycallback, 1) ; if( ! results ) ta.setText("Problem enumerating windows") ; else ta.setText(this.strb.toString()) ; } private void closeWin32win(int winhandle) { SendMessage(winhandle, win.WM_SYSCOMMAND, win.SC_CLOSE, 0); // PostMessage(winhandle, win.WM_CLOSE, 0, 0) ; // this also works, but SendMessage like this fails! } public void paint(Graphics g) { // black border around applet panel g.setColor(Color.black) ; g.drawRect(0,0,this.getSize().width-1, this.getSize().height-1) ; } public void actionPerformed(ActionEvent e) { if(e.getSource() == cleartext){ winhandle.setText("") ; ta.setText("") ; } else if(e.getSource() == updatewins) doWinEnum(); else if(e.getSource() == closewindow) { //close the win32 window specified by textfield int hexhandleint = 0; String hexhandle = winhandle.getText().trim(); if(hexhandle.length()==0) //if white space only, do nothing return; try { ta.append(" Trying to close window with handle: " + hexhandle + "\n") ; hexhandleint=Integer.parseInt(hexhandle, 16); if(!IsWindow(hexhandleint)){ ta.append(" Window handle " + hexhandle + " does not exist\n") ; return; } else //the window exists, so close it. closeWin32win(hexhandleint) ; ta.append(" SendMessage(winhandle, win.WM_SYSCOMMAND, win.SC_CLOSE, 0) successful\n") ; } catch(NumberFormatException nfe) { ta.append(" The window handle is NOT a hex-number\n") ; } } } /** @dll.import("USER32") */ private static native int GetWindowText(int hwnd, StringBuffer text, int cch); /** @dll.import("USER32") */ private static native int GetWindowThreadProcessId(int hwnd, int[] lpdwProcessId) ; /** @dll.import("USER32") */ private static native boolean EnumWindows(NewEnumProc myproc, int param); /** @dll.import("USER32") */ public static native int SendMessage (int winhandle, int message, int wparam, int lparam ); /** @dll.import("USER32") */ public static native boolean IsWindow (int winhandle); }