这是我的 JNI C 代码:
JNIEXPORT jint JNICALL Java_org_avuna_httpd_util_CLibJNI_bind(JNIEnv * this, jclass cls, jint sockfd, jint family, jstring path, jint len) {
struct sockaddr_un sun;
sun.sun_family = family;
const char *npath = (*this)->GetStringUTFChars(this, path, 0);
sun.sun_path = npath;
return bind(sockfd, sun, sizeof(&npath));
}
我只是猜不成功(sizeof(&npath)
)如何获取长度。我不太精通 C,但我认为有一种方法可以从 jstring 中获取长度。
Google 没有任何结果,我错过了什么基本的东西?
最佳答案
由于变量的类型是 const char *
这表明它是一个以 nul
结尾的字符串,因此
size_t length = strlen(npath);
应该足够了。
尽管您对 bind()
的调用是错误的,但您应该传递地址结构的大小,为此
return bind(sockfd, sun, sizeof(sun));
应该是正确的。
关于java - 获取 jstring/char * 的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30361046/