Scary DLL Problem, DLLs crash?

This is a discussion on Scary DLL Problem, DLLs crash? within the Windows Programming forums, part of the Platform Specific Boards category; So I been trying to code a bunch of DLL stuff, I've done it before, but for some reason this ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    137

    [Resolved]DLL

    So I been trying to code a bunch of DLL stuff, I've done it before, but for some reason this time it's not working. It crashes.

    Now, bear in mind that to simplify the problem, I've created a simple application that will run one function. But even that crashes!

    How is this possible? Is it a vista thing? I tried both CodeBlocks and DevCpp.

    Main PROGRAM:
    Code:
    #include <iostream>
    #include <windows.h>
    
    typedef int (*DLLFUNCTION)(int);
    
    HINSTANCE g_hDLLinst;
    
    int main(){
        DLLFUNCTION Message = NULL;
        std::cout << "Running DLL" << std::endl;
        g_hDLLinst = LoadLibrary("test.dll");
        std::cout << "Library Loaded" << std::endl;
        if(g_hDLLinst != NULL){
          Message = (DLLFUNCTION)GetProcAddress(g_hDLLinst, "Message");
        }
        std::cout << "Message Loaded" << std::endl;
        int x = Message(5);
        if(x == 5){
          std::cout << "Message Worked" << std::endl;
        }
        FreeLibrary(g_hDLLinst);
        return 0;
    }
    DLL:
    MAIN.H:
    Code:
    #ifndef __MAIN_H__
    #define __MAIN_H__
    
    #include <windows.h>
    
    /*  To use this exported function of dll, include this header
     *  in your project.
     */
    
    #define DLL_EXPORT __declspec(dllexport)
    
    
    #ifdef __cplusplus
    extern "C"
    {
    #endif
    
    int DLL_EXPORT DLLMessage(int x);
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif // __MAIN_H__
    MAIN.CPP
    Code:
    #include "main.h"
    
    // a sample exported function
    int DLL_EXPORT DLLMessage(int x){
        MessageBox(0, "DLL Worked", "DLL Message", MB_OK | MB_ICONINFORMATION);
        return x;
    }
    
    BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved){
        switch (fdwReason)
        {
            case DLL_PROCESS_ATTACH:
                // attach to process
                // return FALSE to fail DLL load
                break;
    
            case DLL_PROCESS_DETACH:
                // detach from process
                break;
    
            case DLL_THREAD_ATTACH:
                // attach to thread
                break;
    
            case DLL_THREAD_DETACH:
                // detach from thread
                break;
        }
        return TRUE; // succesful
    }
    I put both the dll and the exe into the same folder, and it does:
    Running DLL
    Library Loaded
    Message Loaded
    Crash.......
    Last edited by execute; 11-16-2008 at 02:15 AM.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Is the return value from GetProcAddress NULL? (I'm assuming you imported DLLMessage as Message, but who knows.)

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Yes it's null, but I don't know why, I didn't do anything weird.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's because you misspelled "DLLMessage" as "Message".

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Have you ever bothered to crawl through the code one line at a time using a debugger?

    Did you think about checking the pointer for NULL before trying to call it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Oh thank you tabstop, I didn't notice that.

    "Message" should have been "DLLMessage"
    I guess it's been a long night. I was hoping it was something else though, because my other DLL program has everything right and that doesn't work either. I might have to post that as well :O.

    And Salem, even if I had known that it was NULL before, or with a debugger, I still wouldn't have seen that error.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Seen what error?

    Code:
    Message = (DLLFUNCTION)GetProcAddress(g_hDLLinst, "Message");
    if ( Message == NULL ) {
      // DANGER!!! - CLIFF AHEAD
    }
    int x = Message(5); // Keep on truckin'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Elaborating on Salems suggestion, you should always check return values.

    Code:
    int
    main( void )
    {
    	HMODULE
    		dll = LoadLibrary( "test.dll" );
    	if( dll != NULL )
    	{
    		DLLFUNCTION 
    			function = ( DLLFUNCTION )GetProcAddress( dll, "Message" );
    		if( function != NULL )
    		{
    			int
    				x = function( 5 );
    			if( x == 5 )
    			{
    				cout << "Success." << endl;
    			}
    			else
    			{
    				cerr << "Unexpected result!" << endl;
    			}
    		}
    		else
    		{
    			cerr << "GetProcAddress failed!" << endl;
    		}
    		FreeLibrary( dll );	
    	}
    	else
    	{
    		cerr << "LoadLibrary failed!" << endl;
    	}
    }



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dll problem
    By fiska in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2006, 06:14 AM
  2. DLL problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2002, 03:00 AM
  3. VCL and DLL class problem
    By borland_man in forum C++ Programming
    Replies: 1
    Last Post: 02-13-2002, 10:07 AM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21