delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2001/04/19/17:02:29

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT sources DOT redhat DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sources DOT redhat DOT com>
List-Help: <mailto:cygwin-help AT sources DOT redhat DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT sources DOT redhat DOT com
Delivered-To: mailing list cygwin AT sources DOT redhat DOT com
Message-ID: <8F23E55D511AD5119A6800D0B76FDDE11E0F41@cpex3.channelpoint.com>
From: Troy Noble <troy DOT noble AT channelpoint DOT com>
To: "'Christian Lescher'" <christian AT lescher DOT de>, cygwin AT cygwin DOT com
Cc: christian DOT lescher AT icn DOT siemens DOT de
Subject: RE: DLL function with string result?
Date: Thu, 19 Apr 2001 14:31:35 -0600
MIME-Version: 1.0
X-Mailer: Internet Mail Service (5.5.2653.19)
X-Scanned-By: MIMEDefang 1.0 (http://www.roaringpenguin.com/mimedefang/)

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 AT lescher DOT de]
Sent: Thursday, April 19, 2001 1:57 PM
To: cygwin AT cygwin DOT com
Cc: christian DOT lescher AT icn DOT siemens DOT 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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019