Mercurial > dwindows
comparison win/dw.c @ 557:1a210e2f214b
Added a bunch of support routines I now own due to the settlement
agreement with F/X. Shared memory and named event semaphores in
particular.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Sat, 17 Apr 2004 05:38:09 +0000 |
parents | 0d47bec8a5fb |
children | c0a708e2cba3 |
comparison
equal
deleted
inserted
replaced
556:f4093dce8155 | 557:1a210e2f214b |
---|---|
25 | 25 |
26 HINSTANCE DWInstance = NULL; | 26 HINSTANCE DWInstance = NULL; |
27 | 27 |
28 DWORD dwVersion = 0, dwComctlVer = 0; | 28 DWORD dwVersion = 0, dwComctlVer = 0; |
29 DWTID _dwtid = -1; | 29 DWTID _dwtid = -1; |
30 SECURITY_DESCRIPTOR _dwsd; | |
30 | 31 |
31 #define PACKVERSION(major,minor) MAKELONG(minor,major) | 32 #define PACKVERSION(major,minor) MAKELONG(minor,major) |
32 | 33 |
33 #define IS_IE5PLUS (dwComctlVer >= PACKVERSION(5,80)) | 34 #define IS_IE5PLUS (dwComctlVer >= PACKVERSION(5,80)) |
34 #define IS_WINNTOR95 (((LOBYTE(LOWORD(dwVersion))) < 5) && (HIBYTE(LOWORD(dwVersion)) < 10)) | 35 #define IS_WINNTOR95 (((LOBYTE(LOWORD(dwVersion))) < 5) && (HIBYTE(LOWORD(dwVersion)) < 10)) |
3264 MyGetMenuInfo = (void*)GetProcAddress(huser, "GetMenuInfo"); | 3265 MyGetMenuInfo = (void*)GetProcAddress(huser, "GetMenuInfo"); |
3265 MySetMenuInfo = (void*)GetProcAddress(huser, "SetMenuInfo"); | 3266 MySetMenuInfo = (void*)GetProcAddress(huser, "SetMenuInfo"); |
3266 FreeLibrary(huser); | 3267 FreeLibrary(huser); |
3267 } | 3268 } |
3268 | 3269 |
3270 /* Initialize Security for named events and memory */ | |
3271 InitializeSecurityDescriptor(&_dwsd, SECURITY_DESCRIPTOR_REVISION); | |
3272 SetSecurityDescriptorDacl(&_dwsd, TRUE, (PACL) NULL, FALSE); | |
3273 | |
3269 return 0; | 3274 return 0; |
3270 } | 3275 } |
3271 | 3276 |
3272 /* | 3277 /* |
3273 * Runs a message loop for Dynamic Windows. | 3278 * Runs a message loop for Dynamic Windows. |
7868 */ | 7873 */ |
7869 int API dw_event_close(HEV *eve) | 7874 int API dw_event_close(HEV *eve) |
7870 { | 7875 { |
7871 if(eve) | 7876 if(eve) |
7872 return CloseHandle(*eve); | 7877 return CloseHandle(*eve); |
7873 return FALSE; | 7878 return 0; |
7879 } | |
7880 | |
7881 /* Create a named event semaphore which can be | |
7882 * opened from other processes. | |
7883 * Parameters: | |
7884 * eve: Pointer to an event handle to receive handle. | |
7885 * name: Name given to semaphore which can be opened | |
7886 * by other processes. | |
7887 */ | |
7888 HEV API dw_named_event_new(char *name) | |
7889 { | |
7890 SECURITY_ATTRIBUTES sa; | |
7891 | |
7892 sa.nLength = sizeof( SECURITY_ATTRIBUTES); | |
7893 sa.lpSecurityDescriptor = &_dwsd; | |
7894 sa.bInheritHandle = FALSE; | |
7895 | |
7896 return CreateEvent(&sa, TRUE, FALSE, name); | |
7897 } | |
7898 | |
7899 /* Destroy this semaphore. | |
7900 * Parameters: | |
7901 * eve: Handle to the semaphore obtained by | |
7902 * a create call. | |
7903 */ | |
7904 HEV API dw_named_event_get(char *name) | |
7905 { | |
7906 return OpenEvent(EVENT_ALL_ACCESS, FALSE, name); | |
7907 } | |
7908 | |
7909 /* Resets the event semaphore so threads who call wait | |
7910 * on this semaphore will block. | |
7911 * Parameters: | |
7912 * eve: Handle to the semaphore obtained by | |
7913 * an open or create call. | |
7914 */ | |
7915 int API dw_named_event_reset(HEV eve) | |
7916 { | |
7917 int rc; | |
7918 | |
7919 rc = ResetEvent(eve); | |
7920 if(!rc) | |
7921 return 1; | |
7922 | |
7923 return 0; | |
7924 } | |
7925 | |
7926 /* Sets the posted state of an event semaphore, any threads | |
7927 * waiting on the semaphore will no longer block. | |
7928 * Parameters: | |
7929 * eve: Handle to the semaphore obtained by | |
7930 * an open or create call. | |
7931 */ | |
7932 int API dw_named_event_post(HEV eve) | |
7933 { | |
7934 int rc; | |
7935 | |
7936 rc = SetEvent(eve); | |
7937 if(!rc) | |
7938 return 1; | |
7939 | |
7940 return 0; | |
7941 } | |
7942 | |
7943 /* Waits on the specified semaphore until it becomes | |
7944 * posted, or returns immediately if it already is posted. | |
7945 * Parameters: | |
7946 * eve: Handle to the semaphore obtained by | |
7947 * an open or create call. | |
7948 * timeout: Number of milliseconds before timing out | |
7949 * or -1 if indefinite. | |
7950 */ | |
7951 int API dw_named_event_wait(HEV eve, unsigned long timeout) | |
7952 { | |
7953 int rc; | |
7954 | |
7955 rc = WaitForSingleObject(eve, timeout); | |
7956 switch (rc) | |
7957 { | |
7958 case WAIT_FAILED: | |
7959 rc = DW_ERROR_TIMEOUT; | |
7960 break; | |
7961 | |
7962 case WAIT_ABANDONED: | |
7963 rc = DW_ERROR_INTERRUPT; | |
7964 break; | |
7965 | |
7966 case WAIT_OBJECT_0: | |
7967 rc = 0; | |
7968 break; | |
7969 } | |
7970 | |
7971 return rc; | |
7972 } | |
7973 | |
7974 /* Release this semaphore, if there are no more open | |
7975 * handles on this semaphore the semaphore will be destroyed. | |
7976 * Parameters: | |
7977 * eve: Handle to the semaphore obtained by | |
7978 * an open or create call. | |
7979 */ | |
7980 int API dw_named_event_close(HEV eve) | |
7981 { | |
7982 int rc; | |
7983 | |
7984 rc = CloseHandle(eve); | |
7985 if(!rc) | |
7986 return 1; | |
7987 | |
7988 return 0; | |
7989 } | |
7990 | |
7991 /* | |
7992 * Allocates a shared memory region with a name. | |
7993 * Parameters: | |
7994 * handle: A pointer to receive a SHM identifier. | |
7995 * dest: A pointer to a pointer to receive the memory address. | |
7996 * size: Size in bytes of the shared memory region to allocate. | |
7997 * name: A string pointer to a unique memory name. | |
7998 */ | |
7999 HSHM API dw_named_memory_new(void **dest, int size, char *name) | |
8000 { | |
8001 SECURITY_ATTRIBUTES sa; | |
8002 HSHM handle; | |
8003 | |
8004 sa.nLength = sizeof(SECURITY_ATTRIBUTES); | |
8005 sa.lpSecurityDescriptor = &_dwsd; | |
8006 sa.bInheritHandle = FALSE; | |
8007 | |
8008 handle = CreateFileMapping((HANDLE)0xFFFFFFFF, &sa, PAGE_READWRITE, 0, size, name); | |
8009 | |
8010 if(!handle) | |
8011 return 0; | |
8012 | |
8013 *dest = MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, 0); | |
8014 | |
8015 if(!*dest) | |
8016 { | |
8017 CloseHandle(handle); | |
8018 return 0; | |
8019 } | |
8020 | |
8021 return handle; | |
8022 } | |
8023 | |
8024 /* | |
8025 * Aquires shared memory region with a name. | |
8026 * Parameters: | |
8027 * dest: A pointer to a pointer to receive the memory address. | |
8028 * size: Size in bytes of the shared memory region to requested. | |
8029 * name: A string pointer to a unique memory name. | |
8030 */ | |
8031 HSHM API dw_named_memory_get(void **dest, int size, char *name) | |
8032 { | |
8033 HSHM handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, name); | |
8034 | |
8035 if(!handle) | |
8036 return 0; | |
8037 | |
8038 *dest = MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, 0); | |
8039 | |
8040 if(!*dest) | |
8041 { | |
8042 CloseHandle(handle); | |
8043 return 0; | |
8044 } | |
8045 | |
8046 return handle; | |
8047 } | |
8048 | |
8049 /* | |
8050 * Frees a shared memory region previously allocated. | |
8051 * Parameters: | |
8052 * handle: Handle obtained from DB_named_memory_allocate. | |
8053 * ptr: The memory address aquired with DB_named_memory_allocate. | |
8054 */ | |
8055 int API dw_named_memory_free(HSHM handle, void *ptr) | |
8056 { | |
8057 UnmapViewOfFile(ptr); | |
8058 CloseHandle(handle); | |
8059 return 0; | |
7874 } | 8060 } |
7875 | 8061 |
7876 /* | 8062 /* |
7877 * Creates a new thread with a starting point of func. | 8063 * Creates a new thread with a starting point of func. |
7878 * Parameters: | 8064 * Parameters: |