This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: DLL function with string result?


Personally, I'd instead write it as follows:

char*
mallocAndStrCpy (const char* srcStr) {
   if (srcStr == NULL)
      return NULL;
   return strdup(srcStr);
}

The caller can use free() on the pointer you return, since the
DLL runs inside the same process on Win32 and thus shares the
same global heap for malloc operations.  Note that you won't be able
to modify the "original" pointer passed to you by the caller, so
the caller will need to make sure they also free(srcStr) at
some point as well or you'll have a memory leak.

This is probably a question best asked on one of the comp.* newsgroups
related to general C/C++ programming or Win32 programming in general,
since it's not specific to DLL's or cygwin specifically.

Troy

-----Original Message-----
From: Christian Lescher [mailto:christian@lescher.de]
Sent: Thursday, April 19, 2001 1:57 PM
To: cygwin@cygwin.com
Cc: christian.lescher@icn.siemens.de
Subject: DLL function with string result?


For writing a DLL function that returns a string result, I need a C
function that takes a pointer to a given string, creates a copy of it
and returns a pointer to the copied string. (The string may be > 64 KB
and should be freed again by the calling program.)

This is probably a rather simple task, but I didn't succeed yet. I tried
something like:

#include <stdlib.h>
#include <string.h>
char*
mallocAndStrCpy (const char* srcStr) {
  return strcpy((char*) malloc(strlen (srcStr)+1),srcStr);
}

However, for the string result I probably have to use something else
than "malloc"?!

Secondly, how should the caller free the assigned memory again?

I'm sorry for this basic question, but I'm still a beginner in
programming with cygwin/gcc under WinNT.

Regards, Christian


--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple

--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]