Microsoft SDK for Java

Java Monikers

ActiveX containers, such as Visual Basic 5.0, are able to find a Bean through a Java Moniker, which uses a URL syntax similar to the one used in the HTML OBJECT tag. For example, in Visual Basic 5.0, you can use the command GetObject("java:java.util.Date") to create an instance of the Date object, and then invoke methods on it.

This is possible because DCOM introduced an extensible moniker namespace, so if you pass a string like "java:some.class" to MkParseDisplayName, OLE32 will load the Microsoft VM and let it provide the class moniker to bind to the object (it looks up "java" as a progid in the registry and gets the CLSID of a handler implemented by the VM).

Furthermore, ActiveX containers that host a Bean can find information about the Bean through a COM interface called ITypeInfo. This allows access to interfaces such as IDispatch and permits all the usual operations represented through ActiveX interfaces, such as calling QueryInterface, and so on.

To help illustrate how moniker binding works, the following is a sample C++ program that uses monikers to bind to a simple Java object called TestObject. (Its source code is shown after this example.)

TEST.CPP

#include <windows.h>
#include <olectl.h>
#include <stdio.h>

HRESULT BindToObject(LPOLESTR pwszDisplayName, REFIID riid, LPVOID *ppunk)
{
    HRESULT hr;
    IBindCtx *pbc;
    ULONG chEaten;
    IMoniker *pmk;

    *ppunk = NULL;
    hr = CreateBindCtx(0, &pbc);
    if (hr == S_OK) {
        hr = MkParseDisplayName(pbc, pwszDisplayName, &chEaten, &pmk);
        if (hr == S_OK) {
            hr = pmk->BindToObject(pbc, NULL, riid, ppunk);
            pmk->Release();
        }
        pbc->Release();
    }
    return hr;
}
void main(int argc, char *argv[])
{
    HRESULT hr;
    IUnknown *punk;
    hr = CoInitialize(NULL);
    if (hr == S_OK) {
        hr = BindToObject(L"java:TestObject", IID_IUnknown, (LPVOID *) &punk);
        if (hr == S_OK) {
            punk->Release();
        } else {
            printf("failed to bind to java object (hr=%08x)\n", hr);
        }
        CoUninitialize();
    }
}

TestObject.java

import java.io.*;
import java.net.*;
import com.ms.com.*;
public class TestObject
{
    public TestObject() {
        System.out.println("created java object!");
    }
}

© 1999 Microsoft Corporation. All rights reserved. Terms of use.