[英]JNA Java CredUIPromptForWindowsCredentialsW
我正在嘗試將此函數轉換為JNA:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycode.winapi;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.WString;
import com.sun.jna.platform.win32.Sspi;
import com.sun.jna.platform.win32.WinDef.HBITMAP;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.ULONG;
import com.sun.jna.platform.win32.WinDef.ULONGByReference;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import java.util.Arrays;
import java.util.List;
/**
* Credui
* @author
*/
public interface Credui extends Library {
/**
* INSTANCE
*/
Credui INSTANCE = (Credui) Native.loadLibrary("Credui", Credui.class);
/**
* CredUIPromptForWindowsCredentialsW
* DWORD WINAPI CredUIPromptForWindowsCredentials(
* _In_opt_ PCREDUI_INFO pUiInfo,
* _In_ DWORD dwAuthError,
* _Inout_ ULONG *pulAuthPackage,
* _In_opt_ LPCVOID pvInAuthBuffer,
* _In_ ULONG ulInAuthBufferSize,
* _Out_ LPVOID *ppvOutAuthBuffer,
* _Out_ ULONG *pulOutAuthBufferSize,
* _Inout_opt_ BOOL *pfSave,
* _In_ DWORD dwFlags
* );
*
* @return
*/
int CredUIPromptForWindowsCredentialsW(
PointerByReference pUiInfo,
int dwAuthError,
ULONGByReference pulAuthPackage,
Pointer pvInAuthBuffer,
ULONG ulInAuthBufferSize,
PointerByReference ppvOutAuthBuffer,
ULONGByReference pulOutAuthBufferSize,
IntByReference pfSave,
int dwFlags
);
/**
* CREDUI_INFO
*
* typedef struct _CREDUI_INFO {
* DWORD cbSize;
* HWND hwndParent;
* PCTSTR pszMessageText;
* PCTSTR pszCaptionText;
* HBITMAP hbmBanner;
* } CREDUI_INFO, *PCREDUI_INFO;
*/
public static class CREDUI_INFO extends Structure {
public int cbSize;
public HWND hwndParent;
public WString pszMessageText;
public WString pszCaptionText;
public HBITMAP hbmBanner;
/**
* getFieldOrder
* @return
*/
@Override
protected List getFieldOrder() {
return Arrays.asList(new String[]{
"cbSize",
"hwndParent",
"pszMessageText",
"pszCaptionText",
"hbmBanner",
});
}
}
}
並致電:
Credui.CREDUI_INFO info = new Credui.CREDUI_INFO();
info.cbSize = info.size();
info.pszCaptionText = new WString(caption);
info.pszMessageText = new WString(message);
PointerByReference pinfo = new PointerByReference(info.getPointer());
WinDef.ULONGByReference authPackage = new WinDef.ULONGByReference();
PointerByReference outCredBuffer = new PointerByReference();
WinDef.ULONGByReference outCredSize = new WinDef.ULONGByReference();
IntByReference save = new IntByReference(0);
WinDef.ULONG ulInAuthBufferSize = new WinDef.ULONG(0);
int result = Credui.INSTANCE.CredUIPromptForWindowsCredentialsW(pinfo, 0, authPackage,
null, ulInAuthBufferSize, outCredBuffer, outCredSize, save, 0);
if( result == 0 ) {
}
我試圖在pUiInfo
中將CredUIPromptForWindowsCredentialsW
聲明為Pointer
或PointerByReference
。
函數CredUIPromptForWindowsCredentialsW
返回代碼160(“錯誤參數”)。這是什么問題?
因為Java在“按值”和“按引用”之間沒有區別,所以JNA會根據最常見的用法模式來推斷它應用於給定的Structure
用法。
在這種情況下,本機PCREDUI_INFO
表示struct*
,這是struct作為函數參數的更常見用法。 JNA將默認使用您的Structure
的已分配內存的地址作為本機參數,並在本機調用之前和之后自動將Structure
的Java字段與本機內存同步。
如果僅傳遞Structure.getPointer()
,則將不會執行任何同步,並且您的本機代碼將獲得具有未定義內容的內存塊(因此會出現“錯誤參數”錯誤)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.