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

From: Howard (alicebt_at_hotmail.com)
Date: 09/03/04


Date: Fri, 03 Sep 2004 15:48:09 GMT


"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



Relevant Pages

  • Passing a string from a C DLL to VB
    ... I want to create a function call in a C DLL that will pass a string to a VB ... I've tried passing a char* to the VB application however this fails. ...
    (microsoft.public.vc.language)
  • Re: How to pass object from dll to exe? error <98>
    ... But you didn't say you were passing an object, ... std exe maininjects dll into AutoCad process space (for speed reasons ... When an EXE launches, the Windows loader, loads the program into its ... Set MyDll = oAcadApp.GetInterfaceObject ...
    (microsoft.public.vb.general.discussion)
  • Re: Critical section shared betwen dll and main program
    ... I am discussing the case of dynamic DLL ... loading and passing PCRITICAL_SECTION between old version of DLL ... and an executable which was build in newer environment. ... without passing a pointer to the kernel32.dll? ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Argument passing with |boofar| (fwd)
    ... nothing to iterate here) passing in the file object ... Ruby book: ... >.each {|char| print char} ... > value each time for each list entry, which is passed as the variable ...
    (comp.lang.ruby)
  • Re: Converting UInt8 * to char *
    ... >> In the carbon files.h getting paths from dialogs requires passing the ... >> requires passing a char *. ... and pascal strings are usually UInt8 ...
    (comp.sys.mac.programmer.help)