CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: dll in dev-cpp

  1. #1
    Join Date
    Jul 2009
    Posts
    3

    dll in dev-cpp

    Hello,

    I'm trying to make a dll in dev-cpp. The software loading the dll is a legacy program and has no knowledge of the function that it needs to call. Therefore the dll will need to call the function when loaded. I've been working in iterations to work my way up to that goal and have made a basic dll in dev-cpp with a tester program.

    Anyways so I compile the dll and the tester program successfully and try to call dlltest.exe
    The output says it loaded the dll successfully but no messagebox pops up.
    What am I doing wrong?

    Thank you,
    ~Thrawn89

    Forgive me if this code doesn't get parsed into tags but I do not know the command to do so:

    --------------------------------------------------------------
    //dlltest.cpp
    #include <windows.h>
    #include <cstdlib>
    #include <cstdio>
    #include "dll.h"

    //This will test out loading a dll.
    void handler(int errCode);

    int main(int argc, char** argv)
    {
    HMODULE lib = LoadLibrary( argv[1] );

    if(!lib)
    {
    printf("LoadLibrary Failed Loading %s.\n", argv[1]);

    handler(GetLastError());
    }

    printf("LoadLibrary loaded %s. Handle is %d\n", argv[1], lib);

    while(1);

    return 0;
    }


    /* Takes a system exit code, prints it, then exits */
    void handler(int errCode)
    {

    CHAR buff[1024];

    FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, 0, errCode, 0, buff, 1024, 0);

    printf("Error(%d): %s\n", errCode, buff);

    exit(errCode);
    }

    -----------------------------------------------
    //dllmain.cpp

    /* Replace "dll.h" with the name of your header */
    #include <windows.h>
    #include <cstdio>
    #include <cstdlib>
    #include "dll.h"

    DWORD WINAPI HelloWorld (LPVOID)
    {

    MessageBox(NULL, "Hello World!\n", "Hello", MB_OK);


    return 1;
    }

    BOOL APIENTRY Dllmain (HINSTANCE hInst /* Library instance handle. */ ,
    DWORD reason /* Reason this function is being called. */ ,
    LPVOID reserved /* Not used. */ )
    {
    switch (reason)
    {
    case DLL_PROCESS_ATTACH:
    CreateThread(NULL, 0, HelloWorld, NULL, 0, NULL);
    break;

    case DLL_PROCESS_DETACH:
    break;

    case DLL_THREAD_ATTACH:
    break;

    case DLL_THREAD_DETACH:
    break;
    }

    return TRUE;
    }

    ---------------------------------------
    //dll.h
    #ifndef _DLL_H_
    #define _DLL_H_

    #if BUILDING_DLL
    # define DLLIMPORT __declspec (dllexport)
    #else /* Not BUILDING_DLL */
    # define DLLIMPORT __declspec (dllimport)
    #endif /* Not BUILDING_DLL */

    extern "C"{
    DWORD WINAPI HelloWorld (LPVOID);
    }

    #endif /* _DLL_H_ */

  2. #2
    Join Date
    Jul 2009
    Posts
    3

    Re: dll in dev-cpp

    The problem was that DLLMain was not being called. Putting extern "C" in front of the BOOL fixed it.

    ~Thrawn89

  3. #3
    Join Date
    Nov 2004
    Location
    Pakistan
    Posts
    466

    Re: dll in dev-cpp

    For other readers this link might help:
    http://www.apitalk.com/document.php?id=1184209001_1

    regards
    » Please 'Rate This Post' if it helped (encourage us to help you more)
    » Build GUI in minute using rad c++
    » Free IDE + GUI code generator - screenshot
    » Free WINAPI sourcecode and tutorials

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: dll in dev-cpp

    BOOL APIENTRY Dllmain (HINSTANCE hInst /* Library instance handle. */ ,
    The problem was that DLLMain was not being called. Putting extern "C" in front of the BOOL fixed it.
    FYI, DllMain is already declared extern "C" in SDK headers. And the name must be exactly DllMain but not Dllmain or DLLMain
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured