Solved

return a string from a dll function

Posted on 2002-04-04
6
219 Views
Last Modified: 2010-04-02
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.
0
Comment
Question by:sdbanks
  • 4
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6918891
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);
}

0
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 6918898
To elaborate - the basic idea is to pass a buffer to a location where you want to receive the string, along with the size of that buffer. The funcion in the DLL checks whether the buffer is of sufficiant size and copys the date string into that buffer...
0
 
LVL 1

Author Comment

by:sdbanks
ID: 6919047
Thank you very much.  It worked great.  I was looking all day for information on how to do this.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 86

Expert Comment

by:jkr
ID: 6919063
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.
}
0
 
LVL 1

Author Comment

by:sdbanks
ID: 6919738
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?

0
 
LVL 86

Expert Comment

by:jkr
ID: 6919764
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...
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question