hey everyone,

i am attempting to create a simple dll in c++ that creates a timestamp, in the following format...

17042011233200

Which is: 17/04/2011 @ 23:32:00

thus far, i have found a code (see below), but after compiling with devc++ it doesnt seem to work, i am new with dll's and this would be my very first one but because of my lack of experience i am unable to create one, please could someone help me.

MY HORRIBLE ATTEMPT

#include <stdio.h>
#include <time.h>
#include "timestamp.h"

BOOL APIENTRY DllMain (HINSTANCE hInst,DWORD reason,LPVOID reserved )
{
    return TRUE;
}

DLL_EXPORT int timestamp()
{
    time_t     now;
    struct tm  *ts;
    char       buf[80];
 
    now = time(0);
 
    ts = localtime(&now);
    strftime(buf, sizeof(buf), "%d%m%Y%H%M%S", ts);
}

the code above compiles correcttly as a dll, but doesnt work when called, if anyone could help me create the full source (including a header) i need for devc++ that would be amazing and i will be so grateful, thanks for you time,

Nathaniel Blackburn.

Recommended Answers

All 10 Replies

How about instead we help you solve what is wrong with this code.
Now, to do that.. we need to know what you mean with 'doesn't work when called'. This sentence alone is not very helpful:
Does it crash? Does it just it give output, but not what you expect? Does it give no output at all?

My guess is that latter. Why? Because there is no return statement in the timestamp() function. You should set the warning level of your compiler higher, I have never used devc++ though.

Sorry, what i mean is im not getting any output from the function

So, do you understand why? That there is no return statement, so how should it know what to return?

Thats what im stuck with im really sorry to be any hassle.

My strong point is lua. Im new to the c language. Would return be something like this...

return buf;

here is the source for the complete DLL so far, all i need to do is add a return which im unsure on how to do because it just causes errors when i attempt the method above, thanks...

Main Source...

#include "codestamp.h"

BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved)
{
    return TRUE;
}

DLL_EXPORT int codestamp(void)
{
    time_t now;
    struct tm *ts;
    char buf[80];
    
    now = time(0);
    ts = localtime(&now);
    
    strftime(buf, sizeof(buf), "%d%m%Y%H%M%S", ts);
}

Main Header...

#ifndef _DLL_H_
#define _DLL_H_

#include <windows.h>
#include <stdio.h>
#include <time.h>

#ifndef DLL_EXPORT
#define DLL_EXPORT extern "C" __declspec(dllexport)
#endif

DLL_EXPORT int codestamp(void);

#endif

Thanks,
Nathaniel Blackburn

Thanks for sharing your post. It helps me alot in completing my project work.

@OP:
The problem you're having is where you're trying to return a char array (buf) from your function; but the prototype/declaration of the function says that your function's supposed to be returning an int!

As you want to return a C-style string / char array, you should change the signature of the function to return a char* NOT an int!!

Although, one major problem I can see there is that you'll then be returning a pointer to an array of chars that is local to the dll function. So by the time the pointer has been passed back to the caller the char array will have dropped out of scope, leaving the pointer pointing to stray memory.
But to get around this, you could declare buf to be static!
e.g.

DLL_EXPORT char* codestamp(void)
{
    time_t now;
    struct tm *ts;
    static char buf[80];
    
    now = time(0);
    ts = localtime(&now);
    
    strftime(buf, sizeof(buf), "%d%m%Y%H%M%S", ts);
	return buf;
}

Alternatively, to avoid having static data in your dll, you could pass a pointer to a char array as a parameter to your dll function and change the return type to void. e.g:

DLL_EXPORT void codestamp(char *buf)
{
    time_t now;
    struct tm *ts;
    
    now = time(0);
    ts = localtime(&now);
    
    strftime(buf, sizeof(buf), "%d%m%Y%H%M%S", ts);
}

This would mean that callers of your dll function would have to set up a buffer to pass into the function. The dll function will then populate the passed-in buffer with the timestamp.

There are probably a few other alternatives, but I'm a bit fried ATM... Long day! heh heh!

Thanks so much JasonHippy, you have made my day. People like you rock because your helpful, i have learnt how to fix my problem instead of someone saying "fix it yourself" which defeats the point of asking for help and you broke it down for me to understand, thanks dude :)

No probs.
I try not to give away complete solutions where possible. But when I do give complete solutions I try to make sure that things are explained in enough detail for the OP (and others) to learn something from it. I also try to offer a few alternatives, so the OP can choose which approach to take.

Which IMO is better than a cryptic one line response, or simply spoon-feeding code with no explanation.

Anyway, glad to have helped!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.