void CGuidGenDlg::OnOK() { UpdateData(TRUE); // save current type selected AfxGetApp()->WriteProfileInt(szOptions, szGuidType, m_nGuidType); // copy the string to the clipboard if (!OpenClipboard()) { AfxMessageBox(IDP_ERR_OPEN_CLIP); return; } CString strResult; GetFormattedGuid(strResult); int nTextLen = (strResult.GetLength()+1)*sizeof(TCHAR); HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nTextLen); if (hGlobal != NULL) { LPVOID lpText = GlobalLock(hGlobal); memcpy_s(lpText, nTextLen, strResult, nTextLen); EmptyClipboard(); GlobalUnlock(hGlobal); #ifdef _UNICODE SetClipboardData(CF_UNICODETEXT, hGlobal); #else SetClipboardData(CF_TEXT, hGlobal); #endif } CloseClipboard(); } void CGuidGenDlg::GetFormattedGuid(CString& rString) { // load appropriate formatting string CString strFormat; ENSURE(strFormat.LoadString(IDS_FORMATS+m_nGuidType)); // then format into destination rString.Format(strFormat, // first copy... m_guid.Data1, m_guid.Data2, m_guid.Data3, m_guid.Data4[0], m_guid.Data4[1], m_guid.Data4[2], m_guid.Data4[3], m_guid.Data4[4], m_guid.Data4[5], m_guid.Data4[6], m_guid.Data4[7], // second copy... m_guid.Data1, m_guid.Data2, m_guid.Data3, m_guid.Data4[0], m_guid.Data4[1], m_guid.Data4[2], m_guid.Data4[3], m_guid.Data4[4], m_guid.Data4[5], m_guid.Data4[6], m_guid.Data4[7]); } void CGuidGenDlg::OnNewguid() { // create random GUID using UuidCreate so that we // can check for more error codes m_guid = GUID_NULL; HRESULT hr = ::UuidCreate(&m_guid); // Errors if (HRESULT_CODE(hr) != RPC_S_OK) { AfxMessageBox(IDP_ERR_NOT_OK); return; } if (m_guid == GUID_NULL) { AfxMessageBox(IDP_ERR_CREATE_GUID); return; } // Warnings if (HRESULT_CODE(hr) == RPC_S_UUID_NO_ADDRESS) AfxMessageBox(IDP_ERR_NO_ADDRESS); if (HRESULT_CODE(hr) == RPC_S_UUID_LOCAL_ONLY) AfxMessageBox(IDP_ERR_LOCAL_ONLY); // update dialog to match m_guid OnChangedSel(IDC_RADIO1+m_nGuidType); }