Solved

return a string from a dll function

Posted on 2002-04-04
6
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Embarcadero C++ Builder XE2 TDateTime 8 92
IdTCPClient1->Disconnect(); not working 3 93
Create a path if not exists 7 128
Which Linux flavors will this run on? 6 116
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…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

710 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