Search code examples
javawindows-7jna

Unable to access the IActiveDesktop interface with JNA (No such interface supported(HRESULT: 80004002)


I'm trying to change the desktop wallpaper on windows 7 with the IActiveDesktop interface. So I start my project and I first use the SystemParametersInfo method using the User32 class. But there isn't fade effect and I can't modify the image position. After some research, I found the IActiveDesktop interface and this question. I took the answer and I adapt it. But when I running my code, it throw Exception in thread "main" com.sun.jna.platform.win32.COM.COMException: No such interface supported(HRESULT: 80004002).

Here you can find my code:

First the class which represent the IActiveDesktop interface

import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.COM.COMUtils;
import com.sun.jna.platform.win32.COM.Unknown;
import com.sun.jna.platform.win32.*;
import com.sun.jna.ptr.PointerByReference;

import static com.sun.jna.platform.win32.Ole32.INSTANCE;

public class ActiveDesktop extends Unknown {

    // {75048700-EF1F-11D0-9888-006097DEACF9}
    // {F490EB00-1240-11D1-9888-006097DEACF9}

    private static final Guid.GUID CLSID_ActiveDesktop = getGUID("{75048700-EF1F-11D0-9888-006097DEACF9}");
    private static final Guid.GUID IID_IActiveDesktop = getGUID("{F490EB00-1240-11D1-9888-006097DEACF9}");

    private static Guid.GUID getGUID(String guidStr) {
        Guid.GUID guid = new Guid.GUID();

        INSTANCE.IIDFromString(guidStr, guid);

        return guid;
    }

    private ActiveDesktop(Pointer pvInstance) {
        super(pvInstance);
    }

    public static ActiveDesktop create(){
        PointerByReference p = new PointerByReference();

        WinNT.HRESULT hr = INSTANCE.CoCreateInstance(CLSID_ActiveDesktop, null, WTypes.CLSCTX_INPROC_SERVER, IID_IActiveDesktop, p); // THE EXCEPTION IS THROW HERE
        COMUtils.checkRC(hr);

        return new ActiveDesktop(p.getValue());
    }
}

And the Main which just load the Ole32 librairy and IActiveDesktop interface:

import com.sun.jna.platform.win32.Ole32;

public class Main {

    public static void main(String[] args) {
        Ole32.INSTANCE.CoInitializeEx(Pointer.NULL, Ole32.COINIT_MULTITHREADED);

        try {
            ActiveDesktop desktop = ActiveDesktop.create();
        } finally {
            Ole32.INSTANCE.CoUninitialize();
        }
    }
}

After a lot of googling, I cannot find an answer. Can anyone help me? May be the answer is where I found this code... In this case, please explain me what's wrong. Thank you.

NB: I'm french so my english isn't perfect. I'm sorry...


Solution

  • I finally found a solution. Which is to replace Ole32.COINIT_MULTITHREADED by Ole32.COINIT_APARTMENTTHREADED when initialize Ole32.