⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 itempropertiesdlg.cpp

📁 PC客户和opc通信的源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
			pCombo->AddString (_T("Word"));
			pCombo->AddString (_T("Short"));
			pCombo->AddString (_T("DWord"));
			pCombo->AddString (_T("Long"));
			pCombo->AddString (_T("Float"));
			pCombo->AddString (_T("Double"));
			pCombo->AddString (_T("String"));
			}
		}

	// If item is not valid (item has not be re-added), allow all data types:
	else
		{
		pCombo->AddString (_T("Native"));
		pCombo->AddString (_T("Boolean"));
		pCombo->AddString (_T("Byte"));
		pCombo->AddString (_T("Byte Array"));
		pCombo->AddString (_T("Char"));
		pCombo->AddString (_T("Char Array"));
		pCombo->AddString (_T("Word"));
		pCombo->AddString (_T("Word Array"));
		pCombo->AddString (_T("Short"));
		pCombo->AddString (_T("Short Array"));
		pCombo->AddString (_T("DWord"));
		pCombo->AddString (_T("DWord Array"));
		pCombo->AddString (_T("Long"));
		pCombo->AddString (_T("Long Array"));
		pCombo->AddString (_T("Float"));
		pCombo->AddString (_T("Float Array"));
		pCombo->AddString (_T("Double"));
		pCombo->AddString (_T("Double Array"));
		pCombo->AddString (_T("String"));
		}

	// Fill available 2.0 item properties (We won't be able to do this
	// unless we have a pointer to the item properties interface.)
	if (m_pIItemProps != NULL)
		{
		TCHAR szBuffer [DEFBUFFSIZE];
		WCHAR *pszItemID		= NULL;
		DWORD dwCount			= 0;
		DWORD dwIndex			= 0;		
		DWORD *pdwIDs			= NULL;
		LPWSTR *pszDescriptions	= NULL;
		LPWSTR *pszLookupIDs	= NULL;
		VARTYPE *pvtDataTypes	= NULL;
		VARIANT *pvtValues		= NULL;
		HRESULT *pValErrs		= NULL;
		HRESULT *pLookupErrs	= NULL;
		HRESULT hr				= E_FAIL;

		// Get pointer to list control:
		CListCtrl *pList = (CListCtrl *) GetDlgItem (IDC_LIST);
		ASSERT (pList != NULL);

		// Delete any list items that are present:
		pList->DeleteAllItems ();

		// Obtain the qualified ID:
		ASSERT (!m_strItemID.IsEmpty ());

		CString strQualifiedID;

		if (!m_strAccessPath.IsEmpty () && m_pServer->IsKepServerEx ())
			{
			strQualifiedID = m_strAccessPath;
			strQualifiedID += _T(".");
			strQualifiedID += m_strItemID;
			}
		else
			strQualifiedID = m_strItemID;

		// Allocate storage for item ID string:
		pszItemID = (WCHAR *) CoTaskMemAlloc ((strQualifiedID.GetLength () + 1) * sizeof (WCHAR));

		// Convert item ID to UNICODE if needed and copy to allocated memory:
#ifdef _UNICODE
		lstrcpyn (pszItemID, strQualifiedID, strQualifiedID.GetLength () + 1);
#else
		_mbstowcsz (pszItemID, strQualifiedID, strQualifiedID.GetLength () + 1);
#endif
		
		// Query available properties:
		hr = m_pIItemProps->QueryAvailableProperties (
			  pszItemID, &dwCount, &pdwIDs, &pszDescriptions, &pvtDataTypes);

		if (SUCCEEDED (hr))
			{
			// Get the current property values for the property ID's we 
			// just queried:
			hr = m_pIItemProps->GetItemProperties (pszItemID, dwCount, 
				pdwIDs, &pvtValues, &pValErrs);

			if (SUCCEEDED (hr))
				{
				// Get lookup item IDs for the property ID's we just queried:
				hr = m_pIItemProps->LookupItemIDs (pszItemID, dwCount, 
					pdwIDs, &pszLookupIDs, &pLookupErrs);

				if (SUCCEEDED (hr))
					{
					// Insert item IDs, descriptions, values and lookup IDs 
					// into the list control:
					for (dwIndex = 0; dwIndex < dwCount; dwIndex++)
						{
						// ID:
						_itot (pdwIDs [dwIndex], szBuffer, 10);
						pList->InsertItem (dwIndex, szBuffer);

						// Description (may have to convert from UNICODE):
#ifdef _UNICODE
						pList->SetItemText (dwIndex, 1, pszDescriptions [dwIndex]);
#else
						_wcstombsz (szBuffer, pszDescriptions [dwIndex], sizeof (szBuffer) / sizeof (TCHAR));
						pList->SetItemText (dwIndex, 1, szBuffer);
#endif						

						// {roperty value:
						if (pValErrs && SUCCEEDED (pValErrs [dwIndex]))
							GetValue (pvtValues [dwIndex], szBuffer, sizeof (szBuffer) / sizeof (TCHAR)); 
						else
							lstrcpyn (szBuffer, _T("???"), sizeof (szBuffer) / sizeof (TCHAR));
						
						pList->SetItemText (dwIndex, 2, szBuffer);

						// Lookup item ID (may have to convert from UNICODE):
						if (pLookupErrs && SUCCEEDED (pLookupErrs [dwIndex]))
							{
#ifdef _UNICODE
							lstrcpyn (szBuffer, pszLookupIDs [dwIndex], sizeof (szBuffer) / sizeof (TCHAR));
#else
							_wcstombsz (szBuffer, pszLookupIDs [dwIndex], sizeof (szBuffer) / sizeof (TCHAR));
#endif
							}
						else
							{
							// No lookup item ID, so place "N/A" in list control:
							lstrcpyn (szBuffer, _T("N/A"), sizeof (szBuffer) / sizeof (TCHAR));
							pszLookupIDs [dwIndex] = NULL;
							}
						
						pList->SetItemText (dwIndex, 3, szBuffer);
						}
					}
				}
			}

		// COM requires us to free memory for data passed back to us:
		for (dwIndex = 0; dwIndex < dwCount; dwIndex++)
			{
			if (pszDescriptions && pszDescriptions [dwIndex]) 
				CoTaskMemFree (pszDescriptions [dwIndex]);

			if (pszLookupIDs && pszLookupIDs [dwIndex])
				CoTaskMemFree (pszLookupIDs [dwIndex]);

			// Clear variants:
			if (pvtValues)
				VariantClear (&pvtValues [dwIndex]);
			}

		if (pdwIDs)
			CoTaskMemFree (pdwIDs);

		if (pszDescriptions)
			CoTaskMemFree (pszDescriptions);

		if (pszLookupIDs)
			CoTaskMemFree (pszLookupIDs);

		if (pvtDataTypes)
			CoTaskMemFree (pvtDataTypes);

		if (pvtValues)
			CoTaskMemFree (pvtValues);
			
		if (pValErrs)
			CoTaskMemFree (pValErrs);

		if (pLookupErrs)
			CoTaskMemFree (pLookupErrs);
		}

	// Set the focus to the first control (data type) if the control that
	// previously had it became disabled:
	if (hWnd && !::IsWindowEnabled (hWnd))
		GetDlgItem (IDC_DATATYPE)->SetFocus ();

	// Transfer data to controls:
	UpdateData (false);
	}

// **************************************************************************
// GetValue ()
//
// Description:
//	Return a string representing the value of a variant.
//
// Parameters:
//  VARIANT		&vtVal		Input variant.
//	TCHAR		*pBuffer	Pointer to buffer for output string.
//	int			nBufLen		Size of output buffer.
//
// Returns:
//  void
// **************************************************************************
void CKItemPropertiesDlg::GetValue (VARIANT &vtVal,	// [in]	
									TCHAR *pBuffer, // [out]
									int nBufLen)	// [in]
	{
	ASSERT (pBuffer != NULL);
	ASSERT (nBufLen > 0);

	// Declare a CString to help construct result string:
	CString strVal;

	// Format string according to data type:
	switch (vtVal.vt)
		{
		case VT_BOOL:
			strVal.Format (_T("%d"), vtVal.boolVal);
			break;

		case VT_UI1:
			strVal.Format (_T("%u"), vtVal.bVal);
			break;

		case VT_I1:
			strVal.Format (_T("%d"), vtVal.cVal);
			break;

		case VT_UI2:
			strVal.Format (_T("%u"), vtVal.uiVal);
			break;

		case VT_I2:
			strVal.Format (_T("%d"), vtVal.iVal);
			break;

		case VT_UI4:
			strVal.Format (_T("%u"), vtVal.ulVal);
			break;

		case VT_I4:
			strVal.Format (_T("%d"), vtVal.lVal);
			break;

		case VT_R4:
			strVal.Format (_T("%G"), vtVal.fltVal);
			break;

		case VT_R8:
			strVal.Format (_T("%G"), vtVal.dblVal);
			break;

		case VT_BSTR:
			strVal = vtVal.bstrVal;
			break;

		case VT_DATE:
			{
			bool bSuccess = false;

			// Cariant time to system time (UTC):
			SYSTEMTIME systime;
			if (VariantTimeToSystemTime (vtVal.dblVal, &systime))
				{
				// Get time zone information:
				TIME_ZONE_INFORMATION tTZI;
				if (GetTimeZoneInformation (&tTZI) != TIME_ZONE_ID_INVALID)
					{
					// Localize system time:
					SYSTEMTIME systimelocal;
					if (SystemTimeToTzSpecificLocalTime (&tTZI, &systime, &systimelocal))
						{
						strVal.Format (_T("%02d:%02d:%02d"), 
							systimelocal.wHour, systimelocal.wMinute, systimelocal.wSecond);			

						bSuccess = true;
						}
					}
				}
			
			if (!bSuccess)
				strVal = _T("???");
			}
			break;

		case VT_UI1	| VT_ARRAY:
		case VT_I1	| VT_ARRAY:
		case VT_UI2	| VT_ARRAY:
		case VT_I2	| VT_ARRAY:
		case VT_UI4	| VT_ARRAY:
		case VT_I4	| VT_ARRAY:
		case VT_R4	| VT_ARRAY:
		case VT_R8	| VT_ARRAY:
			{
			CSafeArray *pSafeArr = (CSafeArray *) vtVal.parray;
			DWORD dwCols = pSafeArr->GetNumCols ();
			DWORD dwSize = pSafeArr->GetByteLength ();
			ULONG cbElements = pSafeArr->cbElements;
			LPBYTE lpByte = (LPBYTE)pSafeArr->pvData;
			DWORD dwCol = 0;

			// Start row delimiter:
			strVal = _T("[ ");

			// Cycle through the elements:
			for (DWORD i = 0; i < dwSize; i += cbElements, lpByte += cbElements)
				{
				TCHAR szNum[32];

				// Format element according to data size:
				if (cbElements == 1)
					{
					if (vtVal.vt ==	(VT_UI1 | VT_ARRAY))
						_stprintf (szNum, _T("%u"), *lpByte);
					else
						_stprintf (szNum, _T("%d"), *(char *)lpByte);
					}
				
				else if (cbElements == 2)
					{
					if (vtVal.vt ==	(VT_UI2 | VT_ARRAY))
						_stprintf (szNum, _T("%u"), *(WORD *)lpByte);
					else 
						_stprintf (szNum, _T("%d"), *(short *)lpByte);
					}
				
				else if (cbElements == 4)
					{
					if (vtVal.vt ==	(VT_R4	| VT_ARRAY))
						_stprintf (szNum, _T("%G"), *(float *)lpByte);
					else if (vtVal.vt ==	(VT_UI4	| VT_ARRAY))
						_stprintf (szNum, _T("%u"), *(DWORD *)lpByte);
					else if (vtVal.vt ==	(VT_I4	| VT_ARRAY))
						_stprintf (szNum, _T("%d"), *(DWORD *)lpByte);
					}

				else if (cbElements == 8)
					_stprintf (szNum, _T("%G"), *(double *)lpByte);

				else
					{
					ASSERT (FALSE);
					}

				// Delimit each element within the row with a comma:
				if (dwCol != 0)
					strVal += _T(", ");

				// Append the formatted element data:
				strVal += szNum;

				// Terminate each row (except the last):
				if (++dwCol == dwCols)
					{
					if (i < dwSize - cbElements) 
						strVal += _T(" ] [ ");

					dwCol = 0;
					}
				}

			// End delimiter:
			strVal += _T(" ]");
			}
			break;

		default:
			// Unsupported datatype:
			strVal = _T ("<Bad VARTYPE>");
			break;
		}

	// Copy value to output buffer:
	lstrcpyn (pBuffer, strVal, nBufLen);
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -