CiNcH Posted March 6, 2008 Share Posted March 6, 2008 (edited) There are several ways to access COM through Java. I have chosen COM4J which generates Java interfaces (definitions) from the DVBViewer COM type library which exposes functionality that the DVBViewer COM server provides. COM4J then acts as a stub or proxy which converts the Java calls to language independent COM calls which are passed to the DVBViewer COM server (all this happens transparently to the developer). I am attaching an Eclipse project with everything that is needed to access DVBViewer COM-Server through Java (DVBViewer interfaces/definitions + COM4J library/ stub) including a sample application that calls a method on the DVBViewer and receives DVBViewer events. Within 'DVBV_COM4J\libs' you'll find the COM4J library and JNI stub/proxy/wrapper (or whatever you want to call it). I have also taken a quick look at j-interop and JACOB (Java COM Bridge) and they both work with reflection to access methods and properties. So you always have to have the API doc beside you (and it is quite ugly code ). Using the COM4J generated interfaces is much more convenient ("IntelliSense" can be used, nice and well-maintainable object-oriented code). Hopefully we'll see some nice Java plugins in the future. DVBV_COM4J.zip package main; import java.util.Scanner; import dvbvstub.definitions.ClassFactory; import dvbvstub.definitions.IDVBViewer; import dvbvstub.definitions.IDVBViewerEventhelper; import dvbvstub.definitions.events.IDVBViewerEvents; import com4j.EventCookie; /** * DVB[removed, violating forum rule §13 ]in demonstrates how to consume COM services exposed by the DVBViewerServer. * * It shows both, how to call a method on the DVBViewer and how to register callbacks * that are triggered by the DVBViewer (asynchronous events). * * @link http://en.wikipedia.org/wiki/Component_Object_Model * * @author Daniel Bechter (daniel.bechter@inode.at) */ public class DVB[removed, violating forum rule §13 ]in { private IDVBViewer m_DVBViewerServer = null; private EventCookie m_EventObject = null; /** * Constructor binds a DVBViewerServer proxy object and registers an event callback. */ public DVB[removed, violating forum rule §13 ]in() { m_DVBViewerServer = ClassFactory.createDVBViewer(); IDVBViewerEventhelper eventHelper = m_DVBViewerServer.events(); m_EventObject = eventHelper.advise(IDVBViewerEvents.class, new EventCallback()); } /** * Waits for user console input. * * <p> * 'q' to quit the DVBViewer */ public void inputLoop() { Scanner in = new Scanner(System.in); while (true) { System.out.println("Quit DVBViewer? [q] "); String input = in.next(); if (input.length() == 1 && input.equals("q")) { closeDVBViewer(); break; } } in.close(); } /** * Asks the DVBViewerServer to close the DVBViewer */ public void closeDVBViewer() { m_DVBViewerServer.quit(); } /** * Application main entry point */ public static void main(String[] args) { new DVB[removed, violating forum rule §13 ]in().inputLoop(); } /** * Defines the methods (callbacks) that are called if a specific DVBViewer event is raised * * @author Daniel Bechter (daniel.bechter@inode.at) */ public class EventCallback extends IDVBViewerEvents { /** * Is called by the DVBViewer when an OSD is displayed * * @param windowID * Integer value specifying the ID of the OSD being displayed */ @Override public void onOSDWindow(int windowID) { System.out.println("DVBViewer Event: OSD with ID " + windowID); } /** * Is called by the DVBViewer when it is about to being closed */ @Override public void onDVBVClose() { System.out.println("DVBViewer Event: closing..."); m_EventObject.close(); System.exit(0); } } } Edited March 7, 2008 by CiNcH 2 Quote Link to comment
CiNcH Posted March 19, 2008 Author Share Posted March 19, 2008 (edited) Here is a little addition. You can do many things with COM4J. One thing I could not manage to get working is getting a SafeArray out of a Variant. I have read about people with exactly the same problem but no solution was provided, neither by the developer himself. The SafeArray part seems quite unfinished. So I have looked into another free Java COM wrapper, JACOB, and I finally succeeded. I will provide an example where you will recognize that JACOB is way less convenient due to Reflection usage (so you will need the DVBViewer COM API doc to call functions) instead of generating stub classes. Probably I will work out stub classes myself for the DVBViewer definitions (and give it a fancy name like 'DVBViewer COM API Framework for Java' or something like that ) to hide the Reflection part and make DVBViewer COM programming with JACOB more convenient because I do not really like the idea of using both, COM4J and JACOB. COM Prototype: function GetAsArray(ChannelID: Integer; StartTime: TDateTime; EndTime: TDateTime; out List: OleVariant): Integer; Call and SafeArray extraction using JACOB: ActiveXComponent dvbvComServer = ActiveXComponent.connectToActiveInstance("DVBViewerServer.DVBViewer"); ActiveXComponent epgManager = dvbvComServer.getPropertyAsComponent("EPGManager"); // function parameter Variant epgSafeArrayVariant = new Variant(); // SafeArray-Variant epgSafeArrayVariant.putVariant(0); Variant startTime = new Variant(); startTime.changeType(Variant.VariantDate); startTime.putDate(new Date()); Variant stopTime = new Variant(); startTime.changeType(Variant.VariantDate); stopTime.putDate(new Date(new Date().getTime() + 259200000)); Variant[] arguments = new Variant[] { new Variant((int)0), startTime, stopTime, epgSafeArrayVariant }; // function call Variant nrOfEntries = epgManager.invoke("GetAsArray", arguments); // data extraction and visualization SafeArray epgSaveArray = (SafeArray)arguments[3].getVariant(); System.out.println("Entries: " + nrOfEntries.getInt()); for (int i = 0; i < nrOfEntries.getInt(); i++) { System.out.print(epgSaveArray.getString(i, 2)); System.out.print(" " + epgSaveArray.getString(i, 5)); System.out.println(); } Edited March 19, 2008 by CiNcH Quote Link to comment
UPollaehne Posted March 20, 2008 Share Posted March 20, 2008 Could you make a performance comparison of calling GetChannelList() of IChannelCollection compared to iterating over all channels using the Item property of IChannelCollection? Quote Link to comment
mrphlox Posted April 11, 2012 Share Posted April 11, 2012 Hi that looks interesting to me! could you pls either upload the zip file again (the link above is no longer valid) or send it to me as a PM? thx! Quote Link to comment
Jan S Posted April 24, 2012 Share Posted April 24, 2012 Hi mrphlox, I have not the orignal zip, but I can give you another small example, it's done with the jacob libary. The zip contains a eclipse project. In order to get it running, you need to setup jacob correctly. eclipse example project For set up see Jacob Project under Downloads. Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.