本文介绍了参数传入本地方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(遍历模块)

long int
Traverser(const char * sTraversingRoot)
{
    long int nCount;
    struct stat rStatBuf;
    time_t nTime;
    char sActualPath[512];
    PGconn *pConn;

    // Open DB connection
    sprintf(sConnInfo,
        "hostaddr=%s port=%s connect_timeout=50 dbname=%s user=%s password=%s",
        sIP, sPort, sDBName, sLogin, sPassword);
    pConn = PQconnectdb(sConnInfo);
    if (PQstatus(pConn) == CONNECTION_BAD) {
        AddErrorToLog("No connect\n");
        return 0;
    }

    GetActualPath(sActualPath, sTraversingRoot);

    if (*sActualPath) {
        stat(sActualPath, &rStatBuf);
    } else {
        stat("/", &rStatBuf);
    }

    if (nClock)
        nTime = time(NULL);

    if(S_ISREG(rStatBuf.st_mode)) {
        nCount = 1;
        ProcessFile(pConn, sActualPath);
    }

    if(S_ISDIR(rStatBuf.st_mode)) {
        nCount = _Traverser(pConn, sActualPath);
    }

    if (nClock)
        fprintf(stdout, "Total time : %u second(s)\n", time(NULL) - nTime);
    // Close DB connection
    PQfinish(pConn);

    return nCount;
}

我想使用相同的名称创建一个Java上的方法

I want to create native with the same name a method on Java

public native void Traverser(String path)

分别在traverser.c模块中会有一个函数

Respectively in the traverser.c module there will be a function

JNIEXPORT void JNICALL Java_ParallelIndexation_Traverser(JNIEnv *env, jobject obj, jstring path)

Java_ParallelIndexation_Traverser函数是遍历器的Traverser函数包装器。问题是:如何从Java_ParallelIndexation_Traverser中的Traverser遍历器调用模块,传递参数jstring path,从而将其转换为const char *(签名Traverser见上文)?

The Java_ParallelIndexation_Traverser function is a Traverser function wrapper from the traverser.c module.The question is: How to call a module from Traverser traverser.c in Java_ParallelIndexation_Traverser, passing it the parameter jstring path, thus converting it to a const char * (signature Traverser see above)?

推荐答案

我正确理解你的问题:如何实现Java_ParallelIndexation_Traverser,以便它调用非托管Traveser函数?

Did I understand your question correctly: how to implement Java_ParallelIndexation_Traverser so that it calls the unmanaged Traveser function?

如果是这样,如下:

JNIEXPORT void JNICALL Java_ParallelIndexation_Traverser(JNIEnv* env, jobject obj, jstring path)
{
    const jbyte* path2 = env->GetStringUTFChars(path, nullptr);
    if (path2 == nullptr)
        return;

    ::Traverser(path2);

    env->ReleaseStringUTFChars(path, path2);
}

编辑

说明:JNIEnv :: GetStringUTFChars将jstring转换为字节数组。然后需要调用JNIEnv :: ReleaseStringUTFChars来释放该字节数组。

Explanation: JNIEnv::GetStringUTFChars converts a jstring to a byte array. You then need to call JNIEnv::ReleaseStringUTFChars to deallocate that byte array.

这篇关于参数传入本地方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 17:29