c++ - JNA 指针到指针

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

我正在尝试使用 JNA 并使用指向指针的指针执行,但我一直遇到麻烦。我可以使用类中的其他函数,但是该函数中的参数给我带来了麻烦。我的界面和java工作正常,我可以使用其他方法,但是“execute_file”和“alenkaexecute”给我带来了麻烦。我的方法声明关闭了吗?我在想也许我应该有一个字符串数组而不是 PointerByReference?在底部,我将 c++ 包含在execute_file 中。

谢谢!

public interface libcvm extends Library{
        void alenkaInit(PointerByReference av);
        int execute_file(int ac, PointerByReference av);
        void alenkaClose();
        int alenkaExecute(ByteBuffer s); 
    }
        public static void main(String[] args) {
        libcvm libcvm = (libcvm) Native.loadLibrary("libcvm.so", libcvm.class);
        PointerByReference pref = new PointerByReference();
        libcvm.execute_file(2, pref);
        Pointer p = pref.getValue();    
    }


int execute_file(int ac, char **av)
{
bool just_once = 0;
string script;

    process_count = 6200000;
    verbose = 0;
total_buffer_size = 0;

    for (int i = 1; i < ac; i++) {
        if(strcmp(av[i],"-l") == 0) {
            process_count = atoff(av[i+1]);
        }
        else if(strcmp(av[i],"-v") == 0) {
            verbose = 1;
        }
        else if(strcmp(av[i],"-i") == 0) {
            interactive = 1;
break;
        }
        else if(strcmp(av[i],"-s") == 0) {
            just_once = 1;
interactive = 1;
script = av[i+1];
        };  
    };

load_col_data(data_dict, "data.dictionary");

    if (!interactive) {
        if((yyin = fopen(av[ac-1], "r")) == NULL) {
            perror(av[ac-1]);
            exit(1);
        };

        if(yyparse()) {
            printf("SQL scan parse failed\n");
            exit(1);
        };

//exit(0);

        scan_state = 1;
        std::clock_t start1 = std::clock();

load_vars();

        statement_count = 0;
        clean_queues();

        yyin = fopen(av[ac-1], "r");
        PROC_FLUSH_BUF ( yyin );
        statement_count = 0;

        extern FILE *yyin;
        context = CreateCudaDevice(0, av, verbose);
        hash_seed = 100;

        if(!yyparse()) {
            if(verbose)
            cout << "SQL scan parse worked " << endl;
        }
        else
            cout << "SQL scan parse failed" << endl;

        fclose(yyin);
        for (map<string,CudaSet*>::iterator it=varNames.begin() ; it != varNames.end(); ++it ) {
            (*it).second->free();
        };

        if(alloced_sz) {
            cudaFree(alloced_tmp);
        };

        if(verbose) {
            cout<< "cycle time " << ( ( std::clock() - start1 ) / (double)CLOCKS_PER_SEC ) << " " << getFreeMem() << endl;
        };
    }
    else {
        context = CreateCudaDevice(0, av, verbose);
        hash_seed = 100;
if(!just_once)
getline(cin, script);   

        while (script != "exit" && script != "EXIT") {

used_vars.clear();
            yy_scan_string(script.c_str());
            scan_state = 0;
            statement_count = 0;
            clean_queues();
            if(yyparse()) {
                printf("SQL scan parse failed \n");
                getline(cin, script);
                continue;
            };

            scan_state = 1;

load_vars();

            statement_count = 0;
            clean_queues();
            yy_scan_string(script.c_str());
            std::clock_t start1 = std::clock();

            if(!yyparse()) {
                if(verbose)
                    cout << "SQL scan parse worked " << endl;
            };
            for (map<string,CudaSet*>::iterator it=varNames.begin() ; it != varNames.end(); ++it ) {
                (*it).second->free();
            };
            varNames.clear();

            if(verbose) {
                cout<< "cycle time " << ( ( std::clock() - start1 ) / (double)CLOCKS_PER_SEC ) << endl;
            };
if(!just_once)
getline(cin, script);
else
script = "exit";
        };
        if(alloced_sz) {
            cudaFree(alloced_tmp);
            alloced_sz = 0;
        };

while(!buffer_names.empty()) {
delete [] buffers[buffer_names.front()];
buffer_sizes.erase(buffer_names.front());
buffers.erase(buffer_names.front());
buffer_names.pop();
};

    };
if(save_dict)
save_col_data(data_dict,"data.dictionary");
    return 0;
}

最佳答案

您的 API 要求提供 char**,在常见的 C 语言中,它是一个字符串数组。

如果您使用String[]作为参数类型,JNA将自动处理转换。

关于c++ - JNA 指针到指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23219256/

相关文章:

c++ - 在这种情况下,通过构造函数或显式函数进行隐式转换更好吗?

c++ - 工厂模式中的模板

属性的 C++ 继承替代方案

linux - UID 程序导致无效字符

c - 从命令行参数创建链表

c++ - 无法使用现有的子类创建窗口 (CreateWindowEx)

c - 使用命名管道的程序崩溃

c++ - 指向整数数组的指针

c - 指针运算 : strange result in C programm

c++ - 分配指针时出现段错误