Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   pass char* over dll-boundaries (http://www.velocityreviews.com/forums/t285403-pass-char-over-dll-boundaries.html)

John Harrison 09-03-2004 11:41 AM

[OT] Re: pass char* over dll-boundaries
 

"Ekim" <the.newsletter@gmx.net> wrote in message
news:2pr02eFo4hqgU1@uni-berlin.de...
> 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



John Sun 09-03-2004 01:07 PM

Re: [OT] Re: pass char* over dll-boundaries
 
> 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" <the.newsletter@gmx.net> wrote in message
news:2pr63fFoniarU1@uni-berlin.de...
> "John Harrison" <john_andronicus@hotmail.com> wrote in message
> news:2pr3idFnlhnuU1@uni-berlin.de...
> >
> > "Ekim" <the.newsletter@gmx.net> wrote in message
> > news:2pr02eFo4hqgU1@uni-berlin.de...
> > > 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
>
>




Howard 09-03-2004 03:48 PM

Re: [OT] Re: pass char* over dll-boundaries
 

"Ekim" <the.newsletter@gmx.net> wrote in message
news:2pr63fFoniarU1@uni-berlin.de...
> "John Harrison" <john_andronicus@hotmail.com> wrote in message
> news:2pr3idFnlhnuU1@uni-berlin.de...
> >
> > "Ekim" <the.newsletter@gmx.net> wrote in message
> > news:2pr02eFo4hqgU1@uni-berlin.de...
> > > 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




Peter Koch Larsen 09-03-2004 06:32 PM

Re: [OT] Re: pass char* over dll-boundaries
 

"John Sun" <JSunnewsgroup@gmail.com> skrev i en meddelelse
news:41386c2a$0$91004$39cecf19@news.twtelecom.net. ..
> > 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



Howard 09-03-2004 06:43 PM

Re: [OT] Re: pass char* over dll-boundaries
 

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



Ekim 09-03-2004 07:42 PM

pass char* over dll-boundaries
 
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!



Ekim 09-03-2004 07:44 PM

Re: [OT] Re: pass char* over dll-boundaries
 
> (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!



puppet_sock@hotmail.com 09-03-2004 07:57 PM

Re: pass char* over dll-boundaries
 
"Ekim" <the.newsletter@gmx.net> wrote in message news:<2pr02eFo4hqgU1@uni-berlin.de>...
[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

Ekim 09-03-2004 09:25 PM

Re: [OT] Re: pass char* over dll-boundaries
 
"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:2pr3idFnlhnuU1@uni-berlin.de...
>
> "Ekim" <the.newsletter@gmx.net> wrote in message
> news:2pr02eFo4hqgU1@uni-berlin.de...
> > 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




All times are GMT. The time now is 03:53 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.