string uuid() { string result; #ifdef _WIN32 #ifndef _UNICODE UUID uid; auto status = UuidCreate(&uid); ASSERT(status == RPC_S_OK, "Failed to create UUID"); char* str; auto res = UuidToStringA(&uid, reinterpret_cast<RPC_CSTR*>(&str)); ASSERT(res == RPC_S_OK, "Failed to create string from UUID"); result = reinterpret_cast<const char*>(str); RpcStringFreeA(reinterpret_cast<RPC_CSTR*>(&str)); #else UUID uid; auto status = UuidCreate(&uid); ASSERT(status == RPC_S_OK, "Failed to create UUID"); wchar_t* str; auto res = UuidToStringW(&uid, reinterpret_cast<RPC_WSTR*>(&str)); ASSERT(res == RPC_S_OK, "Failed to create string from UUID"); wstring wideRes = reinterpret_cast<const wchar_t*>(str); result = engine::toUtf8String(wideRes); RpcStringFreeW(reinterpret_cast<RPC_WSTR*>(&str)); #endif #endif return result; }
static PyObject* uuidcreate(PyObject* obj, PyObject*args) { UUID result; wchar_t *cresult; PyObject *oresult; /* May return ok, local only, and no address. For local only, the documentation says we still get a uuid. For RPC_S_UUID_NO_ADDRESS, it's not clear whether we can use the result. */ if (UuidCreate(&result) == RPC_S_UUID_NO_ADDRESS) { PyErr_SetString(PyExc_NotImplementedError, "processing 'no address' result"); return NULL; } if (UuidToStringW(&result, &cresult) == RPC_S_OUT_OF_MEMORY) { PyErr_SetString(PyExc_MemoryError, "out of memory in uuidgen"); return NULL; } oresult = PyUnicode_FromWideChar(cresult, wcslen(cresult)); RpcStringFreeW(&cresult); return oresult; }
/* * implemented */ DWORD WINAPI pSetupStringFromGuid(LPGUID lpGUID, PWSTR pString, DWORD dwStringLen) { RPC_STATUS Status; RPC_WSTR rpcBuffer; WCHAR szBuffer[39]; if (dwStringLen < 39) { return ERROR_INSUFFICIENT_BUFFER; } Status = UuidToStringW(lpGUID, &rpcBuffer); if (Status != RPC_S_OK) { return Status; } wcscpy(szBuffer, L"{"); wcscat(szBuffer, rpcBuffer); wcscat(szBuffer, L"}"); wcscpy(pString, szBuffer); RpcStringFreeW(&rpcBuffer); return NO_ERROR; }
/* 010509 Carl Corcoran */ HRESULT CCString::FromGuid(GUID* pGuid) { WCHAR* wsz; UuidToStringW(pGuid, &wsz); this->_CreateFromW(wsz); RpcStringFreeW(&wsz); return S_OK; }
void ff_arch_misc_create_guid_cstr(const wchar_t **guid_cstr, int *guid_cstr_len) { UUID guid; RPC_STATUS status; RPC_WSTR guid_str; size_t guid_str_len; status = UuidCreate(&guid); ff_winapi_fatal_error_check(status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY, L"UuidCreate() failed"); status = UuidToStringW(&guid, &guid_str); ff_winapi_fatal_error_check(status == RPC_S_OK, L"UuidToStringW() failed"); guid_str_len = wcslen((wchar_t *) guid_str); ff_assert(guid_str_len > 0); *guid_cstr = (const wchar_t *) guid_str; *guid_cstr_len = (int) guid_str_len; ff_assert(*guid_cstr_len > 0); }
DWORD WINAPI NetClassInstaller( IN DI_FUNCTION InstallFunction, IN HDEVINFO DeviceInfoSet, IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL) { SP_DRVINFO_DATA_W DriverInfoData; SP_DRVINFO_DETAIL_DATA_W DriverInfoDetail; WCHAR SectionName[LINE_LEN]; HINF hInf = INVALID_HANDLE_VALUE; INFCONTEXT InfContext; UINT ErrorLine; INT CharacteristicsInt; DWORD Characteristics; LPWSTR BusType = NULL; RPC_STATUS RpcStatus; UUID Uuid; LPWSTR UuidRpcString = NULL; LPWSTR UuidString = NULL; LONG rc; DWORD dwLength; if (InstallFunction != DIF_INSTALLDEVICE) return ERROR_DI_DO_DEFAULT; DPRINT("%lu %p %p\n", InstallFunction, DeviceInfoSet, DeviceInfoData); /* Get driver info details */ DriverInfoData.cbSize = sizeof(SP_DRVINFO_DATA_W); if (!SetupDiGetSelectedDriverW(DeviceInfoSet, DeviceInfoData, &DriverInfoData)) { rc = GetLastError(); DPRINT("SetupDiGetSelectedDriverW() failed with error 0x%lx\n", rc); goto cleanup; } DriverInfoDetail.cbSize = sizeof(SP_DRVINFO_DETAIL_DATA_W); if (!SetupDiGetDriverInfoDetailW(DeviceInfoSet, DeviceInfoData, &DriverInfoData, &DriverInfoDetail, sizeof(DriverInfoDetail), NULL) && GetLastError() != ERROR_INSUFFICIENT_BUFFER) { rc = GetLastError(); DPRINT("SetupDiGetDriverInfoDetailW() failed with error 0x%lx\n", rc); goto cleanup; } hInf = SetupOpenInfFileW(DriverInfoDetail.InfFileName, NULL, INF_STYLE_WIN4, &ErrorLine); if (hInf == INVALID_HANDLE_VALUE) { rc = GetLastError(); DPRINT("SetupOpenInfFileW() failed with error 0x%lx\n", rc); goto cleanup; } if (!SetupDiGetActualSectionToInstallW(hInf, DriverInfoDetail.SectionName, SectionName, LINE_LEN, NULL, NULL)) { rc = GetLastError(); DPRINT("SetupDiGetActualSectionToInstallW() failed with error 0x%lx\n", rc); goto cleanup; } /* Get Characteristics and BusType (optional) from .inf file */ if (!SetupFindFirstLineW(hInf, SectionName, L"Characteristics", &InfContext)) { rc = GetLastError(); DPRINT("Unable to find key %S in section %S of file %S (error 0x%lx)\n", L"Characteristics", SectionName, DriverInfoDetail.InfFileName, rc); goto cleanup; } if (!SetupGetIntField(&InfContext, 1, &CharacteristicsInt)) { rc = GetLastError(); DPRINT("SetupGetIntField() failed with error 0x%lx\n", rc); goto cleanup; } Characteristics = (DWORD)CharacteristicsInt; if (IsEqualIID(&DeviceInfoData->ClassGuid, &GUID_DEVCLASS_NET)) { if (SetupFindFirstLineW(hInf, SectionName, L"BusType", &InfContext)) { if (!SetupGetStringFieldW(&InfContext, 1, NULL, 0, &dwLength)) { rc = GetLastError(); DPRINT("SetupGetStringFieldW() failed with error 0x%lx\n", rc); goto cleanup; } BusType = HeapAlloc(GetProcessHeap(), 0, dwLength * sizeof(WCHAR)); if (!BusType) { DPRINT("HeapAlloc() failed\n"); rc = ERROR_NOT_ENOUGH_MEMORY; goto cleanup; } if (!SetupGetStringFieldW(&InfContext, 1, BusType, dwLength, NULL)) { rc = GetLastError(); DPRINT("SetupGetStringFieldW() failed with error 0x%lx\n", rc); goto cleanup; } } } /* Create a new UUID */ RpcStatus = UuidCreate(&Uuid); if (RpcStatus != RPC_S_OK && RpcStatus != RPC_S_UUID_LOCAL_ONLY) { DPRINT("UuidCreate() failed with RPC status 0x%lx\n", RpcStatus); rc = ERROR_GEN_FAILURE; goto cleanup; } RpcStatus = UuidToStringW(&Uuid, &UuidRpcString); if (RpcStatus != RPC_S_OK) { DPRINT("UuidToStringW() failed with RPC status 0x%lx\n", RpcStatus); rc = ERROR_GEN_FAILURE; goto cleanup; } /* Add curly braces around Uuid */ UuidString = HeapAlloc(GetProcessHeap(), 0, (2 + wcslen(UuidRpcString)) * sizeof(WCHAR) + sizeof(UNICODE_NULL)); if (!UuidString) { DPRINT("HeapAlloc() failed\n"); rc = ERROR_NOT_ENOUGH_MEMORY; goto cleanup; } wcscpy(UuidString, L"{"); wcscat(UuidString, UuidRpcString); wcscat(UuidString, L"}"); if (IsEqualIID(&DeviceInfoData->ClassGuid, &GUID_DEVCLASS_NET)) rc = InstallNetDevice(DeviceInfoSet, DeviceInfoData, UuidString, Characteristics, BusType); else if (IsEqualIID(&DeviceInfoData->ClassGuid, &GUID_DEVCLASS_NETCLIENT)) rc = InstallNetClient(); else if (IsEqualIID(&DeviceInfoData->ClassGuid, &GUID_DEVCLASS_NETSERVICE)) rc = InstallNetService(); else if (IsEqualIID(&DeviceInfoData->ClassGuid, &GUID_DEVCLASS_NETTRANS)) rc = InstallNetTransport(); else { DPRINT("Invalid class guid\n"); rc = ERROR_GEN_FAILURE; } cleanup: if (hInf != INVALID_HANDLE_VALUE) SetupCloseInfFile(hInf); if (UuidRpcString != NULL) RpcStringFreeW(&UuidRpcString); HeapFree(GetProcessHeap(), 0, BusType); HeapFree(GetProcessHeap(), 0, UuidString); if (rc == ERROR_SUCCESS) rc = ERROR_DI_DO_DEFAULT; DPRINT("Returning 0x%lx\n", rc); return rc; }
/****************************************************************************++ Routine Description: Creates a handle to the CSP Arguments: pwzContainerName - name of the container to be created. if NULL, GUID is generated for the name of the container fCreateNewKeys - forces new keys to be created phCryptProv - pointer to the location, where handle should be returned Notes: - Return Value: - S_OK - or - - CAPI error returned by CryptAcquireContextW --*****************************************************************************/ HRESULT CreateCryptProv( IN PCWSTR pwzContainerName, IN BOOL fCreateNewKeys, OUT HCRYPTPROV* phCryptProv) { HRESULT hr = S_OK; HCRYPTKEY hKey = NULL; RPC_STATUS status = RPC_S_OK; BOOL fCreatedContainer = FALSE; WCHAR* pwzNewContainerName = NULL; *phCryptProv = NULL; if (NULL == pwzContainerName) { UUID uuid; BOOL fServiceAccount = FALSE; // // generate container name from the UUID // status = UuidCreate(&uuid); hr = HRESULT_FROM_RPCSTATUS(status); if (FAILED(hr)) { goto Cleanup; } status = UuidToStringW(&uuid, (unsigned short**)&pwzNewContainerName); hr = HRESULT_FROM_RPCSTATUS(status); if (FAILED(hr)) { goto Cleanup; } pwzContainerName = pwzNewContainerName; hr = IsServiceAccount(&fServiceAccount); if (FAILED(hr)) { goto Cleanup; } // // open the clean key container // // note: CRYPT_NEW_KEYSET is not creating new keys, it just // creates new key container. duh. // if (!CryptAcquireContextW(phCryptProv, pwzNewContainerName, NULL, // default provider name DEFAULT_PROV_TYPE, fServiceAccount ? (CRYPT_SILENT | CRYPT_NEWKEYSET | CRYPT_MACHINE_KEYSET) : (CRYPT_SILENT | CRYPT_NEWKEYSET))) { hr = HRESULT_FROM_WIN32(GetLastError()); // // we are seeing that CryptAcquireContextW returns NTE_FAIL under low // memory condition, so we just mask the error // if (NTE_FAIL == hr) { hr = E_OUTOFMEMORY; } goto Cleanup; } fCreatedContainer = TRUE; } else { BOOL fServiceAccount = FALSE; hr = IsServiceAccount(&fServiceAccount); if (FAILED(hr)) { goto Cleanup; } // // open the provider first, create the keys too // if (!CryptAcquireContextW(phCryptProv, pwzContainerName, NULL, // default provider name DEFAULT_PROV_TYPE, fServiceAccount ? (CRYPT_SILENT | CRYPT_MACHINE_KEYSET) : (CRYPT_SILENT))) { hr = HRESULT_FROM_WIN32(GetLastError()); // // we are seeing that CryptAcquireContextW returns NTE_FAIL under low // memory condition, so we just mask the error // if (NTE_FAIL == hr) { hr = E_OUTOFMEMORY; } goto Cleanup; } } if (fCreateNewKeys) { // // make sure keys exist // if (!CryptGetUserKey(*phCryptProv, DEFAULT_KEY_SPEC, &hKey)) { hr = HRESULT_FROM_WIN32(GetLastError()); // if key does not exist, create it if (HRESULT_FROM_WIN32((unsigned long)NTE_NO_KEY) == hr) { hr = S_OK; if (!CryptGenKey(*phCryptProv, DEFAULT_KEY_SPEC, CRYPT_EXPORTABLE, &hKey)) { hr = HRESULT_FROM_WIN32(GetLastError()); // // we are seeing that CryptGenKey returns ERROR_CANTOPEN under low // memory condition, so we just mask the error // if (HRESULT_FROM_WIN32(ERROR_CANTOPEN) == hr) { hr = E_OUTOFMEMORY; } goto Cleanup; } } else { // failed to get user key by some misterious reason, so bail out goto Cleanup; } } } Cleanup: DestroyKey(hKey); if (FAILED(hr)) { // // release the context // ReleaseCryptProv(*phCryptProv); *phCryptProv = NULL; // // delete the keys, if we created them // if (fCreatedContainer) { DeleteKeys(pwzContainerName); } } if (NULL != pwzNewContainerName) { // this always returns RPC_S_OK status = RpcStringFreeW((unsigned short**)&pwzNewContainerName); USES(status); } return hr; }
static void UuidConversionAndComparison(void) { CHAR strx[100], x; LPSTR str = strx; WCHAR wstrx[100], wx; LPWSTR wstr = wstrx; UUID Uuid1, Uuid2, *PUuid1, *PUuid2; RPC_STATUS rslt; int i1,i2; /* Uuid Equality */ for (i1 = 0; i1 < 11; i1++) for (i2 = 0; i2 < 11; i2++) { if (i1 < 10) { Uuid1 = Uuid_Table[i1]; PUuid1 = &Uuid1; } else { PUuid1 = NULL; } if (i2 < 10) { Uuid2 = Uuid_Table[i2]; PUuid2 = &Uuid2; } else { PUuid2 = NULL; } ok( (UuidEqual(PUuid1, PUuid2, &rslt) == Uuid_Comparison_Grid[i1][i2]), "UUID Equality\n" ); } /* Uuid to String to Uuid (char) */ for (i1 = 0; i1 < 10; i1++) { Uuid1 = Uuid_Table[i1]; ok( (UuidToStringA(&Uuid1, (unsigned char**)&str) == RPC_S_OK), "Simple UUID->String copy\n" ); ok( (UuidFromStringA((unsigned char*)str, &Uuid2) == RPC_S_OK), "Simple String->UUID copy from generated UUID String\n" ); ok( UuidEqual(&Uuid1, &Uuid2, &rslt), "Uuid -> String -> Uuid transform\n" ); /* invalid uuid tests -- size of valid UUID string=36 */ for (i2 = 0; i2 < 36; i2++) { x = str[i2]; str[i2] = 'g'; /* whatever, but "g" is a good boundary condition */ ok( (UuidFromStringA((unsigned char*)str, &Uuid1) == RPC_S_INVALID_STRING_UUID), "Invalid UUID String\n" ); str[i2] = x; /* change it back so remaining tests are interesting. */ } RpcStringFreeA((unsigned char **)&str); } /* Uuid to String to Uuid (wchar) */ for (i1 = 0; i1 < 10; i1++) { Uuid1 = Uuid_Table[i1]; rslt=UuidToStringW(&Uuid1, &wstr); ok( (rslt == RPC_S_OK), "Simple UUID->WString copy\n" ); ok( (UuidFromStringW(wstr, &Uuid2) == RPC_S_OK), "Simple WString->UUID copy from generated UUID String\n" ); ok( UuidEqual(&Uuid1, &Uuid2, &rslt), "Uuid -> WString -> Uuid transform\n" ); /* invalid uuid tests -- size of valid UUID string=36 */ for (i2 = 0; i2 < 36; i2++) { wx = wstr[i2]; wstr[i2] = 'g'; /* whatever, but "g" is a good boundary condition */ ok( (UuidFromStringW(wstr, &Uuid1) == RPC_S_INVALID_STRING_UUID), "Invalid UUID WString\n" ); wstr[i2] = wx; /* change it back so remaining tests are interesting. */ } RpcStringFreeW(&wstr); } }