java - 获取 jstring/char * 的长度?

标签 java c++ c java-native-interface

这是我的 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/

相关文章:

c++ - 从 10 以外的其他基数的输入流中读取

c++ - opencv数组大于255

c - 将结构指针设置为等于另一个结构的地址

c++ - 将异步操作的处理程序绑定(bind)到另一个类的非静态成员

c - 如何为需要多次输入的 C 程序编写 Bash 脚本?

java - JMX - 监视进程存在

Java 套接字应用程序在尝试发送文件时发送额外的字节

java - 对象数组的空指针异常

java - 无法从spark中的java代码启 Action 业;初始作业尚未接受任何资源

c++ - 工厂 : how to pass temporary smart pointers to functions. C++