Using C function in a C++ project 
Author Message
 Using C function in a C++ project

Ok,
I have a C++ project and I call a C function by using
extern "C" char* MyFunction();

This works great if I have for example cause MyFunction to print to the
screen.
My question is how to I return a string or an array from MyFunction?

Thank-you



Tue, 13 Dec 2005 02:26:21 GMT  
 Using C function in a C++ project

Quote:
> I have a C++ project and I call a C function by using
> extern "C" char* MyFunction();

> This works great if I have for example cause MyFunction to print to
the
> screen.
> My question is how to I return a string or an array from MyFunction?

With a "return" statement, of course. The problem here, the caller and
the callee should agree up front about the ownership of the memory
occupied by the string, and ways in which said memory is allocated and
deleted. Typical examples include:

asctime - returns a pointer to a fixed size static buffer. Every call
overwrites the same buffer, so the caller is advised to copy the string
into its own memory as soon as asctime returns. This approach is also a
pain in multithreaded programs.

strdup - allocates memory with malloc. The caller is responsible for
freeing the memory with free() when it is done with it.

It is often a good idea to take a pointer to memory buffer, and its
size, as parameters, and fill in the caller-provided buffer. This
removes all lifetime and ownership issues. See e.g. mbstowcs, and most
Win32 APIs that take strings, such as GetWindowText.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Tue, 13 Dec 2005 02:48:21 GMT  
 Using C function in a C++ project


Quote:


> > I have a C++ project and I call a C function by using
> > extern "C" char* MyFunction();

> > This works great if I have for example cause MyFunction to print to
> the
> > screen.
> > My question is how to I return a string or an array from MyFunction?

> With a "return" statement, of course. The problem here, the caller and
> the callee should agree up front about the ownership of the memory
> occupied by the string, and ways in which said memory is allocated and
> deleted. Typical examples include:

I got it to work. I do a
char CommaProcs[2000]

and then
return CommaProcs;

On the C++ side I enumerate through the char and parse it out to what I need.
Thank-you



Tue, 13 Dec 2005 03:39:53 GMT  
 Using C function in a C++ project

Quote:
> I got it to work. I do a
> char CommaProcs[2000]

> and then
> return CommaProcs;

If that is local you have a problem. It may "appear" to work but effectively
you accessing old memory on the stack. Only if it is locally

static char CommaProcs[2000]

is it fine. What Igor suggested is a good solution

Cheers

Stephen Howe



Tue, 13 Dec 2005 03:49:10 GMT  
 Using C function in a C++ project


Quote:
>> If that is local you have a problem. It may "appear" to work but effectively
> you accessing old memory on the stack. Only if it is locally

> static char CommaProcs[2000]

> is it fine. What Igor suggested is a good solution

> Cheers

> Stephen Howe

Thanks for the tip. That is what I am doing now...


Tue, 13 Dec 2005 03:53:47 GMT  
 Using C function in a C++ project
        You cannot use local variable's ( stack variable ) address as a
return value of a function. The moment the function exits, the variable [
for which memory is allocated on the stack ] will get destroyed and any
reference to it from outside the function will result in a memory read
error. Of course you can make it a 'static' variable, but it has its own
issues.

    Read Igor's reply once again. The best way is to pass a pointer to a
memory buffer & its size and fill that buffer inside the function.

--
Cheers
Check Abdoul [ VC++ MVP ]
-----------------------------------


Quote:





> > > I have a C++ project and I call a C function by using
> > > extern "C" char* MyFunction();

> > > This works great if I have for example cause MyFunction to print to
> > the
> > > screen.
> > > My question is how to I return a string or an array from MyFunction?

> > With a "return" statement, of course. The problem here, the caller and
> > the callee should agree up front about the ownership of the memory
> > occupied by the string, and ways in which said memory is allocated and
> > deleted. Typical examples include:

> I got it to work. I do a
> char CommaProcs[2000]

> and then
> return CommaProcs;

> On the C++ side I enumerate through the char and parse it out to what I
need.
> Thank-you



Tue, 13 Dec 2005 03:54:39 GMT  
 Using C function in a C++ project

Quote:



>>> If that is local you have a problem. It may "appear" to work but
>>> effectively
>> you accessing old memory on the stack. Only if it is locally

>> static char CommaProcs[2000]

>> is it fine. What Igor suggested is a good solution

>> Cheers

>> Stephen Howe

> Thanks for the tip. That is what I am doing now...

Note that this approach is not thread safe.  If thread safety is not an
issue to you, then you have nothing to fear.  There is a way to make this
thread safe, however.

--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone.  My wish has come true.  I no longer know how to use my
telephone."
 - Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]



Tue, 13 Dec 2005 04:43:14 GMT  
 Using C function in a C++ project


Quote:



> > Thanks for the tip. That is what I am doing now...

> Note that this approach is not thread safe.  If thread safety is not an
> issue to you, then you have nothing to fear.  There is a way to make this
> thread safe, however.

Thank-you for your advice. Thread safety is not an issue in this situation.
I am coming from the Pascal side of things. Do you have a simple
explanation for making it thread safe?


Tue, 13 Dec 2005 21:24:03 GMT  
 Using C function in a C++ project

Quote:



>> Note that this approach is not thread safe.  If thread safety is not
>> an issue to you, then you have nothing to fear.  There is a way to
>> make this thread safe, however.

> Thank-you for your advice. Thread safety is not an issue in this
> situation. I am coming from the Pascal side of things. Do you have a
> simple explanation for making it thread safe?

No.  :-)

If this becomes an issue, look up "Thread Local Storage" in MSDN.

--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone.  My wish has come true.  I no longer know how to use my
telephone."
 - Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]



Wed, 14 Dec 2005 01:34:13 GMT  
 Using C function in a C++ project

Quote:






>> > Thanks for the tip. That is what I am doing now...

>> Note that this approach is not thread safe.  If thread safety is not an
>> issue to you, then you have nothing to fear.  There is a way to make this
>> thread safe, however.

>Thank-you for your advice. Thread safety is not an issue in this situation.
>I am coming from the Pascal side of things. Do you have a simple
>explanation for making it thread safe?

There are two approaches.

Pass in the buffer:

void MyFunction(char* userBuffer, int size)
{
        //check size is big enough.
        //write into buffer

Quote:
}

Then the caller does:

vector<char> v(200);
MyFunction(&v[0], v.size());

or
char* p = new char[200];
MyFunction(p, 200);
//use p
delete[] p;

or
char buffer[200]
MyFunction(buffer, sizeof buffer);
//use buffer

Now, MyFunction doesn't have any data other than what's passed in, so
there isn't a thread safety (or re-entrancy) issue.

Approach two is to keep the static array, and protect calls with C++
code. e.g.

#include <string>

std::string MyFunctionWrapper()
{
        //lock_a_mutex; //however you want to do this-Win32, MFC, etc
        std::string result = MyFunction();
        //unlock mutex
        return result;

Quote:
}

Approach 1 is usually preferred with C APIs.

Tom



Wed, 14 Dec 2005 01:57:07 GMT  
 Using C function in a C++ project

Quote:
> Thank-you for your advice. Thread safety is not an issue in this
situation.
> I am coming from the Pascal side of things. Do you have a simple
> explanation for making it thread safe?

No because thread safety is not simple.

Stephen Howe



Wed, 14 Dec 2005 02:32:07 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. CS Game Projects

3. VS.NET: Adding one .cs file to more then one project

4. Compile CS source code using ICodeCompiler

5. Two CS files (using namespaces)

6. Survey on using C as intro CS langauge (?)

7. Survey on using C as intro CS langa

8. Need C++ text for non cs major course

9. How to show/call Form2.cs from Form1.cs ?

10. using STL classes in a mixed C and C++ project

11. Using C files in C++ projects

12. Using graph.h from a C++ project

 

 
Powered by phpBB® Forum Software