Looking for some help, problems with C++ Dlls (Using Visual Studio C++ 6.0)

Discussion in 'C++' started by Dave, Jan 24, 2007.

  1. Dave

    Dave Guest

    Hi everyone. Okay, I'm trying to dynamically link some dlls and I'm
    having some problems. I originally had a project that seems to work,
    but for the life of me I can't repeat the results.

    I used app wizard to create a simple Win32 Dynamic Linked library. the
    code is as follows:


    #include "stdafx.h"
    #include "stdio.h"

    int i = 0;

    BOOL APIENTRY DllMain( HANDLE hModule,
    DWORD ul_reason_for_call,
    LPVOID lpReserved
    )
    {
    return TRUE;
    }

    void _stdcall InitDll(){
    i = 1;
    }



    I build that and get 'TestDll.dll'

    The program I use to open this dll looks like this:


    #include "stdafx.h"
    #include <windows.h>


    typedef void(__stdcall *InitDllOperation)();


    int main(int argc, char* argv[])
    {

    InitDllOperation initDll;

    HINSTANCE dllLib = LoadLibrary("TestDll.dll");

    if( dllLib == NULL )
    {
    printf( "Unable to load library\n");
    return 1;
    }
    else
    {
    printf( "loaded library successfully\n");
    }

    initDll =
    (InitDllOperation)GetProcAddress((HINSTANCE)dllLib,"InitDll");

    if( initDll == NULL )
    {
    printf( "initDll failed to load\n");
    return 1;
    }
    else
    {
    printf( "init Success\n");
    }

    return 0;
    }


    Every time the library loads successfully, but the 'initDll' object is
    ALWAYS Null. The return from GetProcAddress seems to always come up
    null. Any suggestions?

    Thanks
     
    Dave, Jan 24, 2007
    #1
    1. Advertisements

  2. * Dave:
    Well, that belongs in a Windows programming newsgroup, sorry.
     
    Alf P. Steinbach, Jan 24, 2007
    #2
    1. Advertisements

  3. [ snip ]
    Hello Dave,

    Your question is off topic here, but you can check two things:

    1. Exporting functions from DLLs requires using __declspec(export)
    2. Name mangling needs to be disabled on exported functions. Mangled
    names may not be what you expect, so GetProcAddress may not find the
    function you want.

    Search for some sample code on Google. There are plenty of good
    examples available.

    But again, this question is off topic here. :)

    Regards,
    Markus.
     
    Markus Svilans, Jan 24, 2007
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.