java.lang.UnsatisfiedLinkError:查找函数'GetModuleFileNameEx'时出错

时间:2017-05-06 16:11:28

标签: java winapi dll jna unsatisfiedlinkerror

我正在尝试在java应用程序中导入GetModuleFileNameEx。该函数的定义是:

DWORD WINAPI GetModuleFileNameEx(
  _In_     HANDLE  hProcess,
  _In_opt_ HMODULE hModule,
  _Out_    LPTSTR  lpFilename,
  _In_     DWORD   nSize
);

我将其翻译成:

public abstract DWORD GetModuleFileNameEx(
    WinNT.HANDLE hProcess,
    WinNT.HMODULE hModule,
    char[] pathName,
    WinNT.DWORD pathNameSize
);

我在课堂上定义的是这样的:

 import com.sun.jna.Native;
 import com.sun.jna.Pointer;
 import com.sun.jna.platform.win32.Kernel32;
 import com.sun.jna.platform.win32.WinDef;
 import com.sun.jna.platform.win32.WinNT;
 import com.sun.jna.win32.W32APIOptions;


 public abstract interface Kernel32Ext
   extends Kernel32
 {
   public static final Kernel32Ext INSTANCE = (Kernel32Ext)Native.loadLibrary("kernel32.dll", Kernel32Ext.class, W32APIOptions.DEFAULT_OPTIONS);
   public abstract DWORD GetModuleFileNameEx(WinNT.HANDLE hProcess, WinNT.HMODULE hModule, char[] pathName, WinNT.DWORD pathNameSize);
 }

但是当我尝试调用该方法时,我收到一个错误:

java.lang.UnsatisfiedLinkError: Error looking up function 'GetModuleFileNameEx': Uvedená procedura nebyla nalezena.

我仔细检查过,根据这里关于堆栈溢出和其他JNA程序的帖子,LPTSTR在JNA API中正确转换为char[]。所以一定有别的东西是错的。我输入了错误的dll,或者输入了错误的选项吗?

我在Windows 7 x64 bit(捷克语,因此非英语错误消息)上运行它。

2 个答案:

答案 0 :(得分:1)

在kernel32(或其他地方)中没有该名称的功能。请参阅GetModuleFileNameEx的MSDN页面。您正在寻找的功能是GetModuleFileNameExW

答案 1 :(得分:0)

好的,完整的代码:

import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.win32.W32APIOptions;

public interface Psapi extends WinNT {
    public static final Psapi INSTANCE = (Psapi)Native.loadLibrary("Psapi.dll", Psapi.class, W32APIOptions.DEFAULT_OPTIONS);
    public abstract WinDef.DWORD GetModuleFileNameExW(WinNT.HANDLE hProcess, WinNT.HMODULE hModule, char[] pathName, WinNT.DWORD pathNameSize);
}

请注意,我最终从Psapi.dll导入了该函数,而不是Kernel32.dll。

示例用法,获取给定窗口HWND的进程的文件名:

  protected WinDef.HWND hwnd;
  @Override
  public String getProcessName() {
    // Refference to int that will later be filled 
    IntByReference pid = new IntByReference(0); 
    // This function gives pid number to the second parameter passed by refference
    UserExt.GetWindowThreadProcessId(hwnd, pid);
    // Now get handle to the process 
    // 0x0400 | 0x0010 stands for reading info
    // if you pass 0 you will get error 5 which stands for access denied
    int pidVal = pid.getValue();
    HANDLE process = Kernel32.INSTANCE.OpenProcess(0x0400 | 0x0010, false, pidVal);
    if(process==null)
      throw new APIException("Winapi error: "+(Kernel32.INSTANCE.GetLastError()));
    // Prepare buffer for characters, just as you would 
    // in goold 'ol C program
    char[] path = new char[150];
    DWORD buffSize = new DWORD(path.length);
    // The W at the end of the function name stands for WIDE - 2byte chars
    Psapi.INSTANCE.GetModuleFileNameExW(process, null, path, buffSize);
    // convert buffer to java string
    return String.copyValueOf(path);
  }