吉娜E_OUTOFMEMORY

时间:2018-10-23 16:03:35

标签: java c winapi jna

我为此c方法编写了等效Java方法:

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();

当我运行c方法时,一切正常,并且打印出“成功”。

但是,当我运行Java方法时,HRESULT hi总是返回-2147024882 意思是E_OUTOFMEMORY。

我不知道为什么会引发此错误,但我希望任何人都可以帮助

此行中确实发生了错误:

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

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

1 个答案:

答案 0 :(得分:0)

解决方案是不能通过值传递SIZE结构。 (在C ++中可以)。 您必须创建一个扩展SIZE并实现Structure.ByValue的类SIZEByValue。现在可以了。