National Instruments 320685D-01 ユーザーズマニュアル

ページ / 211
Chapter 3
Windows 95/NT Compiler/Linker Issues
©
 National Instruments Corporation
3-3
LabWindows/CVI Programmer Reference Manual
callback in a DLL, you must take special care. Refer to the 
 section in Chapter 2, 
, of this manual, for a detailed 
discussion of run state change callbacks. 
DllMain 
Each DLL can have a 
DllMain
 function, except that the Borland compiler uses 
DllEntryPoint
 as the name. The operating system calls the 
DllMain
 function with various 
messages. To generate the template for a 
DllMain
 function, use the Insert Constructs 
command in the Edit menu of a Source window. 
Use caution when inserting code in the 
PROCESS_ATTACH
 and 
PROCESS_DETACH
 cases. In 
particular, avoid calling into other DLLs in these two cases. The order in which 
Windows 95/NT initializes DLLs at startup and unloads them at process termination is not 
well-defined. Thus, the DLLs you want to call might not be in memory when your 
DllMain
 
receives the 
PROCESS_ATTACH
 or 
PROCESS_DETACH
 message.
It is always safe to call into the LabWindows/CVI Run-time Engine in a run state change 
callback, as long as you do so before calling 
CloseCVIRTE
.
Releasing Resources when a DLL Unloads
When a program terminates, the operating system disposes resources your DLL allocates. If 
your DLL remains loaded throughout program execution, it does not need to dispose 
resources explicitly when the system unloads it at program termination. However, if the 
program unloads your DLL during program execution, it is a good idea for your DLL to 
dispose of any resources it allocates. It can release resources in the 
DllMain
 function in 
response to the 
PROCESS_DETACH
 message. The DLL can also release resources in a function 
that it registers with the ANSI C 
atexit
 function. The system calls the function you register 
when the DLL receives the 
PROCESS_DETACH
 message.
If your DLL calls into the LabWindows/CVI Run-time Engine DLL, it can allocate resources 
such as user interface panels. If a program unloads your DLL during execution, you might 
want to dispose these resources by calling functions such as 
DisposePanel
 in the 
LabWindows/CVI Run-time Engine. On the other hand, as explained in the previous section, 
it is generally unsafe to call into other DLLs in response to the 
PROCESS_DETACH
 message. 
To solve this dilemma, you can use the 
CVIRTEHasBeenDetached
 function in the 
Utility Library. It is always safe to call the 
CVIRTEHasBeenDetached
 function. 
CVIRTEHasBeenDetached
 returns FALSE until the main Run-time Engine DLL, 
cvirte.dll
, receives the 
PROCESS_DETACH
 message. Consequently, if 
CVIRTEHasBeenDetached
 returns FALSE, your DLL can safely call functions in 
LabWindows/CVI Run-time Engine to release resources. 
00ProRef.book : 06chap03.fm  Page 3  Monday, March 9, 1998  3:23 PM