?
Solved

Calling C library from C++ program

Posted on 2014-02-21
1
Medium Priority
?
433 Views
Last Modified: 2014-03-05
I have C++ based java native methods that works perfectly in passing String parameters back and forth with Java but wanted to know how could i load the C library from this C++ program and pass the parameters into it and get return string back from it.

The reason I want to not interact with C directly is because I need to pass 32 string parameters and handling them may be a challenge. If there is a easy way to handle 32 parameters in C  that will be great.
0
Comment
Question by:jgdvishnu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 1500 total points
ID: 39877819
You can call C libraries from C++ code just like other C++ libraries. E.g.

// lib.h

size_t get_string(char* buf, const size_t bufsz);

Open in new window


// lib.c

size_t get_string(char* buf, const size_t bufsz) {

  const char* pText = "This is my output";
  size_t len = strlen(pText);

  if (len  + 1 > bufsz) return 0; // not enough room

  strcpy(buf,pText);

  return len;
}

Open in new window


//usingclib.cpp
#include "lib.h"
#include <string>
#inlude <iostream>
// ...

  char str[255];
  std::string string_from_c_lib;

  if (0 < get_text(str,sizeof(str)) {

    string_from_c_lib = str;

    std::cout << "Got that from C lib: " << string_from_c_lib << std::endl;
  }

Open in new window

0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question