Dll Loading Twice?

This is a discussion on Dll Loading Twice? within the Windows Programming forums, part of the Platform Specific Boards category; Hi guys I'm experimenting with DLL's and loading them. I've got a simple program that loads a DLL, and I've ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Dll Loading Twice?

    Hi guys I'm experimenting with DLL's and loading them. I've got a simple program that loads a DLL, and I've created a DLL that calls a function that displays a MessageBox.

    The problem is when I launch my loader program, the MessageBox is displayed twice... I'm only calling LoadLibrary() once, so how can it be displayed twice?!

    Any help would be greatly appreciated.

    Here's the loader:

    Code:
    #include <iostream>
    #include <Windows.h>
    
    int main() {
    	HMODULE hDll = LoadLibrary("messageboxdll.dll");
    	
    	if (!hDll) {
    		printf("Could not load DLL.\n");
    		return 1;
    	}
    
    	CloseHandle(hDll);
    }
    Here's the DLL:

    Code:
    #include <windows.h>
    
    __declspec (dllexport) void Alert(void);
    
    void Alert(void) {
    	MessageBox(NULL, "Successfully loaded!", "Loaded", MB_OK);
    }
    
    BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
    	Alert();
    	return TRUE;
    }
    I'm using Visual Studio 08.
    Last edited by pobri19; 11-25-2009 at 11:08 AM.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    DllMain is called once when your application loads it, and once when it unloads it. Your DllMain should look more like this instead:

    Code:
    BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
    	if(fdwReason == DLL_PROCESS_ATTACH)
    		Alert();
    	return TRUE;
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Quote Originally Posted by bithub View Post
    DllMain is called once when your application loads it, and once when it unloads it. Your DllMain should look more like this instead:

    Code:
    BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
    	if(fdwReason == DLL_PROCESS_ATTACH)
    		Alert();
    	return TRUE;
    }
    Wow, no wonder it was displaying it twice :P Thanks a lot!
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading a DLL that is not in the same directory as the executable
    By starcatcher in forum Windows Programming
    Replies: 10
    Last Post: 12-13-2008, 07:05 AM
  2. Loading Struct from dll
    By peyman_k in forum C++ Programming
    Replies: 2
    Last Post: 08-15-2008, 04:12 PM
  3. Explicit DLL loading
    By true_organs in forum Windows Programming
    Replies: 6
    Last Post: 05-24-2008, 02:26 PM
  4. Loading a DLL at runtime
    By filler_bunny in forum Windows Programming
    Replies: 9
    Last Post: 02-23-2003, 08:03 AM

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