Search in sources :

Example 11 with GUID

use of com.sun.jna.platform.win32.Guid.GUID 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 12 with GUID

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

the class Ole32Util method getStringFromGUID.

/**
	 * Convert a GUID into a string.
	 * @param guid
	 *  GUID.
	 * @return
	 *  String representation of a GUID.
	 */
public static String getStringFromGUID(GUID guid) {
    GUID pguid = new GUID(guid.getPointer());
    int max = 39;
    char[] lpsz = new char[max];
    int len = Ole32.INSTANCE.StringFromGUID2(pguid, lpsz, max);
    if (len == 0) {
        throw new RuntimeException("StringFromGUID2");
    }
    lpsz[len - 1] = 0;
    return Native.toString(lpsz);
}
Also used : GUID(com.sun.jna.platform.win32.Guid.GUID)

Example 13 with GUID

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

the class Shell32Util method getKnownFolderPath.

/**
     * Retrieves the full path of a known folder identified by the folder's KNOWNFOLDERID. This function replaces
     * {@link #getFolderPath}. That older function is now simply a wrapper for getKnownFolderPath
     * @param guid the KNOWNFOLDERS GUID as defined in {@link KnownFolders}
     * @return the path of the known folder. The returned path does not include a trailing backslash. For example,
     *        "C:\Users" is returned rather than "C:\Users\".
     * @throws Win32Exception if the guid references a KNOWNFOLDERID which does not have a path (such as a folder marked
     *        as KF_CATEGORY_VIRTUAL) or that the KNOWNFOLDERID is not present on the system. Not all KNOWNFOLDERID values are
     *        present on all systems.
     */
public static String getKnownFolderPath(GUID guid) throws Win32Exception {
    int flags = ShlObj.KNOWN_FOLDER_FLAG.NONE.getFlag();
    PointerByReference outPath = new PointerByReference();
    HANDLE token = null;
    HRESULT hr = Shell32.INSTANCE.SHGetKnownFolderPath(guid, flags, token, outPath);
    if (!W32Errors.SUCCEEDED(hr.intValue())) {
        throw new Win32Exception(hr);
    }
    String result = outPath.getValue().getWideString(0);
    Ole32.INSTANCE.CoTaskMemFree(outPath.getValue());
    return result;
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) PointerByReference(com.sun.jna.ptr.PointerByReference) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 14 with GUID

use of com.sun.jna.platform.win32.Guid.GUID 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 15 with GUID

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

the class Ole32Util method getGUIDFromString.

/**
	 * Convert a string to a GUID.
	 * @param guidString
	 *  String representation of a GUID, including { }.
	 * @return
	 *  A GUID.
	 */
public static GUID getGUIDFromString(String guidString) {
    GUID lpiid = new GUID();
    HRESULT hr = Ole32.INSTANCE.IIDFromString(guidString, lpiid);
    if (!hr.equals(W32Errors.S_OK)) {
        throw new RuntimeException(hr.toString());
    }
    return lpiid;
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) GUID(com.sun.jna.platform.win32.Guid.GUID)

Aggregations

GUID (com.sun.jna.platform.win32.Guid.GUID)20 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)10 PointerByReference (com.sun.jna.ptr.PointerByReference)7 COMException (com.sun.jna.platform.win32.COM.COMException)3 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 WinNT (com.sun.jna.platform.win32.WinNT)2 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)2 Memory (com.sun.jna.Memory)1 CLSID (com.sun.jna.platform.win32.Guid.CLSID)1 REFIID (com.sun.jna.platform.win32.Guid.REFIID)1 DomainController (com.sun.jna.platform.win32.Netapi32Util.DomainController)1 DomainTrust (com.sun.jna.platform.win32.Netapi32Util.DomainTrust)1 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)1 IntByReference (com.sun.jna.ptr.IntByReference)1 File (java.io.File)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1