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

📄 dllmain.cpp

📁 基于Intellution开发包的开发的OPC服务器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			return TRUE;
		}
	}
	return FALSE;
}


////////////////////////////////////////////////////////////////
// AddRegNamedStringValue()
//
// Summary:	Internal utility function to add a named data value 
//			string to an existing Key (with optional Subkey) in 
//			the system Registry under HKEY_CLASSES_ROOT.
//
// Args:	LPTSTR pszKey,
//			LPTSTR pszSubkey,
//			LPTSTR pszValueName,
//			LPTSTR pszValue
//
// Returns:	BOOL
//			TRUE if success; FALSE if not.
////////////////////////////////////////////////////////////////
BOOL AddRegNamedStringValue(LPTSTR		pszKey, 
							LPTSTR		pszSubkey, 
							LPTSTR		pszValueName,
							LPTSTR		pszValue)
{
	BOOL	bOk = FALSE;
	LONG	lRetVal;
	HKEY	hKey;
	TCHAR	szKey[MAX_STRING_LENGTH];


	lstrcpy(szKey, pszKey);

	if (NULL != pszSubkey)
	{
		lstrcat(szKey, TEXT("\\"));
		lstrcat(szKey, pszSubkey);
	}

	lRetVal = RegOpenKeyEx(HKEY_CLASSES_ROOT, 
						   szKey, 
						   0, 
						   KEY_ALL_ACCESS, 
						   &hKey);

	if (NULL != pszValue && ERROR_SUCCESS == lRetVal)
	{
		lRetVal = RegSetValueEx(hKey, 
								pszValueName, 
								0, 
								REG_SZ, 
								(BYTE *)pszValue,
								(lstrlen(pszValue)+1)*sizeof(TCHAR));

		if (ERROR_SUCCESS == lRetVal)
		{
			bOk = TRUE;
		}

		RegCloseKey(hKey);
	}

	return bOk;
}


////////////////////////////////////////////////////////////////
// A_StringFromGUID2()
//
// Summary:  ANSI Surrogate for the OLE Unicode API call.
////////////////////////////////////////////////////////////////
STDAPI A_StringFromGUID2(REFGUID	guid, 
						 LPSTR		pszGUID, 
						 int		nNumChars)
{
	HRESULT		hr = E_INVALIDARG;
	LPWSTR		pszUc;
	IMalloc		*pIMalloc;
	
	
	if (NULL != pszGUID && 0 < nNumChars)
	{
		hr = CoGetMalloc(MEMCTX_TASK, &pIMalloc);
		if (SUCCEEDED(hr))
		{
			pszUc = (LPWSTR)pIMalloc->Alloc((nNumChars+1)*sizeof(TCHAR));
			pIMalloc->Release();
			if (NULL != pszUc)
			{
				hr = StringFromGUID2(guid, pszUc, nNumChars);
				if (SUCCEEDED(hr))
				{
					hr = UcToAnsi(pszUc, pszGUID, nNumChars);
				}
				
				CoTaskMemFree((void *)pszUc);
			}
			else
			{
				hr = E_OUTOFMEMORY;
			}
		}
	}
	
	return hr;
}

////////////////////////////////////////////////////////////////
// UcToAnsi()
//
// Summary:  Convert a UNICODE input string to an output ANSI 
//           string.
//
// Args:     LPWSTR pszUnicodeString
//             Pointer to an input wide UNICODE string.
//           LPSTR pszAnsiString
//             Pointer to output ANSI string.
//           int nNumChars
//             Character count.
//
// Returns:  HRESULT
////////////////////////////////////////////////////////////////
HRESULT UcToAnsi(LPWSTR		pszUc, 
				 LPSTR		pszAnsi, 
				 int		nNumChars)
{
	HRESULT		hr = E_FAIL;
	int			nSize,
				nOut;
	
	
	if (0 == nNumChars)
	{
		nSize = WideCharToMultiByte(CP_ACP, 0, pszUc, -1, NULL, 0, NULL, NULL);
	}
	else
	{
		nSize = nNumChars;
	}
	
	if (0 != nSize)
	{
		nOut = WideCharToMultiByte(CP_ACP, 0, pszUc, -1, pszAnsi, nSize, NULL, NULL);
		if (0 != nOut)
		{
			hr = NOERROR;
		}
	}
	
	return hr;
}


////////////////////////////////////////////////////////////////
// OPCServerUnload()
//
// This function is called by the ClassFactory
// whenever a server is destroyed in case it is the last one.
// This allows an EXE implementation to exit.
// In the case of a DLL there is nothing to do.
//
////////////////////////////////////////////////////////////////
void OPCServerUnload(void)
{
}


////////////////////////////////////////////////////////////////
// GetServerClassID()
// 
// Obtains the class ID for the server.
//
// Returns:  TRUE
//
////////////////////////////////////////////////////////////////
HRESULT	GetServerClassID(CLSID *pClassID)
{
	*pClassID = CLSID_COPCDrvServer;
	return S_OK;
}


////////////////////////////////////////////////////////////////
// BuildStrings()
// 
// Builds text strings for the given driver acronym
//
// Returns:  void
//
////////////////////////////////////////////////////////////////
void BuildStrings(void)
{
	// Server description
	_tcscpy(g_tszServerDesciption, g_tszAcronym);
	_tcscat(g_tszServerDesciption, TEXT("Drv OPC Server"));

	// Version independant prog id
	_tcscpy(g_tszIndepProgID, TEXT("Intellution."));
	_tcscat(g_tszIndepProgID, g_tszAcronym);
	_tcscat(g_tszIndepProgID, TEXT("OPC"));

	// Version dependant prog id
	_tcscpy(g_tszDepProgID, TEXT("Intellution."));
	_tcscat(g_tszDepProgID, g_tszAcronym);
	_tcscat(g_tszDepProgID, TEXT("OPC.1"));

	// Base registry key
	_tcscpy(g_tszRegKey, TEXT("Software\\Intellution\\Drivers\\"));
	_tcscat(g_tszRegKey, g_tszAcronym);
	_tcscat(g_tszRegKey, TEXT("\\OPC"));

	// Error sent to Event Viewer id IMalloc interface fails
	_tcscpy(g_tszMallocError, g_tszAcronym);
	_tcscat(g_tszMallocError, TEXT("OPC - unable to obtain IMalloc interface pointer"));
}


////////////////////////////////////////////////////////////////
// SetRegistryDefaults()
// 
// This function will set the internal defaul parameters for the
// server, group and item objects in the system registry.
//
// This function should be called only during server registration!!
//
////////////////////////////////////////////////////////////////
void SetRegistryDefaults(void)
{
	DWORD	dwStatus		= 0,
			dwDisposition	= 0,
			dwData			= 0,
			dwSize			= sizeof(DWORD),
			dwStringSize	= 100;
	HKEY	hKeyResult;
	char	*szKey,
			szKeyVal[BUFFER_SIZE],
			szKeyValName[BUFFER_SIZE],
			*pBuff;
	int		nLength			= 0;
		

	USES_CONVERSION;
	szKey = T2A(g_tszRegKey);

	//
	// Get the values for the Server Object's defaults
	//

	hKeyResult = (HKEY)0;
	strcpy(szKeyVal, szKey);
	strcat(szKeyVal, "\\ServerDefaults");
	dwStatus = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
							  (LPCTSTR)szKeyVal,
							  0,
							  NULL,
							  REG_OPTION_NON_VOLATILE,
							  KEY_ALL_ACCESS,
							  NULL,
							  &hKeyResult,
							  &dwDisposition);

	// Let's see if the key was created or opened.
	switch(dwDisposition)
	{
	case REG_CREATED_NEW_KEY:
		//
		// The key was not present and was just created, so set the defaults
		//

		// Sleep time
		strcpy(szKeyValName, "ThreadSleepTime");
		dwStatus = RegSetValueEx(hKeyResult,
								 szKeyValName,
								 0,
								 REG_DWORD, 
								 (const unsigned char *)&g_dwWaitPeriod,
								 dwSize);

		// Priority
		strcpy(szKeyValName, "ThreadPriority");
		dwStatus = RegSetValueEx(hKeyResult,
								 szKeyValName,
								 0,
								 REG_DWORD, 
								 (const unsigned char *)&g_dwServerThreadPriority,
								 dwSize);
		break;

	default:
	case REG_OPENED_EXISTING_KEY:
		break;
	}
	RegCloseKey(hKeyResult);


	//
	// Get the values for the Item Object's defaults
	//

	hKeyResult = (HKEY)0;
	strcpy(szKeyVal, szKey);
	strcat(szKeyVal, "\\ItemDefaults");
	dwStatus = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
							  (LPCTSTR)szKeyVal,
							  0,
							  NULL,
							  REG_OPTION_NON_VOLATILE,
							  KEY_ALL_ACCESS,
							  NULL,
							  &hKeyResult,
							  &dwDisposition);

	// Let's see if the key was created or opened.
	switch(dwDisposition)
	{
	case REG_CREATED_NEW_KEY:
		//
		// The key was not present and was just created, so set it to the defaults
		//

		// AutoStart
		strcpy(szKeyValName, "AutoStart");
		dwStatus = RegSetValueEx(hKeyResult,
								 szKeyValName,
								 0,
								 REG_DWORD, 
								 (const unsigned char *)&g_bAutoStart,
								 dwSize);

		// StringLength
		strcpy(szKeyValName, "StringLength");
		dwStatus = RegSetValueEx(hKeyResult,
								 szKeyValName,
								 0,
								 REG_DWORD, 
								 (const unsigned char *)&g_wStringLength,
								 dwSize);

		// SignalConditioning
		strcpy(szKeyValName, "SignalConditioning");
		nLength	= g_strDefaultSignalConditioning.GetLength();
		pBuff	= g_strDefaultSignalConditioning.GetBuffer(nLength+1);
		dwStatus = RegSetValueEx(hKeyResult,
								 szKeyValName,
								 0,
								 REG_SZ, 
								 (const unsigned char *)pBuff,
								 strlen(pBuff));
		g_strDefaultSignalConditioning.ReleaseBuffer();

		// LoEGU
		strcpy(szKeyValName, "LoEGU");
		nLength	= g_strDefaultLoEGU.GetLength();
		pBuff	= g_strDefaultLoEGU.GetBuffer(nLength+1);
		dwStatus = RegSetValueEx(hKeyResult,
								 szKeyValName,
								 0,
								 REG_SZ, 
								 (const unsigned char *)pBuff,
								 strlen(pBuff));
		g_strDefaultLoEGU.ReleaseBuffer();

		// HiEGU
		strcpy(szKeyValName, "HiEGU");
		nLength	= g_strDefaultHiEGU.GetLength();
		pBuff	= g_strDefaultHiEGU.GetBuffer(nLength+1);
		dwStatus = RegSetValueEx(hKeyResult,
								 szKeyValName,
								 0,
								 REG_SZ, 
								 (const unsigned char *)pBuff,
								 strlen(pBuff));
		g_strDefaultHiEGU.ReleaseBuffer();

		// HardwareOptions
		strcpy(szKeyValName, "HardwareOptions");
		nLength	= g_strDefaultHardwareOptions.GetLength();
		pBuff	= g_strDefaultHardwareOptions.GetBuffer(nLength+1);
		dwStatus = RegSetValueEx(hKeyResult,
								 szKeyValName,
								 0,
								 REG_SZ, 
								 (const unsigned char *)pBuff,
								 strlen(pBuff));
		g_strDefaultHardwareOptions.ReleaseBuffer();
		break;

	default:
	case REG_OPENED_EXISTING_KEY:
		break;
	}
	RegCloseKey(hKeyResult);


	//
	// Get the Group Object's defaults
	//

	hKeyResult = (HKEY)0;
	strcpy(szKeyVal, szKey);
	strcat(szKeyVal, "\\GroupDefaults");
	dwStatus = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
							  (LPCTSTR)szKeyVal,
							  0,
							  NULL,
							  REG_OPTION_NON_VOLATILE,
							  KEY_ALL_ACCESS,
							  NULL,
							  &hKeyResult,
							  &dwDisposition);

	// Let's see if the key was created or opened.
	switch(dwDisposition)
	{
	case REG_CREATED_NEW_KEY:
		//
		// The key was not present and was just created, so set it to the default
		//

		// Generated Group object name
		strcpy(szKeyValName, "GeneratedGroupName");
		nLength	= g_strDefaultGeneratedGroupName.GetLength();
		pBuff	= g_strDefaultGeneratedGroupName.GetBuffer(nLength+1);
		dwStatus = RegSetValueEx(hKeyResult,
								 szKeyValName,
								 0,
								 REG_SZ, 
								 (const unsigned char *)pBuff,
								 strlen(pBuff));
		g_strDefaultGeneratedGroupName.ReleaseBuffer();
		break;

	default:
	case REG_OPENED_EXISTING_KEY:
		break;
	}
	RegCloseKey(hKeyResult);
}


////////////////////////////////////////////////////////////////
// GetRegistryDefaults()
// 
// This function will read default parameters for the server, group
// and item objects from the system registry.
//
// Returns	TRUE	if successful
//			FALSE	if error
//
////////////////////////////////////////////////////////////////
BOOL	GetRegistryDefaults(void)
{
	TCHAR	pszData[BUFFER_SIZE];


	//
	// Get the server defaults
	//

	if (GetRegNamedStringValue(g_tszRegKey, _T("ServerDefaults"), _T("ThreadSleepTime"), pszData))
	{
		g_dwWaitPeriod = *(DWORD *)pszData;
	}
	if (GetRegNamedStringValue(g_tszRegKey, _T("ServerDefaults"), _T("ThreadPriority"), pszData))
	{
		g_dwServerThreadPriority = *(DWORD *)pszData;
	}

	//
	// Get the group defaults
	//

	if (GetRegNamedStringValue(g_tszRegKey, _T("GroupDefaults"), _T("GeneratedGroupName"), pszData))
	{
		g_strDefaultGeneratedGroupName = pszData;
	}

	//
	// Get the item defaults
	//

	if (GetRegNamedStringValue(g_tszRegKey, _T("ItemDefaults"), _T("AutoStart"), pszData))
	{
		g_bAutoStart = (BOOL)*(DWORD *)pszData;
	}
	if (GetRegNamedStringValue(g_tszRegKey, _T("ItemDefaults"), _T("StringLength"), pszData))
	{
		g_wStringLength = (WORD)*(DWORD *)pszData;
	}
	if (GetRegNamedStringValue(g_tszRegKey, _T("ItemDefaults"), _T("SignalConditioning"), pszData))
	{
		g_strDefaultSignalConditioning = pszData;
	}
	if (GetRegNamedStringValue(g_tszRegKey, _T("ItemDefaults"), _T("LoEGU"), pszData))
	{
		g_strDefaultLoEGU = pszData;
	}
	if (GetRegNamedStringValue(g_tszRegKey, _T("ItemDefaults"), _T("HiEGU"), pszData))
	{
		g_strDefaultHiEGU = pszData;
	}
	if (GetRegNamedStringValue(g_tszRegKey, _T("ItemDefaults"), _T("HardwareOptions"), pszData))
	{
		g_strDefaultHardwareOptions = pszData;
		if (!g_strDefaultHardwareOptions.CompareNoCase("NONE"))
		{
			g_strDefaultHardwareOptions.Empty();
		}
	}

	return TRUE;
}


⌨️ 快捷键说明

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