vs2010 c++ GUID获取代码实现

来源:互联网 发布:程序员工资条 编辑:程序博客网 时间:2024/06/04 19:13
void CGuidGenDlg::OnOK(){UpdateData(TRUE);// save current type selectedAfxGetApp()->WriteProfileInt(szOptions, szGuidType, m_nGuidType);// copy the string to the clipboardif (!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 _UNICODESetClipboardData(CF_UNICODETEXT, hGlobal);#elseSetClipboardData(CF_TEXT, hGlobal);#endif}CloseClipboard();}void CGuidGenDlg::GetFormattedGuid(CString& rString){// load appropriate formatting stringCString strFormat;ENSURE(strFormat.LoadString(IDS_FORMATS+m_nGuidType));// then format into destinationrString.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 codesm_guid = GUID_NULL;HRESULT hr = ::UuidCreate(&m_guid);// Errorsif (HRESULT_CODE(hr) != RPC_S_OK){AfxMessageBox(IDP_ERR_NOT_OK);return;}if (m_guid == GUID_NULL){AfxMessageBox(IDP_ERR_CREATE_GUID);return;}// Warningsif (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_guidOnChangedSel(IDC_RADIO1+m_nGuidType);}

原创粉丝点击