pass char* over dll-boundaries

Discussion in 'C++' started by John Harrison, Sep 3, 2004.

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

    "Ekim" <> wrote in message
    news:...
    > 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 Harrison, Sep 3, 2004
    #1
    1. Advertising

  2. John Harrison

    John Sun Guest

    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" <> wrote in message
    news:...
    > "John Harrison" <> wrote in message
    > news:...
    > >
    > > "Ekim" <> wrote in message
    > > news:...
    > > > 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
    >
    >
    John Sun, Sep 3, 2004
    #2
    1. Advertising

  3. John Harrison

    Howard Guest

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

    "Ekim" <> wrote in message
    news:...
    > "John Harrison" <> wrote in message
    > news:...
    > >
    > > "Ekim" <> wrote in message
    > > news:...
    > > > 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
    Howard, Sep 3, 2004
    #3
  4. Re: [OT] Re: pass char* over dll-boundaries

    "John Sun" <> skrev i en meddelelse
    news:41386c2a$0$91004$...
    > > 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
    Peter Koch Larsen, Sep 3, 2004
    #4
  5. John Harrison

    Howard Guest

    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
    Howard, Sep 3, 2004
    #5
  6. John Harrison

    Ekim Guest

    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, Sep 3, 2004
    #6
  7. John Harrison

    Ekim Guest

    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!
    Ekim, Sep 3, 2004
    #7
  8. John Harrison

    Guest

    "Ekim" <> wrote in message news:<>...
    [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
    , Sep 3, 2004
    #8
  9. John Harrison

    Ekim Guest

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

    "John Harrison" <> wrote in message
    news:...
    >
    > "Ekim" <> wrote in message
    > news:...
    > > 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
    Ekim, Sep 3, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    549
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,043
    Ian Collins
    May 9, 2006
  3. ben mitch
    Replies:
    21
    Views:
    882
    Chris Dollin
    Jun 16, 2008
  4. ben mitch
    Replies:
    22
    Views:
    837
    Chris Dollin
    Jun 16, 2008
  5. Ò»Ê×Ê«

    Passing string over DLL boundaries.

    Ò»Ê×Ê«, Aug 24, 2008, in forum: C++
    Replies:
    4
    Views:
    413
    Juha Nieminen
    Aug 24, 2008
Loading...

Share This Page