C:
					
void example2_sendString(const char* pszVal)
{
	/ /注意:printf调用目前是不会打印的,直到Java调用完成才会打印
	printf("(C) '%s'\n", pszVal);
}

void example2_getString(char** ppszVal)
{
	*ppszVal = (char*)malloc(sizeof(char) * 6);
	memset(*ppszVal, 0, sizeof(char) * 6);
	strcpy(*ppszVal, "hello");
}

void example2_cleanup(char* pszVal)
{
	free(pszVal);
}
					
				
Java:
					
public interface CLibrary extends Library {
	public void example2_sendString(String val);
	public void example2_getString(PointerByReference val);
	public void example2_cleanup(Pointer p);
}
...
CLibrary clib = (CLibrary)Native.loadLibrary("testlib", CLibrary.class);
...
// send a string to C
clib.example2_sendString("fooey");


/ /得到字符串从C
/ /分配一个void * * 
PointerByReference ptrRef = new PointerByReference();
//调用动态库的函数
clib.example2_getString(ptrRef);
//获取指针的值
Pointer p = ptrRef.getValue();
//   提取以null结尾的字符串的指针
String val = p.getString(0);
System.out.println("example 2b: " + val);

//释放资源
clib.example2_cleanup(p);
			

//www.sitcoder.com
				        
                        
                                                                    
						
						
						
									  				


	
色迷迷 哭 呕吐 大笑 口水 微笑 啵一个 发怒

Hi,您需要填写昵称和邮箱!

  • 必填项
  • 必填项