JNA指向指针

时间:2014-04-22 12:14:04

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

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

谢谢!

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;
}

1 个答案:

答案 0 :(得分:1)

您的API要求char**,这与C相同,是一个字符串数组。

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