Need some help with Dlls

This is a discussion on Need some help with Dlls within the Windows Programming forums, part of the Platform Specific Boards category; I have just started using dlls and am trying to make a dynamic benchmarking application using dlls as benchmarks (one ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    19

    Question Need some help with Dlls

    I have just started using dlls and am trying to make a dynamic benchmarking application using dlls as benchmarks (one test per dll, all info inside dll).
    However my program always fails to load dlls number 2 or more. Is there some limitations i'm not seeing?

    I have narrowed it down to the following piece of code.
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main(){
    	HINSTANCE dll1;
    	HINSTANCE dll2;
    	
    	dll1 = LoadLibrary("a.dll");
    	dll2 = LoadLibrary("b.dll");
    	
    	if(!dll1)printf("Error: Dll1 could not be loaded!\r\n");
    	if(!dll2)printf("Error: Dll2 could not be loaded!\r\n");
    }
    This will produce the output: Error: Dll2 could not be loaded!\r\n.

    a.dll and b.dll are copies of the same file.

    Both contain only the implimentations for the following functions:
    Code:
    void __declspec (dllexport) getTestFunction(char* placement, int size);
    long int __declspec (dllexport) realTestTwo()

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    a.dll and b.dll are copies of the same file.
    Think carefully now.... what's wrong with this picture?

    Second, you need to create headers for those dlls and #include them at the top of your page and you will need to link against the libraries the compiler creats when building your DLLs.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    19
    Well, the way i do it works when its only one dll.

    I just made a test dll and made 2 copies of it to test this, is it not 'allowed'.

    Edit: Also, here I am not trying to run any of the functions, if I try to run them (using GetProcAdress and assigning to variable of function definition) it works fine.

    My main question here is why am I unable to load the dlls.
    Last edited by dumazz; 05-06-2011 at 10:50 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    BECAUSE... windows does not load duplicate DLL files, even across multiple copies of a program...

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    19
    Ok, thanks for your reply, I did not know that

    However, I created another dll with the functions
    Code:
    void __declspec (dllexport) getTestFunction2(char* placement, int size);
    long int __declspec (dllexport) realTestTwo1243214();  //Yes, I'm too lazy to make up a more creative name :p
    And it did not work with this either.

    Edit: That is now the second dll I am loading, the first one is the one in original topic.
    Edit2:
    even across multiple copies of a program
    What do you mean by this, I originally copied the dll, not the program.
    Last edited by dumazz; 05-06-2011 at 10:56 AM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'm also pretty sure it won't load an empty DLL... but you'll need to check the docs for that.

    Are your DllMain() functions declared correctly?

    It would definately help if you could post the code... (in code tags please)....

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    19
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    extern "C"{
        void __declspec (dllexport) getTestFunction2(char* placement, int size){
            snprintf(placement, size, "realTestTwo1243214");
            //snprintf(placement, size, "7zFunctionName");
            return;
        }
        
        
        long int __declspec (dllexport) realTestTwo1243214(){
             printf("Currently in the realTest function in the DLL! \x02\r\n");
             return 0;
        }
    }
    
    bool APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved)
    {
        switch (reason)
        {
          case DLL_PROCESS_ATTACH:
            break;
    
          case DLL_PROCESS_DETACH:
            break;
    
          case DLL_THREAD_ATTACH:
            break;
    
          case DLL_THREAD_DETACH:
            break;
        }
        return TRUE;
    }
    That is the dll's code. The first dll is the same code, but with different function names


    Edit: GetLastError returns 998 - Invalid access to memory location on 2nd dll.
    Last edited by dumazz; 05-06-2011 at 11:03 AM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    At this point I'm not sure what your problem is... Maybe the others can help...

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by CommonTater View Post
    BECAUSE... windows does not load duplicate DLL files, even across multiple copies of a program...
    Uh, what?

    Windows has utterly no clue that the two DLLs are the "same." They aren't the same. They are separate files.

    If you don't believe it, compile this:

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main()
    {
    	HMODULE a = LoadLibrary("foo.dll");
    	HMODULE b = LoadLibrary("bar.dll");
    
    	printf("%p\n", a);
    	printf("%p\n", b);
    }
    Then put two copies of the same DLL alongside the resulting EXE, naming them "foo.dll" and "bar.dll." No problem.

    EDIT: Furthermore, yes, Windows will load DLLs multiple times between separate copies of the same program. If one copy of the program has allocated VM space at the DLLs preferred base address, the DLL will need to be rebased. In the other copy of the program the DLL was not rebased (or was rebased to yet a different location). It will share them if it CAN. If it can't, it's no disaster, it just loads it again.
    Last edited by brewbuck; 05-06-2011 at 01:30 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    OK... live and learn (I seldom use Dlls.) but that doesn't explain the OP's problem...

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    19
    Well, I must be doing _something_ wrong since I cannot use 2 dlls in my program.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dumazz View Post
    Well, I must be doing _something_ wrong since I cannot use 2 dlls in my program.
    Well... if I'm right, then we have something of a mystery why it won't load two different files.
    If I'm wrong... then we have a real mystery about why it won't load two files at all.

    The thing stuck in my craw is that I have a couple of programs that use 5 and 6 Dlls with no problems at all...
    Build the Dll... make a header to include in my program... link against the Dll's export library... off and running.

    I wish I could be more help here... but now I'm asking the question too...

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    19
    Well, found a resource that might be of some help: PRB&#58; LoadLibrary API Fails with 998 &#40;ERROR_NOACCESS&#41; Error
    This seems to be the error that is happening.

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by dumazz View Post
    Well, found a resource that might be of some help: PRB: LoadLibrary API Fails with 998 (ERROR_NOACCESS) Error
    This seems to be the error that is happening.
    You mean that's the error code you get when you call GetLastError()?

    Can you post the contents of DllMain() in the DLL in question?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    19
    Scroll up, dll code is in post #7

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DLLs in C++
    By RoshanX in forum C++ Programming
    Replies: 0
    Last Post: 02-22-2006, 11:48 AM
  2. DLLs
    By cgod in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2004, 06:21 AM
  3. .dlls
    By alpha2018 in forum C++ Programming
    Replies: 2
    Last Post: 08-25-2004, 12:27 PM
  4. Use of DLLs
    By Supar in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2003, 03:25 PM
  5. Using C DLLs in VB
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 10-10-2001, 02:58 PM

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