Solved

return a string from a dll function

Posted on 2002-04-04
6
221 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…

685 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