swig char*通过函数参数,以java的byte[]模式
example.i
%module example
%{
#include <example.h>
%}
%include "various.i"
extern void modifyString(char *BYTE, int maxlen);
example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
extern "C"{
extern int gcd(int x, int y);
extern double Foo;
extern char* modifyAndReturnNewString(const char* input);
extern void modifyString(char *str, int maxlen);
}
#endif // EXAMPLE_H
example.cpp
#include "example.h"
#include <ctype.h>
#include <string.h>
/* A global variable */
double Foo = 3.0;
/* Compute the greatest common divisor of positive integers */
int gcd(int x, int y) {
    int g;
    g = y;
    while (x > 0) {
        g = x;
        x = y % x;
        y = g;
    }
    return g;
}
char* modifyAndReturnNewString(const char* input) {
    char* result = strdup(input); // 复制输入
    result[0] = 'X'; // 修改
    return result; // 返回新字符串(需在 Java 端释放)
}
void modifyString(char *str, int maxlen) {
    if (maxlen > 0) {
        str[0] = 'X'; // 修改第一个字节
    }
}
example_wrap.cxx
#include <example.h>
#ifdef __cplusplus
extern "C" {
#endif
SWIGEXPORT void JNICALL Java_com_example_exampleJNI_modifyString(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jint jarg2) {
  char *arg1 = (char *) 0 ;
  int arg2 ;
  
  (void)jenv;
  (void)jcls;
  {
    arg1 = (char *) jenv->GetByteArrayElements(jarg1, 0); 
  }
  arg2 = (int)jarg2; 
  modifyString(arg1,arg2);
  {
    jenv->ReleaseByteArrayElements(jarg1, (jbyte *) arg1, 0);//0	将修改后的数据复制回 Java 数组,并释放本地数组
                                                             //JNI_COMMIT	将修改后的数据复制回 Java 数组,但不释放本地数组(仍可继续使用)
                                                             //JNI_ABORT	不复制修改回 Java 数组,直接释放本地数组
  }
  
}
#ifdef __cplusplus
}
#endif
java
package Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import com.example.example;
import com.example.exampleJNI;
public class MainWindow {
	protected Shell shell;
	 static {
	    try {
		//System.loadLibrary("example");
		System.load("D:\\qtprojects\\example\\build\\Desktop_Qt_5_15_2_MSVC2019_64bit-Release\\release\\example.dll");
	    } catch (UnsatisfiedLinkError e) {
	      System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
	      System.exit(1);
	    }
	  }
	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			MainWindow window = new MainWindow();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(585, 490);
		shell.setText("SWT Application");
		Button btnFun = new Button(shell, SWT.NONE);
		btnFun.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				byte[] buffer = "test".getBytes();
				example.modifyString(buffer, buffer.length);
				String result = new String(buffer);
				System.out.println(result); // 输出 "Xest"				
			}
		});
		btnFun.setBounds(10, 10, 80, 27);
		btnFun.setText("fun1");
	}
}
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号