Jump to content

HowTo: Access DVBViewer COM-API through Java


Recommended Posts

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_com.jpg

 

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 by CiNcH
  • Like 2
Link to comment
  • 2 weeks later...

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 :bye: ) 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 by CiNcH
Link to comment

Could you make a performance comparison of calling GetChannelList() of IChannelCollection compared to iterating over all channels using the Item property of IChannelCollection?

Link to comment
  • 4 years later...

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!

Link to comment
  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...