import helloserver.*; import java.awt.*; import java.awt.event.*; public class Client{ //Private Objects - We'll hold a reference to the COM object IComHelloServer theServer; Frame theWindow; Button callComFunction, clear; Label serverResponse; Panel thePanel; ClearDisplay clearAction; CallComFunction callCom; WindowHandler winEvent; int loopThreadID=0; public static void main(String args[]){ Client comClient = new Client(); } public Client(){ com.ms.com.ComLib.declareMessagePumpThread(); loopThreadID = com.ms.win32.Kernel32.GetCurrentThreadId(); //Inside the virtual machine this invokes a call to CoCreateInstance //starts up the program HelloServer.exe and gives me reference to the interface theServer = (IComHelloServer) new ComHelloServer(); //Give the Server some time to come up try{ Thread.sleep(10); }catch(Exception ii) {} startGUI(); //setup a windows message loop for the thread, we want to catch the WM_QUIT message com.ms.win32.MSG msg = new com.ms.win32.MSG(); while (com.ms.win32.User32.GetMessage(msg, 0, 0, 0)) { com.ms.win32.User32.TranslateMessage(msg); if (msg.message==com.ms.win32.win.WM_QUIT) break; com.ms.win32.User32.DispatchMessage(msg); } //When were done, we release it com.ms.com.ComLib.release(theServer); //Give the VM some time to release the Server try{ Thread.sleep(1000); }catch(Exception ii){} System.exit(0); } void startGUI(){ theWindow = new Frame("HelloServer Driver"); //theWindow.setSize(300,300); thePanel = new Panel(); serverResponse = new Label(); //This is where we display the Server's response setupButtons(); setupPanel(); setupLayout(); theWindow.setVisible(true); winEvent = new WindowHandler(theWindow, loopThreadID); theWindow.addWindowListener(winEvent); } void setupButtons(){ callComFunction = new Button("Call COM Method"); clear = new Button("Clear Display"); clearAction = new ClearDisplay(serverResponse, theWindow); callCom = new CallComFunction(theServer, serverResponse, theWindow); callComFunction.addActionListener(callCom); clear.addActionListener(clearAction); } void setupLayout(){ theWindow.setLayout(new BorderLayout()); setupPanel(); theWindow.add(thePanel, BorderLayout.CENTER); } void setupPanel(){ thePanel.setLayout(new GridLayout(0,1)); thePanel.add(callComFunction); thePanel.add(clear); thePanel.add(serverResponse); } } class WindowHandler extends WindowAdapter{ Frame theWindow; int threadID; public WindowHandler(Frame theWindow, int threadID){ this.theWindow = theWindow; this.threadID = threadID; } public void windowClosing(WindowEvent evt){ theWindow.dispose(); com.ms.win32.User32.PostThreadMessage(threadID, com.ms.win32.win.WM_QUIT, 0, 0); } } class CallComFunction implements ActionListener{ IComHelloServer theServer; Frame theWindow; Label serverResponse; public CallComFunction(IComHelloServer theServer, Label serverResponse, Frame theWindow){ this.theServer = theServer; this.theWindow = theWindow; this.serverResponse = serverResponse; } public void actionPerformed(ActionEvent evt){ String response = theServer.HelloWorld(); //Make a call on the COM interface serverResponse.setText(response); theWindow.validate(); //Let's redo the layout to catch the update } } class ClearDisplay implements ActionListener{ Frame theWindow; Label serverResponse; public ClearDisplay(Label serverResponse, Frame theWindow){ this.theWindow = theWindow; this.serverResponse = serverResponse; } public void actionPerformed(ActionEvent evt){ serverResponse.setText(""); //Clear the display box theWindow.validate(); //Let's redo the layout to catch the update } }