Thread: writing a dll in visual studio 2008

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    writing a dll in visual studio 2008

    I am trying to write a dll file with visual studio 2008 and i am having a problem. here is the source code for the dll:

    Code:
    #include <windows.h>
    #include <winsock.h>
    
    BOOL WINAPI DllMain (HANDLE hModule, DWORD dwFunction, LPVOID lpNot)
    {
    	return TRUE;
    }
    
    long _stdcall add_num (long a, long b)
    {
    	return ((long)(a+b));
    }
    i cant seem to get this dll function to work though. I dont have any build errors or anything but i cant get the dll to load into my liberty basic project for some reason i keep getting a dll load error. I know that i am supposed to use a .def file but i cant seem to figure out how to use it with visual studio 2008. Im really messed up here.
    HA! I WIN!

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    you are not exporting it. Add a __declspec(dllexport) compile directive to your functions signature. Also for better header reuse (since it will also be required to call your exported functions), you usually see something like this done.

    Code:
    #ifdef DLL_EXPORT
    #define MY_API __declspec(dllexport)
    #else //DLL_EXPORT
    #define MY_API __declspec(dllimport)
    #endif //!DLL_EXPORT

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    ??? Ok, now im really confused. i changed my function to the call you said to, but it still wont work. I'm really lost. maybe i'll try to read up on it tomorrow some more?
    HA! I WIN!

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Are you placing the dll in the same directory as your executable? What exactly is the error?

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    To clarify what valaris said.

    A DLL must EXPORT a function (for apps to use).
    To use a DLL function you must IMPORT it.

    This code snippet allows you to use the same header file in the DLL and the app (avoiding any issues from maintaining diff import/export header files).

    Code:
    #ifdef DLL_EXPORT
    #define MY_API __declspec(dllexport)
    #else //DLL_EXPORT
    #define MY_API __declspec(dllimport)
    #endif //!DLL_EXPORT
    This code is placed at the top of the DLL header file (before function declarations).
    Change the function declaration to;

    DLL_EXPORT int MyFunc(int iInput);

    In the DLL c./.cpp file you first #define DLL_EXPORT and then include the DLL header.

    In the APP you only include the header (and do not define DLL_EXPORT).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. multiple errors generated by socket headers
    By nocturna_gr in forum Windows Programming
    Replies: 5
    Last Post: 12-16-2007, 06:33 PM
  3. Problem Compiling Program
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 11-06-2007, 12:56 PM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM