Click here to Skip to main content
15,908,776 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to return char* Pin
Richard MacCutchan30-Nov-11 22:30
mveRichard MacCutchan30-Nov-11 22:30 
GeneralRe: How to return char* Pin
«_Superman_»30-Nov-11 22:35
professional«_Superman_»30-Nov-11 22:35 
GeneralRe: How to return char* Pin
Richard MacCutchan30-Nov-11 23:45
mveRichard MacCutchan30-Nov-11 23:45 
JokeRe: How to return char* Pin
«_Superman_»30-Nov-11 23:56
professional«_Superman_»30-Nov-11 23:56 
AnswerRe: How to return char* Pin
Richard Andrew x6430-Nov-11 19:51
professionalRichard Andrew x6430-Nov-11 19:51 
GeneralRe: How to return char* Pin
Paulraj G30-Nov-11 20:01
Paulraj G30-Nov-11 20:01 
GeneralRe: How to return char* Pin
«_Superman_»30-Nov-11 20:13
professional«_Superman_»30-Nov-11 20:13 
GeneralRe: How to return char* Pin
Stefan_Lang6-Dec-11 0:33
Stefan_Lang6-Dec-11 0:33 
First of all, if your function is part of a DLL called from outside that DLL, then memory allocated inside your function may not be released by the caller!

The reason is that the caller may not use the same memory mapping and therefore cannot manipulate the heap form your DLL.

There are various ways around that:

1. the easiest is to provide a release function in addition to your original one. The only pupose of that function is to release the buffer you previously allocated, and the caller of your function should call it once it doesn't need your string anymore.

2. A somewhat better solution, and in fact widely used, is to add another parameter to your function: a string buffer that the caller must pass to your DLL, so cou can copy the resulting string into it. Of course you must verify the buffer is big enough or else issue an appropriate error or warning. You couls also provide an additional function that simply returns the size you require the buffer to be, so the caller could first call this function, then allocate the memory and pass the resulting buffer to your function.

3. There are other ways, such as using Shared memory, but I think that would be overkill.

Here's a suggestion based on variant 2 above:
C++
// yourfile.h
extern "C" std::size_t requiredSize();
extern "C" std::size_t getString(char* buffer);

// yourfile.cpp
char mystring[] = "hello world";     // just an example
std::size_t requiredSize() {
   return sizeof(mystring) / sizeof(char);  // an upper limit to the size required for getString
}
std::size_t getString(char* buffer) {
   if (!buffer)                      // error: no buffer provided!
       return 0;                     // response: 0 bytes copied
   strcpy(buffer, mystring);         // hope for the best and just copy
   return requiredSize();            // return the length of the copied string
}

And here's how to use it from C++:
C++
#include "yourfile.h"
void foo() {
   std::size_t sz = requiredSize();  // inquire required size
   char* mybuffer = new char[sz];    // allocate a sufficiently large buffer
   getString(buffer);                // get the string
   puts(buffer);                     // do something with the string (in this case, print it)
   delete [] buffer;                 // release the buffer
}

AnswerRe: How to return char* Pin
Software_Developer30-Nov-11 20:25
Software_Developer30-Nov-11 20:25 
QuestionRe: How to return char* Pin
CPallini1-Dec-11 1:53
mveCPallini1-Dec-11 1:53 
GeneralRe: How to return char* Pin
Addy Tas1-Dec-11 2:14
Addy Tas1-Dec-11 2:14 
AnswerRe: How to return char* Pin
Erudite_Eric1-Dec-11 4:09
Erudite_Eric1-Dec-11 4:09 
GeneralRe: How to return char* Pin
Addy Tas1-Dec-11 7:12
Addy Tas1-Dec-11 7:12 
GeneralRe: How to return char* Pin
Stefan_Lang6-Dec-11 1:41
Stefan_Lang6-Dec-11 1:41 
GeneralRe: How to return char* Pin
Erudite_Eric6-Dec-11 3:40
Erudite_Eric6-Dec-11 3:40 
Questiondxf extraction in c Pin
tanatswa3330-Nov-11 10:44
tanatswa3330-Nov-11 10:44 
AnswerRe: dxf extraction in c Pin
Albert Holguin30-Nov-11 11:43
professionalAlbert Holguin30-Nov-11 11:43 
GeneralRe: dxf extraction in c Pin
tanatswa3330-Nov-11 12:00
tanatswa3330-Nov-11 12:00 
GeneralRe: dxf extraction in c Pin
Albert Holguin30-Nov-11 16:49
professionalAlbert Holguin30-Nov-11 16:49 
GeneralRe: dxf extraction in c Pin
enhzflep30-Nov-11 17:51
enhzflep30-Nov-11 17:51 
GeneralRe: dxf extraction in c Pin
Albert Holguin1-Dec-11 3:26
professionalAlbert Holguin1-Dec-11 3:26 
GeneralRe: dxf extraction in c Pin
Richard MacCutchan30-Nov-11 22:11
mveRichard MacCutchan30-Nov-11 22:11 
GeneralRe: dxf extraction in c Pin
Addy Tas1-Dec-11 8:38
Addy Tas1-Dec-11 8:38 
AnswerRe: dxf extraction in c Pin
Software_Developer30-Nov-11 23:16
Software_Developer30-Nov-11 23:16 
Questionc++ SQL Server Rowset Column Buffer Pin
jkirkerx30-Nov-11 9:02
professionaljkirkerx30-Nov-11 9:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.