INF:Some Init & Terminate Routines Not Called for CDLLOBJS.LIB

ID Number: Q70785

6.00 6.00a

OS/2

Summary:

The XI, XIF, XC, and XCF segments contain pointers to functions that

should be run at startup and termination of an application. Because of

a design limitation in the current implementation of the CDLLOBJS.LIB

run-time model (that is, C run-time in a DLL), these pointers are not

called and the code is not executed. This may cause problems with

certain libraries or products from other vendors that rely on the

execution of this code. The example below contains a workaround for

this limitation.

More Information:

The addresses in the XI (near initialization) and XIF (far

initialization) segments are normally called during the startup of the

C run time [before main() is called]. In addition, the XC (near

termination) and XCF (far termination) segments are used to hold

pointers that are called during cleanup [after main()]. Several

libraries and vendors rely on this fact and add pointers to

initialization routines to these segments. For instance:

1. GRTEXTP.OBJ places a call to the graphics library initialization

code [_QCGINIT()] in the XI segment.

2. Glockenspeil C++ places pointers to static constructors in the XI

segment and pointers to static destructors in the XC segment.

In the current design of the CDLLOBJS.LIB run-time model, the above

segments are never processed and the required code is not executed.

The sample code below illustrates a possible workaround for this

situation by creating two functions called Auto_Init() and

Auto_Term(). To apply the workaround, call Auto_Init() as the first

item in main() and Auto_Term() as the last. For example:

Sample Code

-----------

void Auto_Init(void);

void Auto_Term(void);

void main(void)

{

Auto_Init();

{your code goes here}

Auto_Term();

}

Sample Code

-----------

; Assemble options needed: /Cx (/Mx with MASM 5.x)

.286p

.MODEL LARGE,C

; Far Initializers (XIF Segment)

XIFB segment word public 'DATA'

xifbegin label byte

XIFB ends

XIFE segment word public 'DATA'

xifend label byte

XIFE ends

; Near Initializers (XI Segment)

XIB segment word public 'DATA'

xibegin label byte

XIB ends

XIE segment word public 'DATA'

xiend label byte

XIE ends

; Near Terminators (XC Segment)

XCB segment word public 'DATA'

xcbegin label byte

XCB ends

XCE segment word public 'DATA'

xcend label byte

XCE ends

; Far Terminators (XCF Segment)

XCFB segment word public 'DATA'

xcfbegin label byte

XCFB ends

XCFE segment word public 'DATA'

xcfend label byte

XCFE ends

.CODE

Auto_Init proc uses si di

mov si,offset @data:xifbegin

mov di,offset @data:xifend

call initterm

mov si,offset @data:xibegin

mov di,offset @data:xiend

call initterm

ret

Auto_Init endp

Auto_Term proc uses si di

mov si,offset @data:xcbegin

mov di,offset @data:xcend

call initterm

mov si,offset @data:xcfbegin

mov di,offset @data:xcfend

call initterm

ret

Auto_Term endp

initterm:

cmp si,di

jae done

sub di,4

mov ax,[di]

or ax,[di+2]

jz initterm

call dword ptr [di]

jmp initterm

done:

ret

end

Additional reference words: 6.00 6.00a