Click here to Skip to main content
15,888,454 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: Problem opening URL Pin
Niklas L27-May-11 1:09
Niklas L27-May-11 1:09 
QuestionFindWindow() without giving Application windows caption Pin
manju 326-May-11 0:44
manju 326-May-11 0:44 
AnswerRe: FindWindow() without giving Application windows caption Pin
tagopi26-May-11 1:33
tagopi26-May-11 1:33 
AnswerRe: FindWindow() without giving Application windows caption Pin
ThatsAlok26-May-11 21:39
ThatsAlok26-May-11 21:39 
Questionendtask program using win32api/c like taskmanager to close programs without invoking taskmanager just in one shot Pin
Jayapal Chandran25-May-11 20:52
Jayapal Chandran25-May-11 20:52 
AnswerRe: endtask program using win32api/c like taskmanager to close programs without invoking taskmanager just in one shot Pin
_AnsHUMAN_ 25-May-11 21:53
_AnsHUMAN_ 25-May-11 21:53 
AnswerRe: endtask program using win32api/c like taskmanager to close programs without invoking taskmanager just in one shot Pin
Mark Salsbery25-May-11 21:54
Mark Salsbery25-May-11 21:54 
AnswerRe: endtask program using win32api/c like taskmanager to close programs without invoking taskmanager just in one shot Pin
MicroVirus26-May-11 0:27
MicroVirus26-May-11 0:27 
I guess you're in luck. I had this same idea and wrote a piece of code, using the two documentations linked by the other poster. The following code searches for the Windows Media Player Tray Control, terminates it, and then starts it again.
I'm sure you can modify it quite easily to work for your situation.
BOOL FindTrayProcess( DWORD& pid );
void PrintError( TCHAR* msg );

int _tmain(int argc, _TCHAR* argv[])
{
	DWORD pid;
	HANDLE hTray;
	TCHAR szPath[MAX_PATH+1] = TEXT("C:\\Program Files\\Windows Media Bonus Pack for Windows XP\\PowerToys\\mpxptray.exe"); // Used for (re)starting it
	STARTUPINFO si;
	PROCESS_INFORMATION pi;

	// Find the tray process, and kill it
	if (FindTrayProcess( pid ))
	{
		if (INVALID_HANDLE_VALUE == (hTray = OpenProcess( PROCESS_TERMINATE, FALSE, pid )))
		{
			PrintError(TEXT("OpenProcess"));
		}
		else
		{
			if ( ! TerminateProcess( hTray, 0 ) )
				PrintError(TEXT("TerminateProcess"));
			CloseHandle(hTray);
		}
	}

	// (Re)start the tray process
	
	memset( (void*)&si, 0, sizeof(STARTUPINFO) );
	si.cb = sizeof(STARTUPINFO);
	memset( (void*)&pi, 0, sizeof(PROCESS_INFORMATION) );

	if ( ! CreateProcess( szPath, NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi ))
	{
		DWORD ErrCode = GetLastError();
		PrintError(TEXT("CreateProcess"));
		return ErrCode;
	}

	CloseHandle( pi.hThread );
	CloseHandle( pi.hProcess );

	return 0;
}

BOOL FindTrayProcess( DWORD& pid )
{
	HANDLE hProcessSnap;
	PROCESSENTRY32 pe32;

	// Take a snapshot of all processes in the system.
	hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
	if( hProcessSnap == INVALID_HANDLE_VALUE )
	{
		PrintError( TEXT("CreateToolhelp32Snapshot") );
		return( FALSE );
	}

	// Set the size of the structure before using it.
	pe32.dwSize = sizeof( PROCESSENTRY32 );

	// Retrieve information about the first process,
	// and exit if unsuccessful
	if( !Process32First( hProcessSnap, &pe32 ) )
	{
		PrintError( TEXT("Process32First") ); // show cause of failure
		CloseHandle( hProcessSnap );          // clean the snapshot object
		return( FALSE );
	}

	// Now walk the snapshot of processes, and
	// display information about each process in turn
	do
	{
		if (_tcsicmp( pe32.szExeFile, TEXT("mpxptray.exe") ) == 0 )
		{
			pid = pe32.th32ProcessID;
			CloseHandle( hProcessSnap );
			return( TRUE );
		}
	} while( Process32Next( hProcessSnap, &pe32 ) );

	CloseHandle( hProcessSnap );
	return( FALSE );
}

void PrintError( TCHAR* msg )
{
	DWORD eNum;
	TCHAR sysMsg[256];
	TCHAR* p;

	eNum = GetLastError( );
	FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
		 NULL, eNum,
		 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		 sysMsg, 256, NULL );

	// Trim the end of the line and terminate it with a null
	p = sysMsg;
	while( ( *p > 31 ) || ( *p == 9 ) )
	++p;
	do { *p-- = 0; } while( ( p >= sysMsg ) &&
						  ( ( *p == '.' ) || ( *p < 33 ) ) );

	// Display the message
	_tprintf( TEXT("\n  WARNING: %s failed with error %d (%s)"), msg, eNum, sysMsg );
}


You'll need to include tlhelp32.h for the process snapshot. Hope this helps Smile | :)

Best Regards,
MicroVirus
GeneralRe: endtask program using win32api/c like taskmanager to close programs without invoking taskmanager just in one shot Pin
Mark Salsbery26-May-11 11:01
Mark Salsbery26-May-11 11:01 
AnswerRe: endtask program using win32api/c like taskmanager to close programs without invoking taskmanager just in one shot Pin
Jayapal Chandran26-May-11 6:10
Jayapal Chandran26-May-11 6:10 
QuestionHow to use help file Pin
MKC00225-May-11 7:06
MKC00225-May-11 7:06 
QuestionRe: How to use help file Pin
David Crow25-May-11 7:49
David Crow25-May-11 7:49 
AnswerRe: How to use help file Pin
CPallini25-May-11 21:51
mveCPallini25-May-11 21:51 
AnswerRe: How to use help file Pin
Mark Salsbery25-May-11 7:51
Mark Salsbery25-May-11 7:51 
AnswerRe: How to use help file Pin
Hans Dietrich25-May-11 17:04
mentorHans Dietrich25-May-11 17:04 
AnswerRe: How to use help file Pin
వేంకటనారాయణ(venkatmakam)25-May-11 19:19
వేంకటనారాయణ(venkatmakam)25-May-11 19:19 
QuestionEdit message in CListCtrl Pin
_Flaviu25-May-11 1:53
_Flaviu25-May-11 1:53 
AnswerRe: Edit message in CListCtrl Pin
Richard MacCutchan25-May-11 2:48
mveRichard MacCutchan25-May-11 2:48 
AnswerRe: Edit message in CListCtrl Pin
Malli_S25-May-11 3:14
Malli_S25-May-11 3:14 
AnswerRe: Edit message in CListCtrl Pin
వేంకటనారాయణ(venkatmakam)25-May-11 3:23
వేంకటనారాయణ(venkatmakam)25-May-11 3:23 
GeneralRe: Edit message in CListCtrl Pin
_Flaviu25-May-11 6:16
_Flaviu25-May-11 6:16 
QuestionWhile going from Paused to Stopped, the Service Hangs at Stopping Pin
SunilKrSingh25-May-11 0:03
SunilKrSingh25-May-11 0:03 
QuestionRe: While going from Paused to Stopped, the Service Hangs at Stopping Pin
Malli_S25-May-11 3:19
Malli_S25-May-11 3:19 
QuestionRe: While going from Paused to Stopped, the Service Hangs at Stopping Pin
Mark Salsbery25-May-11 7:03
Mark Salsbery25-May-11 7:03 
AnswerRe: While going from Paused to Stopped, the Service Hangs at Stopping Pin
jschell25-May-11 10:10
jschell25-May-11 10:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.