Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pass char* over dll-boundaries

Reply
Thread Tools

pass char* over dll-boundaries

 
 
John Harrison
Guest
Posts: n/a
 
      09-03-2004

"Ekim" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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,
news:comp.os.ms-windows.programmer.win32 for instance.

John


 
Reply With Quote
 
 
 
 
John Sun
Guest
Posts: n/a
 
      09-03-2004
> 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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "John Harrison" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> > "Ekim" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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,
> > news:comp.os.ms-windows.programmer.win32 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
>
>



 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      09-03-2004

"Ekim" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "John Harrison" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> > "Ekim" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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,
> > news:comp.os.ms-windows.programmer.win32 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



 
Reply With Quote
 
Peter Koch Larsen
Guest
Posts: n/a
 
      09-03-2004

"John Sun" <(E-Mail Removed)> skrev i en meddelelse
news:41386c2a$0$91004$(E-Mail Removed). ..
> > 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


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      09-03-2004

" >
>
> 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


 
Reply With Quote
 
Ekim
Guest
Posts: n/a
 
      09-03-2004
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!


 
Reply With Quote
 
Ekim
Guest
Posts: n/a
 
      09-03-2004
> (So what happened to your original problem of passing the string from a
> DLL?)


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!


 
Reply With Quote
 
puppet_sock@hotmail.com
Guest
Posts: n/a
 
      09-03-2004
"Ekim" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
[snip]
> 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
 
Reply With Quote
 
Ekim
Guest
Posts: n/a
 
      09-03-2004
"John Harrison" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Ekim" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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,
> news:comp.os.ms-windows.programmer.win32 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
difference between pass by address and pass by reference!! blufox C Programming 2 04-03-2006 02:53 PM
Pass by reference / pass by value Jerry Java 20 09-09-2005 06:08 PM
pass by ref vs pass by pointer Radde C++ 38 08-23-2005 06:29 PM
Pass-by-reference instead of pass-by-pointer = a bad idea? Mr A C++ 111 07-14-2005 03:04 AM



Advertisments