Results 1 to 11 of 11

Thread: DLLs using LoadLibrary()

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    DLLs using LoadLibrary()

    I am trying to write a DLL and call a function in it using LoadLibrary and GetProcAddress(). The code compiles fine, and I can enter two numbers, but then it crashes. This is my code:
    Code:
    //Main program
    #include <iostream>
    using namespace std;
    #include <windows.h>
    
    typedef void (__stdcall* MYFUNCTION)(int, int, int &);
    
    int main()
    {
    	HINSTANCE hDLL;
    	MYFUNCTION funcAdd;
    	int x, y, sum;
    
    	hDLL = LoadLibrary("dll.dll"); 
    
    	if (hDLL)
    	{
    		funcAdd = (MYFUNCTION)GetProcAddress(hDLL, "funcAdd");
    
    		cout << "Enter 2 ints:" << endl;
    		cin >> x >> y;
    		(funcAdd)(x, y, sum);// call the function
    		cout << "The sum is " << sum << endl;
    
    		FreeLibrary(hDLL);
    	}
    	cin.get();
    	return 0;
    }
    Code:
    //DLL code
    void __stdcall __declspec(dllexport) funcAdd(int a, int b, int &c)
    {
        c = a + b;
    }
    Any ideas?
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    try using

    Code:
    extern C _declspec(dllimport) funcAdd(int a, int b, int &c);
    & import a lib file into your project ( of your dll)
    VS.NET 2003

    Need to email me?

  3. #3

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    I don't understand. Why do I need a lib file? Where do I get this lib file?
    Alcohol & calculus don't mix.
    Never drink & derive.

  4. #4
    Lively Member ExciteMouse's Avatar
    Join Date
    Jul 2000
    Location
    Dallas, TX
    Posts
    78
    when you compile a DLL in C++ there will be a .LIB file created along with it.

    then all you have to do is include the LIB in your project settings, make a header to call to the LIB which calls your DLL.

    so in the header you would put:

    Code:
    void _stdcall funcAdd(int a, int b, int &c);
    // yes there is supposed to be only one underscore infront of stdcall.

  5. #5

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    I'm using Borland command line compiler 5.5, there is no .lib file. I know I can include the header file and link with the .lib file, but I am doing this for practice so I know how to use DLLs for which I don't have a header and .lib file (cards.dll for example).
    Alcohol & calculus don't mix.
    Never drink & derive.

  6. #6
    Zaei
    Guest
    If you are using LoadLibrary, there is no reason for a .lib file. That is only required when implicitly linking with the library.

    Do you have a .def file? If I understand correctly, if you dont, the names get mangled, and "funcAdd" wont work. It appears that GetProcAddress() isnt finding the function specified, but you try to use the function anyway.

    if __stdcall compiles, there is no reason to change it, unless you plan on compiling on several platforms... in which case you might not be developing DLLs.

    Z.

  7. #7

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Red face

    I figured it out The dependency walker lists the function as @funcAdd$qqsiiri, so I changed funcAdd in GetProcAddress() to that and now it works. One question though, how would I change it from @funcAdd$qqsiiri to funcAdd in the DLL? (Keep in mind I am using borland command line tools 5.5)
    Alcohol & calculus don't mix.
    Never drink & derive.

  8. #8
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    if u don't use extern C { } block in DLL, the compiler names the functions with extra stuff in them. To rename the function in a DLL you will need to access the DLL inside (source & recompile)
    VS.NET 2003

    Need to email me?

  9. #9
    Zaei
    Guest
    Like I said, you need a .def file.

    Z.

  10. #10
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    Zaei - don't u need a def file when the function is

    void _stdcall BlaBla
    {


    }

    because C++ renames it anyway, with extern C {} no _stdcall, no def?
    VS.NET 2003

    Need to email me?

  11. #11
    Zaei
    Guest
    I dont use def files much, so i dont know exactly what they do, but for all intents, they remove the name mangling from the function. So, funcAdd is really funcAdd, instead of the mangled version. extern "C" tells the compiler that the function IS a C function. __stdcall simply tells the compiler which calling convention to use.

    Z.

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