DLL variable export specification for. Def file writing

Source: Internet
Author: User
Notes when writing *. Def in VC ++
Note the following when exporting global variables in the DLL:
The following two
Lib. Def description line, indicating the name of the def File
Library "dlltest"
The above shows that the dynamic link library is dlltest. In many DLL def configuration files, some use double quotation marks to enclose the names, and some do not know why, I didn't try out any difference. Who knows? Please tell me. Thank you.
Exports
; Export keywords. Write the export function and the export variable below.
Getglobalvar
; Export functions
Dllglobalvar data
This sentence is the key. After you write it like this, the compilation method used by VC ++ will be smaller, but this is the statement recommended by VC ++. Actually, this sentence was originally
; Dllglobalvar constant
; If you use the above sentence, VC ++ will remind you when compiling, this method is old, please do not use, however, if you use a new method, you will be depressed.
Due to the subsequent variable export, you will encounter a problem that cannot be connected:
Compiling...
Main. cpp
Linking...
Main. OBJ: Error lnk2001: unresolved external symbol "int dllglobalvar "(? Dllglobalvar @ 3ha)
Debug/dllcall.exe: Fatal error lnk1120: 1 unresolved externals
Error executing link.exe.
Dllcall.exe-2 error (s), 0 warning (s)
No matter how you write it repeatedly *. def. This is useless because you use the newer dllglobalvar data, which is compiled by VC ++ *. the LIB file is about 2 k smaller than the dllglobalvar constant file, and this is precisely the root cause of the connection failure. I only know the problem here, but what is the cause, thank you for your understanding.
The call statement is as follows:
// Main. cpp
# Include <stdio. h>
# Include <windows. h>
# Pragma comment (Lib, "dllvar1.lib ")
Extern "C" _ declspec (dllimport) getglobalvar ();
Extern int dllglobalvar;
Int main (INT argc, char * argv [])
{
Printf ("this is % d/N", getglobalvar ());
* (Int *) dllglobalvar = 0;
Printf ("now data is % d/N", getglobalvar ());
Return 0;
}
// Lib. h
# Ifndef lib_h
# Define lib_h
Extern "C" int getglobalvar ();
Extern int dllglobalvar;
# Endif
// Lib. cpp
# Include "Lib. H"
# Include <windows. h>
Int dllglobalvar;
Bool apientry dllmain (handle hmodule,
DWORD ul_reason_for_call,
Lpvoid lpreserved)
{
Switch (ul_reason_for_call)
{
Case dll_process_attach:
Dllglobalvar = 100;
Break;
Case dll_thread_attach:
Case dll_thread_detach:
Case dll_process_detach:
Break;
}
Return true;
}
Int getglobalvar ()
{
Return dllglobalvar;
}
; LIB. Def
Library "dllvar1"
Exports
Dllglobalvar constant
Getglobalvar
Write it here. To sum it up, you must have a deep understanding of the compilation methods of programming tools. If you do not know the cards in your hands, you must be blind, in the beginning, you will lose.
In programming, you must pay attention to the updates of these codes. Some new codes will be compiled and some must use old conventions. Otherwise, you will not know how to die.
In addition, I need to understand the compilation content of the compilation tool. I don't know what the difference between constant and data is. It seems that there are many places to be supplemented.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.