DLL学习摘录2

 

摘录自"programming windows(charles petzold)"

 

1

a dynamic library module doesn't receive messages. However, a library module can call GetMessage and PeekMessage . The messages the library pulls from the queue with these functions are actually messages for the program that called the library function. In general, the library works on behalf of the program calling it. 也就是说你不能给一个DLL发送消息, 你只能给一个EXE发送消息. 但是DLL 可以调用getmessage 或者 peekmessage去读取EXE中的消息.

 

2

A dynamic library can load resources (such as icons, strings, and bitmaps) either from the library file or from the file of the program that calls the library. The functions that load resources require an instance handle. If the library uses its own instance handle (which is passed to the library during initialization), the library can obtain resources from its own file. To load resources from the calling program's .EXE file, the library function requires the instance handle of the program calling the function.

 

3

Registering window classes and creating windows in a library can be a little tricky. Both the window class structure and the CreateWindow call require an instance handle. Although you can use the library module's instance handle in creating the window class and the window, the window messages still go through the message queue of the program calling the library when the library creates the window. If you must create window classes and windows within a library, it's probably best to use the calling program's instance handle.

在DLL中你也可以进行创建窗口等方面的工作, 问题是即使你在DLL中用DLL的instance创建了该窗口,那DLL也不会得到那些发给该窗口的消息,必须要调用getMessage或PeekMessage去EXE的queue中读取消息.

 

4

Because messages for modal dialog boxes are retrieved outside a program's message loop, you can create a modal dialog box in a library by calling DialogBox . The instance handle can be that of the library, and the hwndParent argument to DialogBox can be set to NULL.

 

5

动态使用dll:

typedef BOOL (WINAPI * PFNRECT) (HDC, int, int, int, int) ;

HANDLE hLibrary ;

PFNRECT pfnRectangle ;

Miscellaneous DLL Topics

 

 

hLibrary = LoadLibrary (TEXT ("GDI32.DLL"))
pfnRectangle = (PFNPRECT) GetProcAddress (hLibrary, TEXT ("Rectangle"))

pfnRectangle (hdc, xLeft, yTop, xRight, yBottom) ;
FreeLibrary (hLibrary) ;

Miscellaneous DLL Topics

Although this technique of run-time dynamic linking doesn't make much sense for the Rectangle function, it can come in handy when you don't know the name of the library module until run time.

The code above uses the LoadLibrary and FreeLibrary functions. Windows maintains "reference counts" for all library modules. LoadLibrary causes the reference count to be incremented. The reference count is also incremented when Windows loads any program that uses the library. FreeLibrary causes the reference count to be decremented, as does the termination of an instance of a program that uses this library. When the reference count is 0, Windows can discard the library from memory, because the library is no longer needed.


6
理解.rc .h??


7
Any function in a dynamic-link library that a Windows program or another library
can use must be exported. However, a DLL need not contain any exported
functions. What would such a DLL contain? The answer is resources.

8
Let's say you're working on a Windows application that requires a number of
bitmaps. Normally you would list these in the resource script of the program and
load them into memory with the LoadBitmap function. But perhaps you want
to create several sets of bitmaps, each set customized for one of the major
display resolutions commonly used with Windows. It would make most sense to
store these different sets of bitmaps in different files, because a user would
need only one set of bitmaps on the fixed disk. These files are resource-only
libraries.
正常情况下,我们如果需要一些bitmaps的话, 就会放在EXE中, 利用loadbitmap去使用它们.
但是当你有许多套的bitmaps的时候, 不同用户只需要其中的一套的话,那放在DLL中就有意义了.
而且这样的话也会减少EXE的空间.
关于如何制作这样的DLL, 可以参考programming windows(charles petzold)
figure21-5



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值