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

📄 pipe.cpp

📁 这是一本学习 window编程的很好的参考教材
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	if ( RegCreateKeyEx ( HKEY_LOCAL_MACHINE , m_SubKey , 0 , "" , 0 , KEY_ALL_ACCESS , NULL , &hRegKey , &dwDisposition ) == ERROR_SUCCESS )
	{
		if ( dwDisposition == REG_OPENED_EXISTING_KEY )
		{	
			dwDataLen = sizeof ( DWORD );

			if ( RegQueryValueEx ( hRegKey , "PID" , 0 , NULL , (BYTE*)&dwValuePId , &dwDataLen ) == ERROR_SUCCESS )
			{
				// Check process id &  mutex

				if ( ( hProcess = OpenProcess ( PROCESS_QUERY_INFORMATION , FALSE , dwValuePId ) ) != NULL )
				{
					CloseHandle		( hProcess );

					if ( ( hMutex = OpenMutex ( MUTEX_ALL_ACCESS , FALSE , (LPCSTR)(m_GUID+"/"+m_PipeName) ) ) != NULL )
					{
						CloseHandle		( hMutex );
						
						SetLastError	( PIPE_ERROR_NAME_ALREADY_EXISTS );
						RegCloseKey		( hRegKey );

						// Pipe registration failed 

						return FALSE;
					}								
				}
			}			
		}

		// Create mutex

		if ( ( m_hMutex = CreateMutex ( NULL , TRUE , (LPCSTR)(m_GUID+"/"+m_PipeName) ) ) != NULL )
		{		
			dwValuePId = GetCurrentProcessId();
			dwValueDef = (DWORD)INVALID_HANDLE_VALUE;

			// Save data

			if ( (	RegSetValueEx ( hRegKey , "PID" , 0 , REG_DWORD , (BYTE*)&dwValuePId , sizeof ( DWORD ) ) &
					RegSetValueEx ( hRegKey , "HRP" , 0 , REG_DWORD , (BYTE*)&dwValueDef , sizeof ( DWORD ) ) & 
					RegSetValueEx ( hRegKey , "HWP" , 0 , REG_DWORD , (BYTE*)&dwValueDef , sizeof ( DWORD ) ) ) == ERROR_SUCCESS )
			{
				RegCloseKey ( hRegKey );
				m_bPipeRegistered = TRUE;

				// Pipe registration succeeded

				return TRUE;
			}
		}

		RegCloseKey ( hRegKey );
	}

	SetLastError ( PIPE_ERROR_NAME_REGISTRATION_FAILED );

	// Pipe registration failed 

	return FALSE;
}

/*** UnRegisterPipe **************************************************************/
/*	                                                                             */
/*  Return Value:   true if successful.                                          */
/*	                                                                             */
/*  Parameters:     none.                                                        */
/*	                                                                             */
/*********************************************************************************/

BOOL cPipe::UnRegisterPipe ( void )
{
	// Delete registry key

	if ( m_bPipeRegistered && ( RegDeleteKey ( HKEY_LOCAL_MACHINE , m_SubKey ) == ERROR_SUCCESS ) )
	{
		// Pipe unregistration succeeded

		m_bPipeRegistered = FALSE;
		return TRUE;
	}

	SetLastError ( PIPE_ERROR_NAME_UNREGISTRATIONS_FAILED );

	// Pipe unregistration failed 

	return FALSE;
}

/*** UpdateRegistration **********************************************************/
/*	                                                                             */
/*  Return Value:   true if successful.                                          */
/*	                                                                             */
/*  Parameters:     none.                                                        */
/*	                                                                             */
/*********************************************************************************/

BOOL cPipe::UpdateRegistration ( void )
{
	HKEY	hRegKey;
	DWORD	dwDisposition;

	// Open registry key 

	if ( RegCreateKeyEx ( HKEY_LOCAL_MACHINE , m_SubKey , 0 , "" , 0 , KEY_ALL_ACCESS , NULL , &hRegKey , &dwDisposition ) == ERROR_SUCCESS )
	{
		if ( (	RegSetValueEx ( hRegKey , "HRP" , 0 , REG_DWORD , (BYTE*)&m_hClientReadPipe , sizeof ( DWORD ) ) & 
				RegSetValueEx ( hRegKey , "HWP" , 0 , REG_DWORD , (BYTE*)&m_hClientWritePipe , sizeof ( DWORD ) ) ) == ERROR_SUCCESS )
		{	
			// Registry update succeeded

			RegCloseKey ( hRegKey );

			return TRUE;
		}
	
		RegCloseKey ( hRegKey );
	}

	// Registry update failed 

	return FALSE;
}

/*** RetrievePipe ****************************************************************/
/*	                                                                             */
/*  Return Value:   true if successful.                                          */
/*	                                                                             */
/*  Parameters:     none.                                                        */
/*	                                                                             */
/*********************************************************************************/

BOOL cPipe::RetrievePipe ( void )
{
	HKEY	hRegKey;
	DWORD	dwValuePId;
	DWORD	dwReadPipe;	
	DWORD	dwWritePipe;
	HANDLE	hProcess;
	DWORD	dwDataLen;
	HANDLE	hMutex;

	// Open registry key

	m_SubKey = "SOFTWARE\\" + m_GUID + "\\" + m_PipeName;

	if ( RegOpenKeyEx ( HKEY_LOCAL_MACHINE , m_SubKey , 0 , KEY_ALL_ACCESS , &hRegKey ) == ERROR_SUCCESS )
	{
		dwDataLen = sizeof ( DWORD );

		if ( RegQueryValueEx ( hRegKey , "PID" , 0 , NULL , (BYTE*)&dwValuePId , &dwDataLen ) == ERROR_SUCCESS )
		{
			// Check process id & mutex 

			if ( ( hProcess = OpenProcess ( PROCESS_DUP_HANDLE , FALSE , dwValuePId ) ) != NULL )
			{
				if ( ( hMutex = OpenMutex ( MUTEX_ALL_ACCESS , FALSE , (LPCSTR)(m_GUID+"/"+m_PipeName) ) ) != NULL )
				{
					if ( ( RegQueryValueEx ( hRegKey , "HRP" , 0 , NULL , (BYTE*)&dwReadPipe , &dwDataLen ) &
						   RegQueryValueEx ( hRegKey , "HWP" , 0 , NULL , (BYTE*)&dwWritePipe , &dwDataLen ) ) == ERROR_SUCCESS )
					{
						if ( dwReadPipe != NULL )
						{
							if ( !DuplicateHandle ( hProcess , (HANDLE)dwReadPipe , GetCurrentProcess() , &m_hReadPipe , 0 , FALSE , DUPLICATE_SAME_ACCESS ) )
							{
								m_hReadPipe = INVALID_HANDLE_VALUE;
							}	
						}

						if ( dwWritePipe != NULL )
						{
							if ( !DuplicateHandle ( hProcess , (HANDLE)dwWritePipe , GetCurrentProcess() , &m_hWritePipe , 0 , FALSE , DUPLICATE_SAME_ACCESS ) )
							{
								m_hWritePipe = INVALID_HANDLE_VALUE;
							}
						}

						if ( ( m_hReadPipe != INVALID_HANDLE_VALUE ) && ( m_hWritePipe != INVALID_HANDLE_VALUE ) )
						{
							CloseHandle	( hProcess );
							CloseHandle	( hMutex );
							RegCloseKey	( hRegKey );

							// Retrieve pipe succeeded

							return TRUE;
						}
					}
					
					SetLastError	( PIPE_ERROR_CREATION_FAILED );
					CloseHandle		( hProcess );
					CloseHandle		( hMutex );
					RegCloseKey		( hRegKey );

					// Retrieve pipe failed 

					return FALSE;
				}
			}

			CloseHandle	( hProcess );
		}

		RegCloseKey ( hRegKey );
	}
	
	SetLastError ( PIPE_ERROR_NAME_DOES_NOT_EXISTS );

	// Retrieve pipe failed 

	return FALSE;
}

/*** CallBackThread **************************************************************/
/*	                                                                             */
/*  Return Value:   thread exit code.                                            */
/*	                                                                             */
/*  Parameters:     pParam             Pointer to a single parameter value       */
/*                                     passed to the thread function.            */
/*	                                                                             */
/*********************************************************************************/

UINT cPipe::CallBackThread ( LPVOID pParam )
{
	cPipe	*pThis			= (cPipe*)pParam;
	BOOL	bValidPtr		= TRUE;
	BYTE	Buffer[PIPE_MAX_READ_BUFFER_SIZE];
	DWORD	dwBytesRead;

	// Init

	pThis->m_bVaildThread	= TRUE;
	pThis->m_pbVaildPtr		= &bValidPtr;

	while ( pThis->m_bRunThread && pThis->ReadPipe ( &Buffer[0] , PIPE_MAX_READ_BUFFER_SIZE , &dwBytesRead ) )
	{
		if ( bValidPtr ) pThis->m_CallBackFunc ( pThis->m_pCallBackParam , &Buffer[0] , dwBytesRead ); 
	}

	// Leave thread

	if ( bValidPtr ) pThis->m_bVaildThread = FALSE;

	return 0;
}

/*** CallBack ********************************************************************/
/*	                                                                             */
/*  Return Value:   true if successful.                                          */
/*	                                                                             */
/*  Parameters:     none.                                                        */
/*	                                                                             */
/*********************************************************************************/

BOOL cPipe::CallBack ( void )
{
	if ( !m_CallBackFunc ) return TRUE;

	if ( AfxBeginThread ( (AFX_THREADPROC)CallBackThread , (void*)this ) )
	{
		// CallBack succeeded

		return TRUE;
	}

	SetLastError ( PIPE_ERROR_CALLBACK );

	// CallBack failed 

	return FALSE;
}

/*** SetLastError ****************************************************************/
/*	                                                                             */
/*  Return Value:   none.                                                        */
/*	                                                                             */
/*  Parameters:     nErrorCode      Specifies the last-error code.               */
/*	                                                                             */
/*********************************************************************************/

void cPipe::SetLastError ( int nErrorCode )
{
	// Set last erroer value

	m_LastError = nErrorCode;
}

⌨️ 快捷键说明

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