pass char* over dll-boundaries

J

John Harrison

Ekim said:
my question is as follows:
I've got a DLL in which I have a method GetBuffer (this one is extern,
exported, is called from outside this program) which shall pass a
char-buffer to the calling-function for further handling.

[snip]

Passing memory from an executable to a DLL is not something that is defined
by the C++ language, so this question is off topic here. But I think you
will find that the problem is that memory allocated in one DLL or executable
cannot be access in another DLL or executable. So that is the problem, not
the way that you have declared the method or the buffer.

For details on how to solve this problem please ask in a topical newsgroup,
for instance.

John
 
J

John Sun

void MyFunction(char* inputString, int inputSize) // here I get the
correct buffer
{
tmpString = inputString; // copy buffer into global variable
tmpLength = inputSize;
}

// now I want to print the buffer in any method like this:
void AnyMethod()
{
printf("Buffer: %s\n", tmpString);
}

I don't see any problem if you do this :
void AnyMethod()
{
char tmpString[_BUFFERSIZE];
MyFunction(inputString, BUFFERSIZE);
printf("Buffer: %s\n", tmpString);

}


Just pay attention to : when you handle a pointer to a memory block to other
routine, if you
accidently delete it in the callee, the caller will have a dangling pointer.

John.

Ekim said:
John Harrison said:
Ekim said:
my question is as follows:
I've got a DLL in which I have a method GetBuffer (this one is extern,
exported, is called from outside this program) which shall pass a
char-buffer to the calling-function for further handling.

[snip]

Passing memory from an executable to a DLL is not something that is defined
by the C++ language, so this question is off topic here. But I think you
will find that the problem is that memory allocated in one DLL or executable
cannot be access in another DLL or executable. So that is the problem, not
the way that you have declared the method or the buffer.

For details on how to solve this problem please ask in a topical newsgroup,
for instance.

John

Good, my question is not really about DLL-boundaries, but about
buffer-passing over functions in c++ in general.

So just a simple example:
----------------
static char* tmpString = NULL;
static int tmpLength = -1;

void MyFunction(char* inputString, int inputSize) // here I get the
correct buffer
{
tmpString = inputString; // copy buffer into global variable
tmpLength = inputSize;
}

// now I want to print the buffer in any method like this:
void AnyMethod()
{
printf("Buffer: %s\n", tmpString);
}

I guess the above code works. But is there for example a possibility to
transform the char* "inputString" I got in MyFunction into a byte-array of
the length "inputSize" I got in MyFunction?

thx
 
H

Howard

Ekim said:
John Harrison said:
Ekim said:
my question is as follows:
I've got a DLL in which I have a method GetBuffer (this one is extern,
exported, is called from outside this program) which shall pass a
char-buffer to the calling-function for further handling.

[snip]

Passing memory from an executable to a DLL is not something that is defined
by the C++ language, so this question is off topic here. But I think you
will find that the problem is that memory allocated in one DLL or executable
cannot be access in another DLL or executable. So that is the problem, not
the way that you have declared the method or the buffer.

For details on how to solve this problem please ask in a topical newsgroup,
for instance.

John

Good, my question is not really about DLL-boundaries, but about
buffer-passing over functions in c++ in general.

Well, your original question *was* about passing between different
applications, which is *ver* different from just passing between functions
in the same application.
So just a simple example:
----------------
static char* tmpString = NULL;
static int tmpLength = -1;

void MyFunction(char* inputString, int inputSize) // here I get the
correct buffer
{
tmpString = inputString; // copy buffer into global variable

This does *not* copy the buffer, it merely makes tmpString point to the same
character(s) as inputString points to. If inputString is deleted, then
tmpString will become an invalid pointer.
tmpLength = inputSize;
}

// now I want to print the buffer in any method like this:
void AnyMethod()
{
printf("Buffer: %s\n", tmpString);
}

I guess the above code works. But is there for example a possibility to
transform the char* "inputString" I got in MyFunction into a byte-array of
the length "inputSize" I got in MyFunction?

What's to transform? You can use a char* pointer in the same fashion as an
array variable. If you need a *separate* char array, then use new
char[size] (and later delete[]) to instantiate a char* pointer (to an array
of char), and strcpy the contents from one string to the other.

But you'll probably save yoursefl a lot of effort if you start using the
std::string class for most string handling.

(So what happened to your original problem of passing the string from a
DLL?)

-Howard
 
P

Peter Koch Larsen

John Sun said:
void MyFunction(char* inputString, int inputSize) // here I get the
correct buffer
{
tmpString = inputString; // copy buffer into global variable
tmpLength = inputSize;
}

// now I want to print the buffer in any method like this:
void AnyMethod()
{
printf("Buffer: %s\n", tmpString);
}

I don't see any problem if you do this :
void AnyMethod()
{
char tmpString[_BUFFERSIZE];

But there is a problem here: _BUFFERSIZE is reserved for the implementation.

MyFunction(inputString, BUFFERSIZE);
printf("Buffer: %s\n", tmpString);

}


Just pay attention to : when you handle a pointer to a memory block to other
routine, if you
accidently delete it in the callee, the caller will have a dangling
pointer.

Always encapsulta these whenever you can

Peter
 
H

Howard

" >
Well, your original question *was* about passing between different
applications, which is *ver* different from just passing between functions
in the same application.

Typo: should have been "*very* different", not "*ver* different".

-Howard
 
E

Ekim

my question is as follows:
I've got a DLL in which I have a method GetBuffer (this one is extern,
exported, is called from outside this program) which shall pass a
char-buffer to the calling-function for further handling.

If I've confused you, here's the code snippet (it's within a simple
Win32-Dll-project):

char* buffer; // global buffer - this one points to my buffer (for
puttiing it simple in here I assume memory is already allocated and buffer
points to the correct string)
int bufferLen; // global bufferlength - value assigned beforehand,
too

extern "C" __declspec(dllexport)
void GetBuffer(int *length, char retBuffer[]) // out-paramaters (buffer
shall be returned)
{
*length = bufferLen;
memcpy(retBuffer, buffer, bufferLen);
}
----------------------------------------------------------------------------
-
I call this function and want to make use of the buffer within a simple
win32-c++-console-application like this:

extern "C" __declspec(dllimport) void GetBuffer(int *length, char
retBuffer[]);

int _tmain(int argc, _TCHAR* argv[])
{
char retBuffer[1000];
int length;
GetBuffer(&length, retBuffer); // call exported function
printf("Buffer: %s\n", retBuffer);
}
---------------------------------------------------------------------------

Still confused? Ok: I just want to get the char*-buffer into my win32-app
that uses this dll - so I just want to ask what I have to do that I can
"export" the char* buffer? I'm not sure if I can use a char*-parameter in
the GetBuffer-function or if I am to use a char[]-array as tried above.
-----
Fact is, that if I execute the above code, I get a memory-access-error - so
I must have made a crucial mistake.

I appreciate your hints,
ekim!
 
E

Ekim

(So what happened to your original problem of passing the string from a

ok ok, to reveal the whole story:
I'm using a pdf-2-text-converter-function from an external vendor which
resides within a dll. This dll forces me to write a callback-function, which
is called when a pdf-file is to be converted. Within this callback-function,
I get a char* that points to the buffer containing the result of the
pdf-converting. The callback function looks like this:

void ContentCallBackFunc(char* i_szStr, int i_size)
{ myBuf = i_szStr; // myBuf is global char*-variable
mySize = i_size; // mySize is global int-variable
}

Now I'm about to write a wrapper around this function (it's again a dll), so
that I can pass in the filename to the pdf-file and get the buffer
containing the converting data back. Let's say a function called
Convert(...) looks something like this:
extern "C" __declspec(dllexport)
void Convert(const char* filename, char **buffer, int *length)
{
convertPdf(filename, (CALLBACK)ContentCallBackFunc); // make use
of the convert-Pdf-function of the external dll --> this implies a call to
ContentCallBackFunc

*buffer = myBuf; // specify output-params
*length = mySize;
}

In any win32-client-program I can use now this external
Convert(...)-function and I shall be able to access the converted buffer. I
make use in a win32-console-app like this:

extern "C" __declspec(dllimport) void Convert(const char* filename, char
**buffer, int *length);

int _tmain(int argc, _TCHAR* argv[])
{
char* buffer = NULL;
int length = -1;
Convert("C:\\test.pdf", &buffer, &length);
printf("Buffer:\t%d\t%s\n", length, buffer); // results in an
memory-access-error!
}

The real question over dll-boundaries is now, whether it is possible to
access the original-buffer from the callback-function just by redirecting
some pointers, or if I have to copy the original pointer to a buffer into a
second buffer-array to be able to access it from another application!?
The key is that I want to avoid copying buffers whenever it is possible,
because my application is very time-critical and eventually it should call
that converting-function thousands of times within a short period. So I
really would prefer just redirecting pointers instead of copying buffers.

So this is true story and real background to my initial question.
Would be fine to receive any hints,
ekim!
 
P

puppet_sock

Ekim said:
If I've confused you, here's the code snippet (it's within a simple
Win32-Dll-project): [snip]
I appreciate your hints,

Probably you should ask this in one of the microsoft.public.vc.*
or one of the msdos or win32 newsgroups.
Socks
 
E

Ekim

John Harrison said:
Ekim said:
my question is as follows:
I've got a DLL in which I have a method GetBuffer (this one is extern,
exported, is called from outside this program) which shall pass a
char-buffer to the calling-function for further handling.

[snip]

Passing memory from an executable to a DLL is not something that is defined
by the C++ language, so this question is off topic here. But I think you
will find that the problem is that memory allocated in one DLL or executable
cannot be access in another DLL or executable. So that is the problem, not
the way that you have declared the method or the buffer.

For details on how to solve this problem please ask in a topical newsgroup,
for instance.

John

Good, my question is not really about DLL-boundaries, but about
buffer-passing over functions in c++ in general.

So just a simple example:
----------------
static char* tmpString = NULL;
static int tmpLength = -1;

void MyFunction(char* inputString, int inputSize) // here I get the
correct buffer
{
tmpString = inputString; // copy buffer into global variable
tmpLength = inputSize;
}

// now I want to print the buffer in any method like this:
void AnyMethod()
{
printf("Buffer: %s\n", tmpString);
}

I guess the above code works. But is there for example a possibility to
transform the char* "inputString" I got in MyFunction into a byte-array of
the length "inputSize" I got in MyFunction?

thx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top