如何在Java中传递宽字符串,以在user32 lib中调用MessageBoxW函数 [英] How to pass wide strings in Java calling the MessageBoxW function in user32 lib

查看:95
本文介绍了如何在Java中传递宽字符串,以在user32 lib中调用MessageBoxW函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在努力使用Java中的功能MessageBoxW.我已经成功地成功调用了user32库并使用了MessageBoxA函数.这是我的代码如下:

I am currently struggling to use the function MessageBoxW in Java. I have managed to successfully call the user32 library and use the MessageBoxA function. Here's my code below:

package messagebox;

import com.sun.jna.Library;
import com.sun.jna.Native;

public class MessageBox 
{
    public interface user32 extends Library
    {
        public int MessageBoxA(int something, String text, String caption, int flags);
    }

    public static void main(String[] args) 
    {
        System.out.println("Program starting... Library loading... ");
        user32 lib = (user32) Native.loadLibrary("user32", user32.class);
        System.out.println("Presenting Message Box ...");
        lib.MessageBoxA(0,"MessageBox success!!!","Attention",0);
    }
}

我环顾四周,找不到任何结论性的答案,但有一点告诉我,我需要更复杂的编码来解决此问题.任何帮助,将不胜感激.

I've looked around and could not find any conclusive answers but something tells me that I need much more sophisticated coding to tackle this problem. Any help with this would be much appreciated.

推荐答案

查看文档(证实我的怀疑).默认情况下,具有16位字符的Java字符串将转换为8位字符串,以使其适合C char*类型.

Looking at the documentation (confirming my suspicions). Java strings, which by default have 16 bit characters, are converted to 8 bit character strings so they fit the C char* type.

但是,如果您实际上想在C端打印宽(16位)字符,会发生什么情况?通过转换java字符串获得的ascii值将被解释为unicode值,因此它们与您键入的内容不匹配.

But what happens if you actually want to print wide (16 bit) characters on the C side? The ascii values that were obtained from converting the java string are interpreted as unicode values, so they don't match what you typed.

文档中提到使用WString类型来避免转换为ascii.

The documentation mentions using the WString type to avoid the conversion to ascii.

您应将方法签名更改为:

You should change the method signature to:

int MessageBoxW(int something, WString text, WString caption, int flags);

然后调用:

lib.MessageBoxW(0, new WString("MessageBox success!!!"), new WString("Attention"), 0);


通过jna-4.2.2测试


Tested with jna-4.2.2

这篇关于如何在Java中传递宽字符串,以在user32 lib中调用MessageBoxW函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆