Search in sources :

Example 6 with CLSID

use of com.sun.jna.platform.win32.Guid.CLSID in project jna by java-native-access.

the class ObjectFactory method discoverClsId.

GUID discoverClsId(ComObject annotation) {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    String clsIdStr = annotation.clsId();
    final String progIdStr = annotation.progId();
    if (null != clsIdStr && !clsIdStr.isEmpty()) {
        return new CLSID(clsIdStr);
    } else if (null != progIdStr && !progIdStr.isEmpty()) {
        final CLSID.ByReference rclsid = new CLSID.ByReference();
        WinNT.HRESULT hr = Ole32.INSTANCE.CLSIDFromProgID(progIdStr, rclsid);
        COMUtils.checkRC(hr);
        return rclsid;
    } else {
        throw new COMException("ComObject must define a value for either clsId or progId");
    }
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) COMException(com.sun.jna.platform.win32.COM.COMException) CLSID(com.sun.jna.platform.win32.Guid.CLSID) PointerByReference(com.sun.jna.ptr.PointerByReference)

Example 7 with CLSID

use of com.sun.jna.platform.win32.Guid.CLSID in project jna by java-native-access.

the class COMInfoUtil method main.

public static void main(String[] args) {
    FileWriter writer = null;
    try {
        String filename = new File(Helper.tempDir, "CLSIDs.txt").getAbsolutePath();
        ArrayList<COMInfo> comInfos = COMUtils.getAllCOMInfoOnSystem();
        writer = new FileWriter(filename);
        for (COMInfo comInfo : comInfos) {
            String result = "CLSID: " + comInfo.clsid + "\n";
            result += "InprocHandler32: " + comInfo.inprocHandler32 + "\n";
            result += "InprocServer32: " + comInfo.inprocServer32 + "\n";
            result += "LocalServer32: " + comInfo.localServer32 + "\n";
            result += "ProgID: " + comInfo.progID + "\n";
            result += "ProgTypeLibID: " + comInfo.typeLib + "\n";
            writer.write(result + "\n");
        }
        System.out.println("file written to: " + filename);
        System.out.println("Found CLSID`s on the system: " + comInfos.size());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) COMInfo(com.sun.jna.platform.win32.COM.COMUtils.COMInfo)

Example 8 with CLSID

use of com.sun.jna.platform.win32.Guid.CLSID in project jna by java-native-access.

the class COMUtils method getAllCOMInfoOnSystem.

/**
     * Gets the all com info on system.
     * 
     * @return the all com info on system
     */
public static ArrayList<COMInfo> getAllCOMInfoOnSystem() {
    HKEYByReference phkResult = new HKEYByReference();
    HKEYByReference phkResult2 = new HKEYByReference();
    String subKey;
    ArrayList<COMInfo> comInfos = new ArrayList<COMUtils.COMInfo>();
    try {
        // open root key
        phkResult = Advapi32Util.registryGetKey(WinReg.HKEY_CLASSES_ROOT, "CLSID", WinNT.KEY_READ);
        // open subkey
        InfoKey infoKey = Advapi32Util.registryQueryInfoKey(phkResult.getValue(), WinNT.KEY_READ);
        for (int i = 0; i < infoKey.lpcSubKeys.getValue(); i++) {
            EnumKey enumKey = Advapi32Util.registryRegEnumKey(phkResult.getValue(), i);
            subKey = Native.toString(enumKey.lpName);
            COMInfo comInfo = new COMInfo(subKey);
            phkResult2 = Advapi32Util.registryGetKey(phkResult.getValue(), subKey, WinNT.KEY_READ);
            InfoKey infoKey2 = Advapi32Util.registryQueryInfoKey(phkResult2.getValue(), WinNT.KEY_READ);
            for (int y = 0; y < infoKey2.lpcSubKeys.getValue(); y++) {
                EnumKey enumKey2 = Advapi32Util.registryRegEnumKey(phkResult2.getValue(), y);
                String subKey2 = Native.toString(enumKey2.lpName);
                if (subKey2.equals("InprocHandler32")) {
                    comInfo.inprocHandler32 = (String) Advapi32Util.registryGetValue(phkResult2.getValue(), subKey2, null);
                } else if (subKey2.equals("InprocServer32")) {
                    comInfo.inprocServer32 = (String) Advapi32Util.registryGetValue(phkResult2.getValue(), subKey2, null);
                } else if (subKey2.equals("LocalServer32")) {
                    comInfo.localServer32 = (String) Advapi32Util.registryGetValue(phkResult2.getValue(), subKey2, null);
                } else if (subKey2.equals("ProgID")) {
                    comInfo.progID = (String) Advapi32Util.registryGetValue(phkResult2.getValue(), subKey2, null);
                } else if (subKey2.equals("TypeLib")) {
                    comInfo.typeLib = (String) Advapi32Util.registryGetValue(phkResult2.getValue(), subKey2, null);
                }
            }
            Advapi32.INSTANCE.RegCloseKey(phkResult2.getValue());
            comInfos.add(comInfo);
        }
    } finally {
        Advapi32.INSTANCE.RegCloseKey(phkResult.getValue());
        Advapi32.INSTANCE.RegCloseKey(phkResult2.getValue());
    }
    return comInfos;
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference) ArrayList(java.util.ArrayList) EnumKey(com.sun.jna.platform.win32.Advapi32Util.EnumKey) InfoKey(com.sun.jna.platform.win32.Advapi32Util.InfoKey)

Example 9 with CLSID

use of com.sun.jna.platform.win32.Guid.CLSID in project jna by java-native-access.

the class ObjectFactory method createObject.

/**
	 * Creates a new COM object (CoCreateInstance) for the given progId and
	 * returns a ProxyObject for the given interface.
	 */
public <T> T createObject(Class<T> comInterface) {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    ComObject comObectAnnotation = comInterface.getAnnotation(ComObject.class);
    if (null == comObectAnnotation) {
        throw new COMException("createObject: Interface must define a value for either clsId or progId via the ComInterface annotation");
    }
    final GUID guid = this.discoverClsId(comObectAnnotation);
    final PointerByReference ptrDisp = new PointerByReference();
    WinNT.HRESULT hr = Ole32.INSTANCE.CoCreateInstance(guid, null, WTypes.CLSCTX_SERVER, IDispatch.IID_IDISPATCH, ptrDisp);
    COMUtils.checkRC(hr);
    Dispatch d = new Dispatch(ptrDisp.getValue());
    T t = this.createProxy(comInterface, d);
    //CoCreateInstance returns a pointer to COM object with a +1 reference count, so we must drop one
    //Note: the createProxy adds one
    int n = d.Release();
    return t;
}
Also used : COMException(com.sun.jna.platform.win32.COM.COMException) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) WinNT(com.sun.jna.platform.win32.WinNT) PointerByReference(com.sun.jna.ptr.PointerByReference) ComObject(com.sun.jna.platform.win32.COM.util.annotation.ComObject) GUID(com.sun.jna.platform.win32.Guid.GUID) WinNT(com.sun.jna.platform.win32.WinNT) Dispatch(com.sun.jna.platform.win32.COM.Dispatch) IDispatch(com.sun.jna.platform.win32.COM.IDispatch) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT)

Example 10 with CLSID

use of com.sun.jna.platform.win32.Guid.CLSID in project jna by java-native-access.

the class IUnknownTest method createIUnknown.

private Unknown createIUnknown() {
    try {
        PointerByReference pUnknown = new PointerByReference();
        // Get CLSID for Word.Application...
        CLSID.ByReference clsid = new CLSID.ByReference();
        HRESULT hr = Ole32.INSTANCE.CLSIDFromProgID("Shell.Application", clsid);
        if (W32Errors.FAILED(hr)) {
            Ole32.INSTANCE.CoUninitialize();
            COMUtils.checkRC(hr);
        }
        hr = Ole32.INSTANCE.CoCreateInstance(clsid, null, WTypes.CLSCTX_SERVER, IUnknown.IID_IUNKNOWN, pUnknown);
        if (W32Errors.FAILED(hr)) {
            COMUtils.checkRC(hr);
        }
        return new Unknown(pUnknown.getValue());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) CLSID(com.sun.jna.platform.win32.Guid.CLSID) PointerByReference(com.sun.jna.ptr.PointerByReference) PointerByReference(com.sun.jna.ptr.PointerByReference)

Aggregations

HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)8 PointerByReference (com.sun.jna.ptr.PointerByReference)7 CLSID (com.sun.jna.platform.win32.Guid.CLSID)5 COMException (com.sun.jna.platform.win32.COM.COMException)3 WString (com.sun.jna.WString)2 Dispatch (com.sun.jna.platform.win32.COM.Dispatch)2 IDispatch (com.sun.jna.platform.win32.COM.IDispatch)2 ComObject (com.sun.jna.platform.win32.COM.util.annotation.ComObject)2 GUID (com.sun.jna.platform.win32.Guid.GUID)2 LCID (com.sun.jna.platform.win32.WinDef.LCID)2 WinNT (com.sun.jna.platform.win32.WinNT)2 EnumKey (com.sun.jna.platform.win32.Advapi32Util.EnumKey)1 InfoKey (com.sun.jna.platform.win32.Advapi32Util.InfoKey)1 COMInfo (com.sun.jna.platform.win32.COM.COMUtils.COMInfo)1 REFIID (com.sun.jna.platform.win32.Guid.REFIID)1 DISPIDByReference (com.sun.jna.platform.win32.OaIdl.DISPIDByReference)1 UINTByReference (com.sun.jna.platform.win32.WinDef.UINTByReference)1 USHORTByReference (com.sun.jna.platform.win32.WinDef.USHORTByReference)1 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)1 File (java.io.File)1