java - 在 Java 中将 SHGetFileInfo 与 JNA 结合使用

标签 java c# winapi jna

我尝试使用 JNA 将 SHGetFileInfo 函数从 Shell32 转换为 Java,并使用 C# 代码和 this作为引用

虽然在 C# 代码中 psfi.iIcon 是 432,但在我翻译的 Java 代码中 psfi.iIcon 是 0。如果我是对的,对于同一个文件,无论我使用哪种语言,它们都应该是相同的,不是吗?

我的Java代码:

public static void main(String[] args) {
    String path = "[PATH_TO_EXE]\\test.exe"; //Of course in my code I used the real path

    SHFILEINFO sfi = new SHFILEINFO();
    DWORD_PTR i = Shell32.INSTANCE.SHGetFileInfo(path, 0, sfi, sfi.size(), SHGFI.SysIconIndex);

    System.out.println(sfi.iIcon); //<-- Prints 0, should print 432
}

public static class SHGFI {
    static final int SysIconIndex = 0x000004000;
    static final int LargeIcon = 0x000000000;
    static final int UseFileAttributes = 0x000000010;
}

public interface Shell32 extends StdCallLibrary {
    Shell32 INSTANCE = Native.loadLibrary("shell32", Shell32.class, W32APIOptions.UNICODE_OPTIONS);
    DWORD_PTR SHGetFileInfo(String pszPath, int dwFileAttributes, SHFILEINFO psfi, int cbFileInfo, int uFlags);
}

public static class SHFILEINFO extends Structure {
    public HICON hIcon;
    public int iIcon;
    public DWORD dwAttributes;
    public char[] szDisplayName = new char[260];
    public char[] szTypeName = new char[80];

    @Override
    protected List<String> getFieldOrder() {
        return Arrays.asList("hIcon", "iIcon", "dwAttributes", "szDisplayName", "szTypeName");
    }
}

我做错了什么根本性的事情吗?我是 JNA 和 Windows 函数的新手

最佳答案

备注部分下,有一条信息,恕我直言,这可能是您问题的根源

You must initialize Component Object Model (COM) with CoInitialize or OleInitialize prior to calling SHGetFileInfo.

这是一个非常简单的调用

CoInitialize(null);

正如 DanielWiddis 在评论中指出的那样,根据文档

New applications should call CoInitializeEx instead of CoInitialize

还有

To close the COM library gracefully, each successful call to CoInitialize or CoInitializeEx, including those that return S_FALSE, must be balanced by a corresponding call to CoUninitialize

示例

CoInitializeEx(null, 0);
CoUninitialize();

关于java - 在 Java 中将 SHGetFileInfo 与 JNA 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55208351/

相关文章:

java - 如何在运行 Maven 测试时优化 CPU 利用率

c# - Team Foundation Server 是将 .net 网站自动发布到远程服务器的正确解决方案吗?

c# - WebBrowser aspx 的命名空间

c++ - 设置系统光标大小

java - 轴突框架 : Saga project with compensation events between two or three microservices

java - 处理多个项目时将资源与 gradle 合并的问题

c++ - SetLayeredWindowAttributes 不适用于 Windows 8+ (C++)

c++ - C win32 应用程序

Java:扩大和自动装箱转换问题

c# - 返回Task <IHttpActionResult>方法的JSON错误消息