Solved

return a string from a dll function

Posted on 2002-04-04
6
218 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much.  It worked great.  I was looking all day for information on how to do this.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now