摘录自"programming windows(charles petzold)"
1 Library Basics
Although a dynamic-link library module can have any extension (such as .EXE or .FON), the standard extension is .DLL. Only dynamic-link libraries with the extension .DLL will be loaded automatically by Windows. If the file has another extension, the program must explicitly load the module by using the LoadLibrary or LoadLibraryEx function.
2
Library Basics
Library: One Word, Many Meanings
Part of the confusion surrounding dynamic-link libraries results from the appearance of the word "library" in several different contexts. Besides dynamic-link libraries, we'll also be talking about "object libraries" and "import libraries."
An object library is a file with the extension .LIB containing code that is added to your program's .EXE file in the process called static linking when you run the linker. For example, in Microsoft Visual C++, the normal C run-time object library that you link with your program is called LIBC.LIB.
An import library is a special form of an object library file. Like object libraries, import libraries have the extension .LIB and are used by the linker to resolve function calls in your source code. However, import libraries contain no code. Instead, they provide the linker with information necessary to set up relocation tables within the .EXE file for dynamic linking. The KERNEL32.LIB, USER32.LIB, and GDI32.LIB files included with the Microsoft compiler are import libraries for Windows functions. If you call the Rectangle function in a program, GDI32.LIB tells LINK that this function is in the GDI32.DLL dynamic-link library. This information goes into the .EXE file so that Windows can perform dynamic linking with the GDI32.DLL dynamic-link library when your program is executed.
Object libraries and import libraries are used only during program development. Dynamic-link libraries are used during run time. A dynamic library must be present on the disk when a program is run that uses the library. When Windows needs to load a DLL module before running a program that requires it, the library file must be stored in the directory containing the .EXE program, the current directory, the Windows system directory, the Windows directory, or a directory accessible through the PATH string in the MS-DOS environment. (The directories are searched in that order.)
3 DLL的链接方法
一是静态链接. 首先在EXE中的工程中包含DLL中的.h文件. 然后在链接的时候链接DLL的LIB. 在EXE被执行时,
该DLL会被自动载入.
一是动态链接. 不用.h, 也不用.lib. 在EXE中调用loadlibrary(DLLName)将DLL载入.
4 模块化
比如你的程序中需要3D效果, 但是你对3D一无所知, 不要害怕, 你可以找一个关于3D的DLL文件, 在需要的时候调用
里面的函数就行了.
5 如何制作.lib(object libraries)?
6 dumpbin.exe 是一个查看文件的工具。它有很多参数,实现很多功能。是vc自带的一个工具。
比如:dumpbin/exports DLL.LIB,可以查看.lib(import libraries)中导出的函数名字.
dumpbin/dependents NAME.EXE 查看一个exe需要的.dll.
dumpbin/exports DLLNAME, 可以查看一个DLL中导出的函数。
7
Functions in a DLL that are used by an application must be "exported." This doesn't involve any tariffs or commerce regulations, just a few keywords that ensure that the function name is added to EDRLIB.LIB (so that the lin
ker can resolve the function name when linking an application that uses the function) and that the function is
visible from EDRLIB.DLL.
那么如果是动态链接的话还是必须要"EXPORTS"吗?
8
我在作这个练习时是新建一个workspace,里面包含两个projects, 一个是EXE的,一个是DLL的。
project->dependencies. 作用:设置某个project(1)依赖另外的project(2),这样在编译project(1)时,如果必要,vc会自动编译
project(2). 这样可以避免你有时候改了project(2)却忘记编译,而直接编译project(1)时候的困惑。而且这样的话,你在EXE的工程中就不
用再设置链接库了。
但是如果DLL不是你做的, 是别人作的,然后提供给你.dll .h .lib三个文件。那么你要在自己的源码中加入.h. 在设置里面设上链接库。且把
.lib放在和.dsw放在同一目录中。把.dll和.exe放在同一目录中。
你也可以在源代码中加入#pragma comment(lib, "XXX.lib") ,以代替在project->setting中的设置。
记住:每一个工具可以做的事,你都可以用代码做到。
9
动态链接时候只能用getprocaddress去调用函数?
应该是的,因为如果是动态链接,没有DLL的.h,你如果直接调函数,连编译都过不了。
你不需要作DLL的人提供函数名资料给你,你可以用dumpbin.exe获取。
10
import 在何时使用?
11
all memory it allocates is owned by the application. Any windows it creates are owned by the application. And any files it opens are owned by the application. Multiple applications can use the same DLL simultaneously, but under Windows these applications are shielded from interfering with each other.
12
Multiple processes can share the same code in a dynamic-link library. However, the data maintained by a DLL is different for each process. Each process has its own address space for any data the DLL uses.
13
It's very nice that Windows isolates applications that are using the same dynamic-link libraries at the same time. However, sometimes it's not preferable. You may want to write a DLL that contains some memory that can be shared among various applications, or perhaps among multiple instances of the same application. This involves using shared memory, which is actually a memory-mapped file.
正常情况下,一个EXE的多个实例之间,它们的数据都是各不相同的两份。各使用各的。
一个DLL如果被多个EXE所使用,那么这个DLL的代码只有一份,但是它为多个EXE产生的数据却互不相干。
但是某些时候如果需要打破这个规则,即让一个EXE的多个实例之间可以共享某一数据,或者让一个DLL的某些
数据可以被所有调用该DLL的EXE共享怎么办呢?
解决方案是: 自己创建一个“节”,并在其中存放你想共享的数据,然后设置该节的属性为共享就可以了。
具体方法:
创建节:
#pragma
data_seg("Shared")
LONG g_lInstanceCount = 0;//这里一定要赋值,否则不会共享!!!
#pragma
data_seg()
在编译器中设置该节的属性为共享,或直接在代码中调用#pragma
comment(linker, "/SECTION:Shared,RWS");
RWS: R 代表READ. W代表WRITE.S代表SHARED.
这儿有一个问题:如果两个不同的EXE,他们使用一个DLL,该DLL中有 SHARED DATA,那么这样两个EXE之间就可以通信了?
是的。如果这个SHARED DATA是个指针,那么如果一个EXE拿到另外一个EXE的指针,它对指针操作也是对自己的内存空间在操作,不会操作到另外一个
进程的。
总结:
两个不同的EXE或DLL通信,用内存映射文件,或者通过共同调用一个拥有SHARED SECTION的DLL来实现。
一个EXE的多个实例之间通信,可以用SHARED SECTION。
14 callback? far-pointer?