分享
 
 
 

How To Use the C Run-Time

王朝c/c++·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

Section 1: Three Forms of C Run-Time (CRT) Libraries Are Available

There are three forms of the C Run-time library provided with the Win32

SDK:

LIBC.LIB is a statically linked library for single-threaded programs.

LIBCMT.LIB is a statically linked library that supports multithreaded

programs.

CRTDLL.LIB is an import library for CRTDLL.DLL that also supports

multithreaded programs. CRTDLL.DLL itself is part of Windows NT.

Microsoft Visual C++ 32-bit edition contains these three forms as well,

however, the CRT in a DLL is named MSVCRT.LIB. The DLL is redistributable.

Its name depends on the version of VC++ (ie MSVCRT10.DLL or MSVCRT20.DLL).

Note however, that MSVCRT10.DLL is not supported on Win32s, while

CRTDLL.LIB is supported on Win32s. MSVCRT20.DLL comes in two versions: one

for Windows NT and the other for Win32s.

Back to the top

Section 2: Using the CRT Libraries When Building a DLL

When building a DLL which uses any of the C Run-time libraries, in order to

ensure that the CRT is properly initialized, either

1.

the initialization function must be named DllMain() and the entry point

must be specified with the linker option -entry:_DllMainCRTStartup@12

- or -

2.

the DLL's entry point must explicitly call CRT_INIT() on process attach

and process detach

This permits the C Run-time libraries to properly allocate and initialize C

Run-time data when a process or thread is attaching to the DLL, to properly

clean up C Run-time data when a process is detaching from the DLL, and for

global C++ objects in the DLL to be properly constructed and destructed.

The Win32 SDK samples all use the first method. Use them as an example.

Also refer to the Win32 Programmer's Reference for DllEntryPoint() and the

Visual C++ documentation for DllMain(). Note that DllMainCRTStartup() calls

CRT_INIT() and CRT_INIT() will call your application's DllMain(), if it

exists.

If you wish to use the second method and call the CRT initialization code

yourself, instead of using DllMainCRTStartup() and DllMain(), there are two

techniques:

1.

if there is no entry function which performs initialization code, simply

specify CRT_INIT() as the entry point of the DLL. Assuming that you've

included NTWIN32.MAK, which defines DLLENTRY as "@12", add the following

option to the DLL's link line:

-entry:_CRT_INIT$(DLLENTRY)

- or -

2.

if you *do* have your own DLL entry point, do the following in the entry

point:

a.

Use this prototype for CRT_INIT():

BOOL WINAPI _CRT_INIT(HINSTANCE hinstDLL, DWORD fdwReason,

LPVOID lpReserved);

For information on CRT_INIT() return values, see the documentation

DllEntryPoint; the same values are returned.

b.

On DLL_PROCESS_ATTACH and DLL_THREAD_ATTACH (see "DllEntryPoint" in

the Win32 API reference for more information on these flags), call

CRT_INIT(), first, before any C Run-time functions are called or any

floating-point operations are performed.

c.

Call your own process/thread initialization/termination code.

d.

On DLL_PROCESS_DETACH and DLL_THREAD_DETACH, call CRT_INIT() last,

after all C Run-time functions have been called and all floating-

point operations are completed.

Be sure to pass on to CRT_INIT() all of the parameters of the entry

point; CRT_INIT() expects those parameters, so things may not work

reliably if they are omitted (in particular, fdwReason is required to

determine whether process initialization or termination is needed).

Below is a skeleton sample entry point function that shows when and how

to make these calls to CRT_INIT() in the DLL entry point:

BOOL WINAPI DllEntryPoint(HINSTANCE hinstDLL, DWORD fdwReason,

LPVOID lpReserved)

{

if (fdwReason == DLL_PROCESS_ATTACH || fdwReason == DLL_THREAD_ATTACH)

if (!_CRT_INIT(hinstDLL, fdwReason, lpReserved))

return(FALSE);

if (fdwReason == DLL_PROCESS_DETACH || fdwReason == DLL_THREAD_DETACH)

if (!_CRT_INIT(hinstDLL, fdwReason, lpReserved))

return(FALSE);

return(TRUE);

}

NOTE that this is *not* necessary if you are using DllMain() and

-entry:_DllMainCRTStartup@12.

Back to the top

Section 3: Using NTWIN32.MAK to Simplify the Build Process

There are macros defined in NTWIN32.MAK that can be used to simplify your

makefiles and to ensure that they are properly built to avoid conflicts.

For this reason, Microsoft highly recommends using NTWIN32.MAK and the

macros therein.

For compilation, use:

$(cvarsdll) for apps/DLLs using CRT in a DLL

For linking, use one of the following:

$(conlibsdll) for console apps/DLLs using CRT in a DLL

$(guilibsdll) for GUI apps using CRT in a DLL

Back to the top

Section 4: Problems Encountered When Using Multiple CRT Libraries

If an application that makes C Run-time calls links to a DLL that also

makes C Run-time calls, be aware that if they are both linked with one of

the statically-linked C Run-time libraries (LIBC.LIB or LIBCMT.LIB), the

.EXE and DLL will have separate copies of all C Run-time functions and

global variables. This means that C Run-time data cannot be shared between

the .EXE and the DLL. Some of the problems that can occur as a result are:

Passing buffered stream handles from the .EXE/DLL to the other module

Allocating memory with a C Run-time call in the .EXE/DLL and

reallocating or freeing it in the other module

Checking or setting the value of the global errno variable in the

.EXE/DLL and expecting it to be the same in the other module. A related

problem is calling perror() in the opposite module from where the C Run-

time error occurred, since perror() uses errno.

To avoid these problems, link both the .EXE and DLL with CRTDLL.LIB or

MSVCRT.LIB, which allows both the .EXE and DLL to use the common set of

functions and data contained within CRT in a DLL, and C Run-time data such

as stream handles can then be shared by both the .EXE and DLL.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有