Link to home
Start Free TrialLog in
Avatar of sdbanks
sdbanks

asked on

return a string from a dll function

I have a visual c++ (version 6) DLL that takes a parameter username, determines the date and time,  stores this datetime value in a database.

I must now return this datetime(String) back to the calling program and I am not sure how to do it.

The dll function is defined as:

__declspec(dllexport) int _cdecl StoreToken(char *strContextName) {
   time_t        ltime;
   struct        tm *gmTime;
   char         *currentUTC;

   time( &ltime );
   gmTime = gmtime( &ltime );                  
   currentUTC = strdup(asctime(gmTime));
   //Code to store currentUTC in database has been omited
   //currentUTC is what I would like to return to the
   //calling program
...
return bstored;  // I am using this to determine is the
                 // function worked.
}

The test program I am using is as follows:

// TestDll.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

extern int StoreToken(char *contextName);

int main(int argc, char* argv[])
{
    char thedate[] = "";
    char contextname[] = ".cn=sdbanks.ou=itd.ou=chho.o=apm";
     StoreToken(contextname);
     return (0);
}


What changes must I do to return currentUTC to the calling program.
Avatar of jkr
jkr
Flag of Germany image

Make it read

__declspec(dllexport) int _cdecl StoreToken(char *strContextName, char* pszUTCBuf, long lnBufSize) {
  time_t        ltime;
  struct        tm *gmTime;
  char         *currentUTC;

  time( &ltime );
  gmTime = gmtime( &ltime );                  
  currentUTC = strdup(asctime(gmTime));

  if ( strlen ( currentUTC) < lnBufSize) {

     strcpy ( pszUTCBuf, currentUTC);

  } else {

   // buffer not big enough, error!
  }

return bstored;  // I am using this to determine is the
                // function worked.
}

#define MAX_THEDATE 256
int main(int argc, char* argv[])
{
   char thedate[ MAX_THEDATE] = "";
   char contextname[] = ".cn=sdbanks.ou=itd.ou=chho.o=apm";
    StoreToken(contextname, thedate, MAX_THEDATE);
    return (0);
}

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sdbanks
sdbanks

ASKER

Thank you very much.  It worked great.  I was looking all day for information on how to do this.
You're welcome - that's the way that e.g. your app receives textual information from Windows (see e.g. 'GetWindowsDirectory()'). Just one correction that I'd recommend:

__declspec(dllexport) int _cdecl StoreToken(char *strContextName, char* pszUTCBuf, long lnBufSize) {
 time_t        ltime;
 struct        tm *gmTime;
 char         *currentUTC;

 time( &ltime );
 gmTime = gmtime( &ltime );                  
 currentUTC = strdup(asctime(gmTime));

 if ( strlen ( currentUTC) < lnBufSize) {

    strcpy ( pszUTCBuf, currentUTC);

 } else {

  // buffer not big enough, error!
 }

 free ( currentUTC); // <----!!! will cause memory leaks otherwise!

return bstored;  // I am using this to determine is the
               // function worked.
}
Avatar of sdbanks

ASKER

I will do that right away.  Is that something I should do for every variable when I exit the routine or just for char *currentUTC?

Well, in this case, just that variable. It is because "strdup()" uses a "malloc()" to return the duplicated string, and the docs also say that you have to "free()" it manually...