PRB: C Run-Time locking Function Causes Sharing Violations

ID Number: Q85284

3.00

WINDOWS

Summary:

SYMPTOMS

The locking function in the Microsoft C run-time library may cause

a sharing violation when it is called under the following

conditions:

- The locking function is called from a dynamic-link library

(DLL).

- The DLL is stored on a network drive.

- The locking function is called with the LK_UNLCK parameter to

unlock a specified number of bytes.

- The unlocked bytes were not previously locked with a call to the

locking function with the LK_LOCK parameter.

CAUSE

This error is the result of incorrect usage and should not be

considered a problem with the Windows Software Development Kit

(SDK) itself.

RESOLUTION

This problem occurs only with the libraries provided with the

Windows SDK version 3.0. To work around this problem, rewrite the

code that calls the locking function to unlock only bytes that have

been previously locked.

More Information:

The following code demonstrates the problem described above:

/* Compile options needed: /Gsw /Zp

* Link options needed: /ALIGN:16

*/

#include <windows.h>

#include <io.h>

#include <fcntl.h>

#include <sys\locking.h>

#include <share.h>

#include <errno.h>

int _far _pascal LibMain(HANDLE h, WORD ds, WORD hs, LPSTR c)

{return 1;}

void _far _pascal WEP(int nParam) {}

void _far _pascal testlock(void)

{

int fd = sopen("m:\\network.fil", O_BINARY | O_RDWR, SH_DENYNO);

if (fd >= 0)

{

int i;

errno = 0;

i = locking(fd, LK_UNLCK, 1L);

close(fd);

}

}

Additional reference words: 6.0 6.00 6.0a 6.00a 6.0ax 6.00ax 7.0 7.00