首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JNA Java CredUIPromptForWindowsCredentialsW

JNA Java CredUIPromptForWindowsCredentialsW
EN

Stack Overflow用户
提问于 2016-04-20 20:55:03
回答 1查看 163关注 0票数 0

我正在尝试将此函数转换为JNA:

代码语言:javascript
复制
/*
 * 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",
            });
        }
    }
}

并呼叫:

代码语言:javascript
复制
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 ) {         
}

我尝试在CredUIPromptForWindowsCredentialsW中将pUiInfo声明为PointerPointerByReference

Function CredUIPromptForWindowsCredentialsW返回代码160 (“错误参数”)。出什么问题了?

EN

回答 1

Stack Overflow用户

发布于 2016-04-21 20:23:43

因为Java没有“按值”和“按引用”的区别,所以JNA根据最常见的使用模式推断它应该用于给定的Structure使用。

在本例中,本机PCREDUI_INFO表示struct*,这是更常见的将结构用作函数参数的用法。JNA将默认使用Structure分配的内存地址作为本机参数,并在本机调用前后自动将Structure的Java域与本机内存同步。

如果您只传递Structure.getPointer(),则不会执行同步,并且您的本机代码将获得具有未定义内容的内存块(因此您的“坏参数”错误)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36744472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档