• Advertisement

Archived

This topic is now archived and is closed to further replies.

A basic DLL question.

This topic is 5744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've just begun to learn DLL-ing with MSVC++6. I'm trying to use run-time linking using LoadLibrary and GetProcAddress. DLLA.h contains __declspec(dllexport) void printa(); DLLA.cpp contains
        
#include "stdafx.h"
#include "DLLA.h"
#include <stdio.h>

BOOL APIENTRY DllMain(HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved) {
    printf("\ndlla(");
    switch (ul_reason_for_call)	{
        case DLL_PROCESS_ATTACH:
            printf("ProcAttach"); break;	
        case DLL_THREAD_ATTACH:
            printf("ThreadAttach"); break;	
        case DLL_THREAD_DETACH:
            printf("ThreadDetach"); break;	
        case DLL_PROCESS_DETACH:
            printf("ProcDetach"); break;	
    }
    printf(")");
    return TRUE;
}

__declspec(dllexport) void printa() {
	printf("\na");
}
  
DLLTester contains
  
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

typedef void (*vfp)();

int main(int argc, char **argv) {
    freopen("c:\\lang\\media\\stdout.txt", "w", stdout);

    HINSTANCE dlla = NULL;
    dlla = LoadLibrary("c:\\lang\\kryll\\dll\\dlla.dll");

    if(!dlla) {
        printf("\nFailed to load dll");
        return 1;
    } else printf("\ndlla.dll = %p", dlla);

    vfp fp;
    fp = (vfp)GetProcAddress(dlla, "printa");
	
    if(!fp) {
        printf("\nFailed to load function");
        return 1;
    } else printf("\nprinta = %p", fp);

    fp();

    return 1;
}
  
DLLA compiles fine, and produces a good dll as far as I can tell. DLLTester also compiles ok. When I run dlltester, LoadLibrary runs fine, but GetProcAddress returns NULL. - Am I exporting the function correctly? OUTPUT: dlla.dll = 10000000 Failed to load function // This seems to be where main returns // dlla(ProcAttach); dlla(ProcDetach); It seems to be that ProcAttach only happens when my program exits. Is this the correct way to runtime-load a dll? (I tried the web for basic dll tutorials, but all I could find were tutorials on how to write dll's) [edited by - Krylloan on May 30, 2002 5:40:14 AM]

Share this post


Link to post
Share on other sites
Advertisement
there''s a tool that comes with msvc 6, the ''dependency viewer''.
try to open your dll with this tool. it can show, which functions
are actually exported by your dll.

another way than __declspec (dllexport) is to include a .def file
in your project. if you want to export printa, this file must
contain the following two lines:

EXPORTS
printa

then printa is exported.

if you only want to export functions, you dont necessarily need
a DllMain function.

Share this post


Link to post
Share on other sites
The problem might be that printa() gets name mangled and thus exported under another name than "printa". The dependency viewer will work fine to verify that names are exported as expected; another way is to instruct the linker to produce a map file.

Share this post


Link to post
Share on other sites
Thanks

The string generated was "?printa@@YAXXZ"
I think I''ll use ordinals from now on.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
try adding this to your .h file for your dll:

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

__declspec(dllexport) void printa();

#ifdef __cplusplus
}
#endif // __cplusplus

Share this post


Link to post
Share on other sites

  • Advertisement