1. Check MAC Address and other machine information
// Link with Iphlpapi.lib
#pragma comment(lib, "IPHLPAPI.lib")
#include <winsock2.h>
#include <iphlpapi.h>
//check MAC Address
ULONG outBufLen = 0;
outBufLen = 15000;
DWORD dwRetVal = 0;
PIP_ADAPTER_ADDRESSES pAddresses = NULL;
pAddresses = new IP_ADAPTER_ADDRESSES[outBufLen];
dwRetVal = GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &outBufLen);
if(dwRetVal == NO_ERROR){
long buffer=0;
long a = 0;
long b = 0;
m_strMACAddress = _T("");
for (int i =0; i < (int)pAddresses->PhysicalAddressLength; i++)
{
buffer = (long) pAddresses->PhysicalAddress[i];
a = buffer/16;
b = buffer%16;
CString temp = _T("");
DEC_TO_HEX(a,m_strMACAddress,temp);
DEC_TO_HEX(b,m_strMACAddress,temp);
if(i != (int)pAddresses->PhysicalAddressLength-1)
m_strMACAddress += _T("-");
}
}
if(pAddresses)
delete pAddresses;
#define DEC_TO_HEX(Dec,MAC,Temp) \
{\
<span style="white-space:pre"> </span>switch(Dec){ \
case 10: MAC += _T("A"); break; \
case 11: MAC += _T("B"); break; \
case 12: MAC += _T("C"); break; \
case 13: MAC += _T("D"); break; \
case 14: MAC += _T("E"); break; \
case 15: MAC += _T("F"); break; \
default: \
Temp.Format(_T("%ld"), Dec); \
MAC += Temp; break; \
<span style="white-space:pre"> </span>} \
}
//get machine name
TCHAR nameBuffer[100];
DWORD nameBufferSize = 100;
if(GetComputerName(nameBuffer, &nameBufferSize) == TRUE){
m_strMachineName = _T("");
int q=0;
while(nameBuffer[q] != '\0'){
m_strMachineName += nameBuffer[q];
q++;
}
}
2. Open up a new dialog
CXXXXXXXXXXDlg aDlg;
aDlg.m_nIssueID = m_nIssueID;
aDlg.m_nProjectID = m_nProjectID;
aDlg.m_nSuiteID = m_nSuiteID;
aDlg.m_strProjectName = m_strSuiteProject;
INT_PTR nRet = -1;
nRet = aDlg.DoModal();
if(nRet == IDCANCEL)
return;
else if(nRet == IDOK)
Reload();
3. CComboBox
//Get comboBox id
m_nID = m_cbNames.GetItemData(m_cbNames.GetCurSel());
//look for certain text, set it the current in comboBox
for(int i=0; i<m_cbBox.GetCount(); i++)
{
CString strTemp;
m_cbBox.GetLBText(i,strTemp);
if(strTemp == m_str)
{
m_cbBox.SetCurSel(i);
break;
}
}
HINSTANCE hInstance ;
hInstance = ShellExecute(((CMainFrame*)AfxGetMainWnd())->m_hWnd, _T("open"), strFileName, NULL, NULL, SW_SHOW);
if ((int)hInstance == 31)
{
CString str = "shell32.dll,OpenAs_RunDLL ";
str += strFileName;
ShellExecute(((CMainFrame*)AfxGetMainWnd())->m_hWnd, _T("open"), _T("rundll32.exe"), str, NULL, SW_SHOW);
}
5. Open up ODBC database with MS SQL Server
CDB db;
if(!db.m_pDB)<span style="white-space:pre"> </span>// m_pDB is from CDatabase
{
AfxMessageBox(db.m_strError);
return;
}
<span style="white-space:pre"> </span>CXXXXXXSet aSet(db.m_pDB);
aSet.m_strFilter.Format(_T("ID = %d"), m_nID);
<span style="white-space:pre"> </span>int _bdttexception = FALSE;
CString _strExceptionMsg;
try
<span style="white-space:pre"> </span>{
aSet.Open();
while(!aSet.IsEOF())
{
<span style="white-space:pre"> </span>// do something .Edit() and then .Update() or .Add()
aSet.MoveNext();
}
aSet.Close();
}
catch(CDBException * pe1)
{
_bdttexception = TRUE;
_strExceptionMsg = pe1->m_strError;
Write_Exception_Log(__FILE__, __LINE__, pe1->m_strError, 1);
pe1->Delete();
}
// Common file
<span style="white-space:pre"> </span>ifstream slnFile(strSuitePath);
string line;
string s = "";
string s1 = "= \"";
string s2 = "\",";
string s3 = "\", \"";
string s4 = "\\";
string s5 = ".";
string::size_type pos1,pos2,pos3,pos4,pos5;
while(getline(slnFile,line)){
string leftPrjPath, rightPrjPath;
pos1 = line.find(s1);
pos2 = line.find(s2);
pos3 = line.find(s3);
pos4 = line.find_last_of(s4);
pos5 = line.find_last_of(s5);
if(pos1 != string::npos && pos4 != string::npos){
string subPrjName = line.substr(pos1+3,pos2-pos1-3);
int index = m_strSuitePath.ReverseFind('\\');
CString temp_strSuitePath = m_strSuitePath.Left(index);
CT2CA temp(temp_strSuitePath);<span style="white-space:pre"> </span>//CString to String
leftPrjPath = temp;
rightPrjPath = line.substr(pos3+4, pos5-pos3-4);
//process left and right path if necessary
while( rightPrjPath.find('..\\') == 2)
{
CString temp_leftPath = leftPrjPath.c_str();<span style="white-space:pre"> </span>//String to CString
index = temp_leftPath.ReverseFind('\\');
temp_strSuitePath = temp_leftPath.Left(index);
CT2CA temp2(temp_strSuitePath);
leftPrjPath = temp2;
rightPrjPath = rightPrjPath.substr(rightPrjPath.find('..\\')+1);
}
string stsubPrjPath = leftPrjPath + "\\" + rightPrjPath + ".exe";
ifstream suiteFile(stsubPrjPath.c_str());
if(suiteFile.is_open())
{
prjlist.push_back(subPrjName.c_str());
m_mapProjects.insert(pair<CString,CString>(subPrjName.c_str(), stsubPrjPath.c_str()));
}
}
}
// .XML file
HTREEITEM hRoot1;
HTREEITEM hNode;
MSXML2::IXMLDOMDocumentPtr m_pDoc;
CString strErrMsg;
HRESULT hr = m_pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
if (FAILED(hr))
{
strErrMsg.Format(_T("Create DOM failed. Please install MSXML parser version 4.0."));
AfxMessageBox(strErrMsg);
}
m_pDoc->validateOnParse = VARIANT_FALSE;
m_pDoc->async = VARIANT_FALSE;
m_pDoc->preserveWhiteSpace = VARIANT_TRUE;
if(VARIANT_FALSE == m_pDoc->load((LPCTSTR)strPrjPath))<span style="white-space:pre"> </span>// load .XML file
{
strErrMsg.Format(_T("Load application configuration file failed."));
AfxMessageBox(strErrMsg);
return;
}
MSXML2::IXMLDOMElementPtr pFileRoot = m_pDoc->selectSingleNode(_T("testsuite"));
if (pFileRoot == NULL)
{
strErrMsg.Format(_T("Read configuration info failed. Please make sure it's a valid configuration file."));
AfxMessageBox(strErrMsg);
return;
}
CString RootSuiteName;
RootSuiteName.Format(_T("%s"),(LPCTSTR)(_bstr_t)(pFileRoot->getAttribute("name")));<span style="white-space:pre"> </span>// _variant_t to CString
hRoot1 = m_RanorexTreeCtrl.InsertItem(RootSuiteName, 0, 0);
MSXML2::IXMLDOMNodePtr pNodeRoot = pFileRoot->selectSingleNode(_T("./content"));
MSXML2::IXMLDOMNodePtr pConfigRoot = pFileRoot->selectSingleNode(_T("./configurations"));
// parse .XML tree
<span style="white-space:pre"> </span>void CXXXXXPg::ParseTestCaseSubTree(MSXML2::IXMLDOMNodePtr pRoot, CTreeCtrl& tcTreeCtrl, HTREEITEM& hRoot)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>MSXML2::IXMLDOMNodeListPtr pNodeList = pRoot->GetchildNodes();
<span style="white-space:pre"> </span>int i;
<span style="white-space:pre"> </span>for(i = 1;i < pNodeList->length;i++)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span> MSXML2::IXMLDOMElementPtr pNode = pNodeList->item[i];
<span style="white-space:pre"> </span> if(pNode){
<span style="white-space:pre"> </span>CString strName, strID;
<span style="white-space:pre"> </span>GET_ATTRIBUTE(pNode, _T("name"), strName, _T("Unknown"));
<span style="white-space:pre"> </span>GET_ATTRIBUTE(pNode, _T("id"), strID, _T("Unknown"));
<span style="white-space:pre"> </span> if(strName)
<span style="white-space:pre"> </span> m_mapCaseList.insert(pair<CString,CString>(strID,strName));
<span style="white-space:pre"> </span>hNode = tcTreeCtrl.InsertItem((LPCTSTR)(_bstr_t)(pNode->getAttribute("name")), 0, 0, hRoot, TVI_LAST);
<span style="white-space:pre"> </span>if(pNode->GettagName() == (_bstr_t)("folder"))
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span> hRoot = hNode;
<span style="white-space:pre"> </span> ParseTestCaseSubTree(pNode, tcTreeCtrl, hRoot);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span> else
<span style="white-space:pre"> </span> continue;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>#define GET_ATTRIBUTE(Element, Name, Result, DefaultValue) \
<span style="white-space:pre"> </span>{\
<span style="white-space:pre"> </span>_variant_t var = Element->getAttribute(Name);\
<span style="white-space:pre"> </span>if(var.vt != VT_NULL)\
<span style="white-space:pre"> </span>Result = (LPCTSTR)(_bstr_t)var;\
<span style="white-space:pre"> </span>else\
<span style="white-space:pre"> </span>Result = DefaultValue;\
<span style="white-space:pre"> </span>}
7. CTreeCtrl consistent checkbox
To check a node: check all its child nodes, iterate its sibling nodes, if all of them are checked, check its parent node and do this examination start from its parent node.
To uncheck a node: uncheck all its child node, iterate its parent node, if it is checked, then uncheck it and do this to its parent until the state is unchecked
void CXXXPg::OnNMClickTree1Ranorex(NMHDR *pNMHDR, LRESULT *pResult)
{
CPoint oPoint;
UINT nFlag;
GetCursorPos(&oPoint);
m_TreeCtrl.ScreenToClient(&oPoint);
HTREEITEM oSelectItem = m_TreeCtrl.HitTest(oPoint,&nFlag);
if(oSelectItem == NULL) return;
m_TreeCtrl.SelectItem(oSelectItem);
if(nFlag & TVHT_ONITEMSTATEICON)
{
BOOL bChecked = !m_TreeCtrl.GetCheck(oSelectItem);
m_TreeCtrl.SetCheck(oSelectItem,bChecked);
ConsistentChildCheck(oSelectItem);
ConsistentParentCheck(oSelectItem);
m_TreeCtrl.SetCheck(oSelectItem,!bChecked);
}
*pResult = 0;
}
void CXXXPg::ConsistentChildCheck(HTREEITEM hTreeItem)
{
BOOL bChecked = m_TreeCtrl.GetCheck(hTreeItem);
if(m_TreeCtrl.ItemHasChildren(hTreeItem))
{
HTREEITEM hChildItem = m_TreeCtrl.GetChildItem(hTreeItem);
while(hChildItem != NULL)
{
m_TreeCtrl.SetCheck(hChildItem, bChecked);
ConsistentChildCheck(hChildItem);
hChildItem = m_TreeCtrl.GetNextItem(hChildItem, TVGN_NEXT);
}
}
}
void CXXXPg::ConsistentParentCheck(HTREEITEM hTreeItem)
{
HTREEITEM hParentItem = m_TreeCtrl.GetParentItem(hTreeItem);
if(hParentItem != NULL)
{
HTREEITEM hChildItem = m_TreeCtrl.GetChildItem(hParentItem);
while(hChildItem != NULL)
{
if(m_TreeCtrl.GetCheck(hChildItem) == FALSE)
{
m_TreeCtrl.SetCheck(hParentItem,FALSE);
return ConsistentParentCheck(hParentItem);
}
hChildItem = m_TreeCtrl.GetNextItem(hChildItem, TVGN_NEXT);
}
m_TreeCtrl.SetCheck(hParentItem, TRUE);
return ConsistentParentCheck(hParentItem);
}
}
8. Iterate CTreeCtrl and do consistent check
//check test cases
HTREEITEM tiTreeRoot;
CString strTempText;
tiTreeRoot = m_TreeCtrl.GetRootItem();
m_TreeCtrl.SetCheck(tiTreeRoot,FALSE);
m_TreeCtrl.Expand(tiTreeRoot,TVE_EXPAND);
m_TreeCtrl.ModifyStyle(TVS_CHECKBOXES,0);
m_TreeCtrl.ModifyStyle(0,TVS_CHECKBOXES);
strTempText = m_TreeCtrl.GetItemText(tiTreeRoot);
while(m_TreeCtrl.GetNextItem(tiTreeRoot,TVGN_NEXTVISIBLE))<span style="white-space:pre"> </span>// uncheck all nodes
{
tiTreeRoot = m_TreeCtrl.GetNextItem(tiTreeRoot,TVGN_NEXTVISIBLE);
m_TreeCtrl.SetCheck(tiTreeRoot,FALSE);
}
tiTreeRoot = m_TreeCtrl.GetRootItem();
while(m_TreeCtrl.GetNextItem(tiTreeRoot,TVGN_NEXTVISIBLE))
{
tiTreeRoot = m_TreeCtrl.GetNextItem(tiTreeRoot,TVGN_NEXTVISIBLE);
strTempText = m_TreeCtrl.GetItemText(tiTreeRoot);
//find text from map
map<CString,vector<CString>>::const_iterator mapIte = m_mapTestCases.begin();
mapIte = m_mapTestCases.find(m_strConfiguration);
if(mapIte != m_mapTestCases.end()){
vector<CString>::const_iterator vectorIte = mapIte->second.begin();
for (vectorIte = mapIte->second.begin(); vectorIte != mapIte->second.end(); vectorIte++)
{
if(strTempText == vectorIte->GetString())<span style="white-space:pre"> </span>// if find certain text, do consistent check
{
m_TreeCtrl.SetCheck(tiTreeRoot,TRUE);
ConsistentChildCheck(tiTreeRoot);
ConsistentParentCheck(tiTreeRoot);
}
}
}
}
9. CListCtrl
// Get column name
CHeaderCtrl* pHdr = m_lstEnv.GetHeaderCtrl();
<span style="white-space:pre"> </span>if(pHdr){
HDITEM hdi;
hdi.mask = HDI_TEXT;
hdi.pszText = strTempClnName.GetBuffer( 256 );
hdi.cchTextMax = 256;
pHdr->GetItem( j, &hdi );
strTempClnName.ReleaseBuffer();
}
//display list
if(bIsFirst)
{
CRect rect;
LV_COLUMN lvcolumn;
lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
lvcolumn.fmt = LVCFMT_LEFT;
DWORD dw = m_lstEnv.GetExtendedStyle();
dw |= LVS_EX_FULLROWSELECT ;
m_lstEnv.SetExtendedStyle(dw);
m_lstEnv.GetWindowRect(&rect);
int i = 0;
clnSize = vecEnvTypeName.size();
int lstContentSize[10] = {0};
for (int i =0; i < 10; i++)
m_lstEnv.DeleteColumn(0);
vector<CString>::iterator ite2 = vecEnvTypeName.begin();
if(ite2 != vecEnvTypeName.end())
{
for (i = 0; i < clnSize; i++)
{
if(ite2 != vecEnvTypeName.end()){
CString temp = _T("");
temp = *ite2;
lvcolumn.pszText = (LPTSTR)(LPCTSTR)temp;
}
lvcolumn.iSubItem = i;
lvcolumn.cx = (int)(rect.Width()/clnSize-8);
m_lstEnv.InsertColumn(i, &lvcolumn);
ite2++;
}
}
bIsFirst = FALSE;
}
//display list content
CString strTemp;
BOOL bInserted = TRUE;
vector<CString>::iterator ite = vecEnvTypeValue.begin();
{
for (int i=0; i<clnSize; i++)
{
if( ite != vecEnvTypeValue.end()){
strTemp = *ite;
if(bInserted){
m_lstEnv.InsertItem(nPermCount, strTemp);
m_lstEnv.SetItemData(nPermCount, nPermCount);
bInserted = FALSE;
}
else{
m_lstEnv.SetItemText(nPermCount, i, strTemp);
}
}
ite++;
}
}
PS:
Every time setup an iterator, need if (ite != XXX.end())