Visual C++ Developer Center > Visual C++ Forums > Visual C++ General > send message from service to user mode application in vista/windows7
Ask a questionAsk a question
 

Answersend message from service to user mode application in vista/windows7

  • Tuesday, October 27, 2009 1:17 PMJangid Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I want to send a message from windows service to user mode application, I know its not allowed in Vista and above OS.
    is there any special window message for this? or any other alternate?

    I want to play sound on a specific operation, service should inform to application for play the sound on some specific events.  



    मनोज कुमार जांगिड NATHCORP

Answers

All Replies

  • Tuesday, October 27, 2009 2:27 PMScott McPhillipsMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    A service and a user mode application can communicate by using a named pipe.  See CreateNamedPipe()
  • Tuesday, October 27, 2009 3:21 PM«_Superman_»MVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You can use a named event. The user mode application can wait for the event to be signaled. The name of the event must be prefixed with Global\.
    Look at the documentation for CreateEvent.

    «_Superman_»
    Microsoft MVP (Visual C++)
  • Wednesday, November 04, 2009 4:50 AMJangid Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yes its worked

    Thank you
    मनोज कुमार जांगिड NATHCORP
  • Tuesday, November 10, 2009 2:00 PMJangid Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    When I call CreateFile() second time I am getting error massege 231 [ERROR_PIPE_BUSY - All pipe instances are busy]

    I am sending message from service application to client application.
    Named pipe is create in service application and client application is receving data from service application.

    Service Application Functions

    bool InitServer()
    {
    	try
    	{
    		SECURITY_ATTRIBUTES sa;
    		sa.lpSecurityDescriptor = (PSECURITY_DESCRIPTOR)malloc(SECURITY_DESCRIPTOR_MIN_LENGTH);
    		if (!InitializeSecurityDescriptor(sa.lpSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION))
    		{
    			strLog.Format(_T("ERROR(Line:%d):%s"),__LINE__, GetErrorString(::GetLastError()));
    			AddToLog(strTitle,strLog);
    		}
    		if (!SetSecurityDescriptorDacl(sa.lpSecurityDescriptor, TRUE, (PACL)0, FALSE))
    		{
    			strLog.Format(_T("ERROR(Line:%d):%s"),__LINE__, GetErrorString(::GetLastError()));
    			AddToLog(strTitle,strLog);
    		}
    		sa.nLength = sizeof sa;
    		sa.bInheritHandle = TRUE;
    
    		union maxSize
    		{
    			UINT _1;
    		};
    
    		m_hServer = ::CreateNamedPipe(	m_strPipeName,
    			PIPE_ACCESS_OUTBOUND, 
    			PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 
    			PIPE_UNLIMITED_INSTANCES, 
    			sizeof maxSize,	
    			sizeof maxSize, 
    			NMPWAIT_USE_DEFAULT_WAIT, 
    			&sa);
    
    		if (m_hServer == INVALID_HANDLE_VALUE)
    		{
    		return false;
    		}
    	}
    	catch(...)
    	{
    		return false;
    	}
    	return true;
    }
    
    
    bool SendData(UINT uID)
    {
    	DWORD dwRead = 0;
    	if (!(WriteFile(m_hServer, (LPVOID)&uID, sizeof UINT, &dwRead, 0)))
    	{
    	return false;
    	}
    ::Sleep(0);
    return true;
    }

    Client Application

    bool	InitClient()
    {
    	DWORD dwError = 0;
    	while (true) 
    	{ 
    		m_hClient = ::CreateFile(	m_strPipeName, 
    					GENERIC_READ, 
    					0, 
    					0, 
    					OPEN_EXISTING, 
    					0, 
    					0);
    		if (m_hClient != INVALID_HANDLE_VALUE)
    		{
    			break;
    		}
    		if (GetLastError() != ERROR_PIPE_BUSY) 
    		{
    			return false;
    		}
    		if (!WaitNamedPipe(m_strPipeName, 10000)) 
    		{ 
    			return false;
    		} 
    	}
    	return true;
    }
    Above client code working when I am connecting first time.
    After restart client application I am getting above mentioned error.

    WaitNamedPipe -  this gives [Error Number - 121 The semaphore timeout period has expired]

    any clue to solve this?


    मनोज कुमार जांगिड NATHCORP
  • Tuesday, November 10, 2009 2:57 PMScott McPhillipsMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    >> After restart client application I am getting above mentioned error

    Does the first client close the pipe (with CloseHandle)?

    If you create only one pipe in the server it can connect to only one client at a time.

  • Wednesday, November 11, 2009 4:26 AMJangid Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yes client application handle closed with CloseHandle()
    ::CloseHandle(m_hClient);
      m_hClient = NULL;

    I have only one client at a time - but this client can be restart multiple times
    मनोज कुमार जांगिड NATHCORP