pass char* over dll-boundaries

From: Ekim (the.newsletter_at_gmx.net)
Date: 09/03/04


Date: Fri, 3 Sep 2004 12:42:15 -0700

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!



Relevant Pages

  • [NT] Sybase xp_freedll Buffer Overflow
    ... Beyond Security would like to welcome Tiscali World Online ... Xp_free accepts a single parameter that is the name of the DLL to free. ... memory buffer. ... This memory copy results in the stack and the stack pointer ...
    (Securiteam)
  • Re: My dll problem
    ... caller has to provide the memory a function in a DLL is supposed to ... function in the last parameter that you passed in a buffer of 180 byte. ... then wire the output to a Byte Array To String ...
    (comp.lang.labview)
  • Re: C++ to Delphi conversion
    ... The DLL and the calling code don't use the same memory manager, so no, it ... it is never very wise to return a newly created buffer ...
    (borland.public.delphi.language.objectpascal)
  • Re: Discovering variable types...
    ... >- but I suppose MS expect us to use wrappers ... memory allocations for your variables from disk as well. ... >They most certainly are of fixed size, changing the size of a String ... >>me to keep buffer size and current postion right in the memory block. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Discovering variable types...
    ... >memory it points to is on the heap. ... sequentially reading data, if one is randomly reading records, then a ... >project is what's prompting me to improve disk access. ... from a memory buffer I can do it in about a second. ...
    (comp.lang.pascal.delphi.misc)