Thread: DEF files?

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    479

    DEF files?

    how do you create them? i read somewhere that you need them in dlls sometimes


    //edit
    i am using msv6

    //edit 2

    i have this in my DLL header
    Code:
    #ifndef DLLCODE_H
    #define DLLCODE_H
    #include "windows.h"
    
    
    extern "C" LRESULT __declspec(dllexport)  CALLBACK  __stdcall  Proc(int,WPARAM,LPARAM);
    
    #endif
    in MSDN it says DEF files arent needed if you use __declspec

    in my DLL cpp file i use the function:
    Code:
    LRESULT  __stdcall CALLBACK  Proc(int code,WPARAM wParam,LPARAM lParam)
    still i cant load the function from my EXE file
    Last edited by pode; 12-28-2004 at 01:44 AM.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Cant try your code, but put the extern "C" in front of the declaration and definition

    Like so

    Code:
    #define DllExport extern "C" __declspec (dllexport) 
     
    DllExport CALLBACK  Proc(int code,WPARAM wParam,LPARAM lParam)

  3. #3
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Ok so go to FILE -> New -> FILE

    It should bring up a list of options for you to select from. Select Def file.

    In the def file start with

    LIBRARY <add name of your dll file here> (no < or >)

    Then at the next line write EXPORTS

    Then list all the functions you wish to export.

    Here is an example:

    Code:
    LIBRARY myDll
    EXPORTS
                  myFunction1
                  myFunction2
    Thats it, happy coding!
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    i dont have that in my file>new>files>
    is it really called def file?

    see my attached picture.

  5. #5
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Allright, select text file, that is essentially all a definition file is. Don't forget to name the extension .def.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    ok thx
    but i got some questions.
    should i create the function prototypes there only
    or the final function, do i put this in the exe folder?
    should i change anything in the dll now?

  7. #7
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    should i create the function prototypes there only
    Only the function NAMES

    or the final function, do i put this in the exe folder?
    This file is created in the same workspace / folder of your dll source file. It is used as linker input not for the final product.

    should i change anything in the dll now?
    No, you shouldn't have to change anything with the dll.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    in my DLL cpp file i use the function:
    Code:
    LRESULT  __stdcall CALLBACK  Proc(int code,WPARAM wParam,LPARAM lParam)
    That function is a little redundant since CALLBACK and __stdcall are the same thing.

    In your def file, you need to put:
    _Proc@12 instead of just Proc since the function uses the stdcall calling convention.

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    ok i've been up all night + morning thinking about the problem and i think i've tried everything, and still i cant get it to work
    ill post the code, maybe someone knows about this problem.

    note: some changes have been done from previous posts.

    the dll.h
    Code:
    #ifndef DLLKOD_H
    #define DLLKOD_H
    #define DllExport extern "C" __declspec (dllexport) 
    #include "windows.h"
    
    
    HHOOK hok;
    
    extern "C" LRESULT CALLBACK Proc(int,WPARAM,LPARAM);
    
    #endif
    the dll.cpp
    Code:
    
    #include "dllkod.h"
    
    
    LRESULT CALLBACK __stdcall Proc(int code,WPARAM wParam,LPARAM lParam)
    {
      return CallNextHookEx(hok,code,wParam,lParam);
    }
    the program cpp shorted down for you
    Code:
    #include <iostream.h>
    #include <windows.h>
    
    int main()
    {
         
    	 static HINSTANCE hi;
    	 HOOKPROC hProc;	
         hi = LoadLibrary("podedll.dll");
        
    	if(hi==0)
    
        cout<<"could not load dll";
    
        hProc=(HOOKPROC)GetProcAddress(hi,"_Proc@12");
    	
    
        if(hProc==0)
        cout<<"could not load hook function";
    	 
    	return 0;
    }
    and the def file
    Code:
    LIBRARY "podedll.dll"
    EXPORTS           
            Proc
    if the def looks like that without the "_" before and "@12" after
    i get the error
    Code:
    pode.def : error LNK2001: unresolved external symbol Proc
    Debug/mainprog.lib : fatal error LNK1120: 1 unresolved externals
    LINK : fatal error LNK1141: failure during build of exports file
    Error executing link.exe.
    
    mainprog.exe - 3 error(s), 0 warning(s)
    else i get
    Code:
    mainprog.exp : warning LNK4070: /OUT:podedll.dll directive in .EXP differs from output filename "Debug/mainprog.exe"; ignoring directive
    Debug/mainprog.exe : warning LNK4086: entrypoint "_mainCRTStartup" is not __stdcall with 12 bytes of arguments; image may not run
    mainprog.exp : error LNK2001: unresolved external symbol _Proc@12
    Debug/mainprog.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    
    mainprog.exe - 2 error(s), 2 warning(s)
    thx!

    //edit
    maybe i havent linked it properly!?
    Last edited by pode; 12-29-2004 at 02:24 AM.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I'm confused, are you getting these errors when compiling the exe or the dll? You do realize that the .def file is for the dll and not the exe, right?

  11. #11
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    LIBRARY "podedll.dll"
    EXPORTS
    Proc
    Should be

    Code:
    LIBRARY podedll
    EXPORTS           
            Proc
    Here is an application that works(the def file is the txt file. you will need to rename it with an extension .def for it to work) It uses a dll function to cause the program to beep:
    Last edited by andyhunter; 12-29-2004 at 05:02 AM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Actually the def file was fine as it was.

    The way to do it without defs is to have a common header for both the dll and the client to use, but with a #define like:

    #ifdef MYDLL_BUILD
    #define MYDLL_PORT __declspec(dllexport)
    #else
    #define MYDLL_PORT __dllclspec(dllimport)
    #endif

    Then prototype your exported functions in that header:

    MYDLL_PORT int MyFunc(int);

    Then when you build the DLL, define MYDLL_BUILD (MSVC defines a macro for the project anyway, see the proj. options, so you can use that instead), and the functions get exported.

    When you build the client, MYDLL_BUILD isn't defined, so it imports the functions. Just make sure the dll is in a place the linker can find it.

    It looks like a lot of work, but since you should have a common header anyway so the client gets the fn. prototypes, in real terms it's minimal.

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    Quote Originally Posted by bithub
    I'm confused, are you getting these errors when compiling the exe or the dll? You do realize that the .def file is for the dll and not the exe, right?

    aaha!

    so i would add the def file to the DLL project rather than the main program project?

    thx bithub for pointing that out
    i that fixed the problem heh

    and thanks everyone else for their help!
    Last edited by pode; 12-29-2004 at 12:59 PM.

  14. #14
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    azteched, thats a great way of doing things if you have a way to access both the client and server. However most of the cases you do not have access to both, usually only one or the other.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    This is about writing a library, you need to provide function+data type declarations for users, in a header you distribute. You don't need any access to code the library user writes themselves.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  2. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  5. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM