1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | // MyShmExample.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <Windows.h> #include <Windowsx.h> #include <stdio.h> #include <conio.h> #include <stdlib.h> #if 0 //Beispiel wie man mit eingeschalteter UAC für den lokalen Prozess die erforderlichen Rechte anfordern kann. static void* BuildSDForAccessToAllAuthenticatedUsers(SECURITY_DESCRIPTOR* pSD) { #if defined (_WIN32_WCE) return NULL; #else DWORD dwAclLength; PSID pAuthenticatedUsersSID = NULL; PACL pDACL = NULL; BOOL bResult = FALSE; PACCESS_ALLOWED_ACE pACE = NULL; SID_IDENTIFIER_AUTHORITY siaNT = SECURITY_NT_AUTHORITY; SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION; // initialize the security descriptor if (InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)) { // obtain a sid for the Authenticated Users Group if (AllocateAndInitializeSid(&siaNT, 1, SECURITY_AUTHENTICATED_USER_RID, 0, 0, 0, 0, 0, 0, 0, &pAuthenticatedUsersSID)) { // NOTE: // // The Authenticated Users group includes all user accounts that // have been successfully authenticated by the system. If access // must be restricted to a specific user or group other than // Authenticated Users, the SID can be constructed using the // LookupAccountSid() API based on a user or group name. // calculate the DACL length dwAclLength = sizeof(ACL) // add space for Authenticated Users group ACE + sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) + GetLengthSid(pAuthenticatedUsersSID); // allocate memory for the DACL pDACL = (PACL) malloc(dwAclLength); if (pDACL) { // initialize the DACL if (InitializeAcl(pDACL, dwAclLength, ACL_REVISION)) { // add the Authenticated Users group ACE to the DACL with // GENERIC_READ, GENERIC_WRITE, and GENERIC_EXECUTE access if (AddAccessAllowedAce(pDACL, ACL_REVISION, GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | SECTION_ALL_ACCESS, pAuthenticatedUsersSID)) { if (SetSecurityDescriptorDacl(pSD, TRUE, pDACL, FALSE)) bResult = TRUE; } } } } } if (!bResult) { if (pAuthenticatedUsersSID) FreeSid(pAuthenticatedUsersSID); if (pDACL) { free(pDACL); pDACL = NULL; } } return pDACL; #endif } #endif #include <WtsApi32.h> /* help functions for shared memory on windows vista */ typedef struct _COPIED_WTS_SESSION_INFO { DWORD SessionId; LPSTR pWinStationName; /* A-function, so we have a char-string */ DWORD dummy; /* originally an enum */ } COPIED_WTS_SESSION_INFO, *PCOPIED_WTS_SESSION_INFO; BOOL LocalSysCreateNamedShm(char* szName, unsigned long ulSize, HANDLE* phShmHandle, void** ppUserSpace) { HANDLE hProcess; unsigned long lMinimumWorkingSetSize; unsigned long lMaximumWorkingSetSize; // the global... is needed for the following case: // - runtime is running as a system service (the default for the plcwinnt) // - runtime wants to communicate with a process that does not run under the system accout // -> Will fail because of the different namespaces, at least in Windows Vista! // Problem occurred in the Targetvisu that is started from the service unter Vista char szNameBuffer[MAX_PATH]; char szGlobalPrefix[] = "Global\\"; char szSessionPrefix[] = "Session\\"; strcpy(szNameBuffer, szGlobalPrefix); strcat(szNameBuffer, szName); // prefer the global entry and afterwards check for the local name *phShmHandle = OpenFileMapping(FILE_MAP_WRITE, TRUE, szNameBuffer); if (*phShmHandle == 0) *phShmHandle = OpenFileMapping(FILE_MAP_WRITE, TRUE, szName); // check if a shared memory exists in any active user session if (*phShmHandle == 0) { /* walk over all sessions to check if the shared memory is opened somewhere */ COPIED_WTS_SESSION_INFO* pResult; DWORD dwCount; DWORD i; WTSEnumerateSessions(0 /* current machine */, 0, 1, (WTS_SESSION_INFO**)&pResult, &dwCount); for (i = 0; i < dwCount; ++i) { char szTemp[15]; strcpy(szNameBuffer, szSessionPrefix); strcat(szNameBuffer, _itoa((int) pResult[i].SessionId, szTemp, 10)); strcat(szNameBuffer, "\\"); strcat(szNameBuffer, szName); *phShmHandle = OpenFileMapping(FILE_MAP_WRITE, TRUE, szNameBuffer); if (*phShmHandle != 0) break; } WTSFreeMemory(pResult); } if (*phShmHandle == 0) { #if 0 //See examlpe above how to obtain a security descriptor. SECURITY_ATTRIBUTES sa; SECURITY_ATTRIBUTES* psa = NULL; if (s_pSecurityDescriptorData) { sa.nLength = sizeof(sa); sa.bInheritHandle = FALSE; sa.lpSecurityDescriptor = &s_SecurityDescriptor; psa = &sa; } #endif //*phShmHandle = CreateFileMapping((HANDLE)0xffffffff, psa, PAGE_READWRITE, 0, ulSize, szName); *phShmHandle = CreateFileMapping((HANDLE)0xffffffff, NULL, PAGE_READWRITE, 0, ulSize, szName); } if (*phShmHandle == 0) { return FALSE; } *ppUserSpace = MapViewOfFile((HANDLE)*phShmHandle, FILE_MAP_WRITE, 0, 0, 0); if (*ppUserSpace == NULL) { return FALSE; } hProcess = GetCurrentProcess(); GetProcessWorkingSetSize(hProcess, (PSIZE_T)&lMinimumWorkingSetSize, (PSIZE_T)&lMaximumWorkingSetSize); if (lMinimumWorkingSetSize <= ulSize) { lMinimumWorkingSetSize = ulSize + 0x10000UL; if(lMaximumWorkingSetSize <= lMinimumWorkingSetSize) { lMaximumWorkingSetSize = lMinimumWorkingSetSize + 0x1000000UL; } if (!SetProcessWorkingSetSize(hProcess, lMinimumWorkingSetSize, lMaximumWorkingSetSize)) { DWORD dwError = GetLastError(); /* return 0; When this call produces an error, everything still seems to work, but returning 0 will lead to a crash of Targetvisu.*/ } } /* Lock memory mapped file */ if (!VirtualLock((LPVOID)*ppUserSpace, ulSize)) { DWORD dwError = GetLastError(); /* TOCHECK: Don't know why locking does not work for SHM larger than 1MB */ /*return 0;*/ } return TRUE; } struct DataExchangeStruct { unsigned long ulSync; INT32 a_i[1000]; }; int main(int argc, char* argv[]) { HANDLE hShmRead, hShmWrite; DataExchangeStruct* pDESRead = NULL; DataExchangeStruct* pDESWrite = NULL; int i; int iStart; int iNums = (int)(sizeof((*pDESRead).a_i) / sizeof((*pDESRead).a_i[0])); int bDoPrintf; unsigned char bWasSet; printf("Opening Shared Memory _CODESYS_SharedMemoryTest_Write (means a memory mapped file)...\n"); LocalSysCreateNamedShm("_CODESYS_SharedMemoryTest_Read",sizeof(DataExchangeStruct),&hShmRead,(void**)&pDESRead); printf("ShmPointer: 0x%p, handle: 0x%p \n",pDESRead,pDESRead); printf("Opening Shared Memory _CODESYS_SharedMemoryTest_Read (means a memory mapped file)...\n"); LocalSysCreateNamedShm("_CODESYS_SharedMemoryTest_Write",sizeof(DataExchangeStruct),&hShmWrite,(void**)&pDESWrite); printf("ShmPointer: 0x%p, handle: 0x%p \n",pDESWrite,hShmWrite); if(pDESRead == NULL || pDESWrite == NULL) { printf("Could not Created SHM!\r\n"); return 1; } while(!_kbhit()) { //for this check, we read and write from/to both shared memories. bDoPrintf = 1; iStart = pDESRead->a_i[0]; //Synchronization. /* bWasSet = _interlockedbittestandset((long*)&pDESRead->ulSync, 0); //We use bit 0 as synchronization marker. We know IEC-application is using the same. if(bWasSet) { printf("Synchronization conflict, skipping access.\r\n"); Sleep(2); continue; } */ for (i = 0; i < iNums; i++) { //Check all elements for correct increment. if (pDESRead->a_i[i] != 0 && pDESRead->a_i[i] != (iStart + i)) { if(bDoPrintf) printf("Array element not correctly incremented: R Index %d, value: %d\r\n", i, pDESRead->a_i[i]); bDoPrintf = 0; //Correct the element. pDESRead->a_i[i] = iStart + i; } } iStart = pDESWrite->a_i[0]; for (i = 0; i < iNums; i++) { //Check all elements for correct increment. if (pDESWrite->a_i[i] != 0 && pDESWrite->a_i[i] != (iStart + i)) { if (bDoPrintf) printf("Array element not correctly incremented: W Index %d, value: %d\r\n", i, pDESWrite->a_i[i]); bDoPrintf = 0; //Correct the element. pDESWrite->a_i[i] = iStart + i; } } //Now increment the array elements. iStart = pDESRead->a_i[0]; for (i = 0; i < iNums; i++) { pDESRead->a_i[i] = iStart + i + 1; } iStart = pDESWrite->a_i[0]; for (i = 0; i < iNums; i++) { pDESWrite->a_i[i] = iStart + i + 1; } //Now check all the elements again. iStart = pDESRead->a_i[0]; for (i = 0; i < iNums; i++) { //Check all elements for correct increment. if (pDESRead->a_i[i] != 0 && pDESRead->a_i[i] != (iStart + i)) { if (bDoPrintf) printf("Array element not correctly incremented: R Index %d, value: %d\r\n", i, pDESRead->a_i[i]); bDoPrintf = 0; //Correct the element. pDESRead->a_i[i] = iStart + i; } } iStart = pDESWrite->a_i[0]; for (i = 0; i < iNums; i++) { //Check all elements for correct increment. if (pDESWrite->a_i[i] != 0 && pDESWrite->a_i[i] != (iStart + i)) { if (bDoPrintf) printf("Array element not correctly incremented: W Index %d, value: %d\r\n", i, pDESWrite->a_i[i]); bDoPrintf = 0; //Correct the element. pDESWrite->a_i[i] = iStart + i; } } //Reset synchronization to give a chance for access to the other application. pDESRead->ulSync = 0; //Just sleep, to not block the CPU completely. Sleep(2); } return 0; } |