Java本机接口和Windows窗体

Java本机接口和Windows窗体,java,c++,winforms,java-native-interface,Java,C++,Winforms,Java Native Interface,所以,我有一个VisualStudio consloe项目,可以编译成.dll文件。 我已经创建了简单的windows窗体System::windows::Forms::form 我创建了一个.java文件: import java.io.Serializable; public class MyBean implements Serializable{ /** * */ static{ System.loadLibrary("MyB

所以,我有一个VisualStudio consloe项目,可以编译成.dll文件。 我已经创建了简单的windows窗体
System::windows::Forms::form

我创建了一个.java文件:

 import java.io.Serializable;


public class MyBean implements Serializable{

    /**
     * 
     */
    static{
        System.loadLibrary("MyBean");
}
    private static final long serialVersionUID = 1L;


    private static native String getDateCpp();


    public String getDate(){
        return getDateCpp();
    }



}
通过javah编译并生成一个.h文件:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class MyBean */

#ifndef _Included_MyBean
#define _Included_MyBean
#ifdef __cplusplus
extern "C" {
#endif
#undef MyBean_serialVersionUID
#define MyBean_serialVersionUID 1i64
/*
 * Class:     MyBean
 * Method:    getDateCpp
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_MyBean_getDateCpp
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif
日志很长,我不会在这里发布

我做错了什么?

  • wcstombs
    从不返回小于0的值。请看这里:

  • <>这是标记C++,所以我认为你应该使用<代码> EnV-><代码>代替(*Env)。< />代码>除了我没什么大不了的

  • 我不确定您是否需要它,但我认为您缺少JNI函数之前的
    extern“C”

  • 最后,确保您的呼叫约定是正确的。不久前,我遇到了同样的问题/异常/转储:

    我通过更改调用约定并使用.def文件导出解决了这个问题。我相信你所需要解决的只是电话会议。不需要.def文件;我想我还是要提一下


    无论何时出现该错误,都是由于调用约定问题造成的。它很少是由于权限,但这也是需要考虑的问题。也许您需要以管理员身份运行。

    我调用的
    form->Show()有一个错误form->ShowDialog()

    从MSDN:

    ShowDialog()

    可以使用此方法在对话框中显示模式对话框 应用调用此方法时,它后面的代码不会被调用 直到对话框关闭后执行

    Show()

    显示控件相当于设置“可见” 属性设置为true。调用Show方法后,将显示 属性返回值true,直到调用Hide方法为止


    所以,当我调用show时,我得到了要转换的空字符串,我的窗口只显示了一秒钟或更短的时间,现在它是模态的,等待关闭,然后执行其余的代码。JNI实施正确,工作正常。

    好的,我会查看您的建议。我的.h文件中有
    extern“C”
    ,我是否也应该将其添加到.cpp文件中?错误很简单。我应该使用
    showDialog()
    而不是
    show()
    ,现在它可以工作了,谢谢你的提示和帮助。我不知道你的代码有什么问题,但它可以简单得多。在Windows上,Java使用代码单位计数、UTF-16LE编码的Unicode字符串。NET使用代码单位计数、UTF-16LE编码的Unicode字符串…bliss。因此,获取.NET字符串的“长度”,用
    PtrToStringChars
    固定它,并将该信息传递给
    env->NewString
    ;解开,你就完了。(NewStringGutf和GetStringCharsUTF使用修改的UTF_8编码,这是非Unicode函数的支柱。)谢谢,你的评论很有价值,但它可以工作,正如我在下面提到的答案,问题在于
    show()
    ,我应该使用
    showDialog()
    “它可以工作”,真的吗?将“你赢得了曼谷之旅和150000英镑”粘贴到你的文本框中,看看会发生什么。
    #ifdef _MSC_VER
    #define _CRT_SECURE_NO_WARNINGS
    #endif
    
    #include "Form1.h"
    #include "MyBean.h"
    #include <string>
    #include <vcclr.h>
    using namespace std;
    
    
    bool To_string( String^ source, string &target )
    {
        pin_ptr<const wchar_t> wch = PtrToStringChars( source );
        int len = (( source->Length+1) * 2);
        char *ch = new char[ len ];
        bool result = wcstombs( ch, wch, len ) != -1;
        target = ch;
        delete ch;
        return result;
    }
    
    JNIEXPORT jstring JNICALL Java_MyBean_getDateCpp
        (JNIEnv * env, jclass jcl){
            Form1^ form = gcnew Form1();
            form->Show();
    
            String^ text = form->text;
            string stdString = "";
    
    
            if(To_string(text,stdString))
                return  (*env).NewStringUTF(stdString.c_str());
            else
                return (*env).NewStringUTF("blad");
    }
    
    #
    # A fatal error has been detected by the Java Runtime Environment:
    #
    #  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000007f9324f811c, pid=4404, tid=4800
    #
    # JRE version: Java(TM) SE Runtime Environment (7.0_45-b18) (build 1.7.0_45-b18)
    # Java VM: Java HotSpot(TM) 64-Bit Server VM (24.45-b08 mixed mode windows-amd64 compressed oops)
    # Problematic frame:
    # C  [KERNELBASE.dll+0x3811c]
    #
    # Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
    #
    # An error report file with more information is saved as:
    # D:\studia\semestr 7\java\lab05\hs_err_pid4404.log
    #
    # If you would like to submit a bug report, please visit:
    #   http://bugreport.sun.com/bugreport/crash.jsp
    # The crash happened outside the Java Virtual Machine in native code.
    # See problematic frame for where to report the bug.
    #