简体   繁体   中英

Jna E_OUTOFMEMORY

I wrote the equivavlent Java method for this c Method:

HRESULT h3 = CoInitializeEx(NULL, COINIT_MULTITHREADED);
HRESULT h4 = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);

IShellItemImageFactory * item;


HRESULT h = SHCreateItemFromParsingName(L"C:\\Users\\Marcel\\Desktop\\CosmosKernel2Boot.iso", NULL, IID_IShellItemImageFactory, (void**)&item);

if (SUCCEEDED(h)) {

    HBITMAP bit;

    SIZE size = { 32,32 };

    HRESULT hh = item->GetImage(size, SIIGBF_RESIZETOFIT, &bit);
    if (SUCCEEDED(hh)) {
        cout << "SUCCEEDED";
    }
}
item->Release();

CoUninitialize();

Java:

Ole32Extra.INSTANCE.CoInitializeEx(null,Ole32.COINIT_MULTITHREADED);
    Ole32Extra.INSTANCE.CoInitializeSecurity(null, -1, null, null, Ole32Extra.RPC_C_AUTHN_LEVEL_DEFAULT, Ole32Extra.RPC_C_IMP_LEVEL_IMPERSONATE, null, Ole32Extra.EOAC_NONE, null);

    PointerByReference factory = new PointerByReference();

    WinNT.HRESULT h5 = Shell32Extra.INSTANCE.SHCreateItemFromParsingName(new WString("C:\\Users\\Marcel\\Desktop\\Test.txt"),null,new Guid.REFIID(new Guid.IID(Shell32Extra.IID_IShellItemImageFactory)),factory);

    if(COMUtils.SUCCEEDED(h5)) {
        IShellItemImageFactory factory1 = new IShellItemImageFactory(factory.getValue());

        PointerByReference bitP = new PointerByReference();

        WinUser.SIZE size = new WinUser.SIZE(32,32);

        WinNT.HRESULT hi = factory1.GetImage(size,0,bitP);

        if(COMUtils.SUCCEEDED(hi)) {
            WinDef.HBITMAP bit = new WinDef.HBITMAP(bitP.getPointer());

            System.out.println("SUCCEEDED");
        }else{
            System.out.println(hi);
        }
        factory1.Release();
    }else{
        System.out.println(h5);
    }
    Ole32Extra.INSTANCE.CoUninitialize();

When I run the c method erverything works normal and "SUCCEEDED" is printed out.

But wehen I run the Java method the HRESULT hi always returns -2147024882 Which means E_OUTOFMEMORY.

I don't know why this error is thrown but I hope anyone can help

The error exactly happens in this lines:

HRESULT hh = item->GetImage(size, SIIGBF_RESIZETOFIT, &bit);

WinDef.HBITMAP bit = new WinDef.HBITMAP(bitP.getPointer());

The solution is that the SIZE struct can not be passed by value. (in C++ it can). You have to create a class SIZEByValue that extends SIZE and implements Structure.ByValue. Now it is working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM