Results 1 to 10 of 10

Thread: App executex too quick to get hWnd..

  1. #1

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720

    App executex too quick to get hWnd..

    I am making a program, when you run it, it finds hWnd of winAmp, and if its 0, itll ask the user 'winamp is not open, wanna open it, Y/N' if the user tYpes Y, it opens and then it finds hwnd again, but its too fast, so the hwnd is always 0, it tries to find it before winamp is fully open. How would I fix this? or go around it?
    asdf

  2. #2
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Can you post the code in question?

  3. #3

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    Is there anyway to make the system 'wait' until a certain file has finished loading?

    code:

    Code:
    #include <iostream.h>
    #include <windows.h>
    
    class WinAmp
    {
    public:
    	WinAmp();
    	~WinAmp();
    	HWND gethwnd()
    	{
    		hwnd_winamp = FindWindow("Winamp v1.x", NULL);
    		return hwnd_winamp;
    	}
    	void play(){SendMessage(hwnd_winamp, WM_COMMAND, 40045, NULL);}
    	void stop(){SendMessage(hwnd_winamp, WM_COMMAND, 40047, NULL);}
    	void pause(){SendMessage(hwnd_winamp, WM_COMMAND, 40046, NULL);}
    	void prev(){SendMessage(hwnd_winamp, WM_COMMAND, 40044, NULL);}
    	void next(){SendMessage(hwnd_winamp, WM_COMMAND, 40048, NULL);}
    	//void GetSongTitle(){}
    	void close(){SendMessage(hwnd_winamp, WM_COMMAND, 40001, NULL);}
    private:
    	HWND hwnd_winamp;
    };
    
    WinAmp::~WinAmp(){}
    WinAmp::WinAmp(){}
    
    void WinAmpMenu();
    
    int main()
    {
    	WinAmp objWA;
    	HWND WA_hWnd = objWA.gethwnd();
    
    	if(WA_hWnd == 0)
    	{
    		cout << "WinAmp is closed, want to open? Y/N\n";
    		char OWA='X';
    
    		cin >> OWA;
    		if (OWA == 'Y')
    		{
    			HINSTANCE ret = ShellExecute(NULL, "open", "C:\\Program Files\\Winamp\\winamp.exe", NULL, NULL, SW_SHOWNORMAL);
    		}
    		else
    		{
    			return(0);
    		}
    	}
    	
    	WinAmpMenu();
    	char CHOE = 'D';
    	do
    	{
    		cout << "Enter a letter\n";
    		cin >> CHOE;
    		switch(CHOE)
    		{
    			case 'P':
    				objWA.play();
    				break;
    			case 'Z':
    				objWA.pause();
    				break;
    			case 'S':
    				objWA.stop();
    				break;
    			case 'C':
    				objWA.close();
    				break;
    			case 'V':
    				objWA.prev();
    				break;
    			case 'N':
    				objWA.next();
    				break;
    			case 'W':
    				cout << "hWnd: " << objWA.gethwnd() << endl; //lets find the hWnd
    				break;
    			case 'M':
    				WinAmpMenu();
    				break;
    			case 'X':
    				return(0);
    		default:
    			cout << "Unknown Letter, press M to view the menu again! -_0\n";
    		}
    	} while(CHOE != 'X');
    	return(0);
    }
    
    void WinAmpMenu()
    {
    	cout << "\n\nWinAmp menu ****`_`\n\n"
    	<<"-------------------\n"
    	<<"P - Play\tZ - Pause\n"
    	<<"S - Stop\tC - Close\n"
    	<<"V - Prev\tN - Next\n"
    	<<"M - Menu\tW - hWnd\n\n"
    	<<"-------------------\n";
    }
    asdf

  4. #4
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Code:
    HWND gethwnd()
    {
    hwnd_winamp = FindWindow("Winamp v1.x", NULL);
    return hwnd_winamp;
    }
    Are you really sure the window name is "Winamp v1.x"? Use MS spy++ to find out to double confirm.

    Spy++ comes with every copy of VC6.

  5. #5
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    Sleep() is the API call your looking for get the info for it on MSDN.

  6. #6

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    Yes transcendetal, it is that classname, I can control the window fine but my prog looks for it too quick.

    Thanks WiKiDJeFF.
    asdf

  7. #7
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    If you want more control, oyu can use GetTickCount instead of sleep, the only problem is you will have to do more coding. Sleep holds the program up for a set time. GetTickCount returns the hnumber of miliseconds your computer has been running for, and thus works as a timer of sorts when used properly.

  8. #8

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    I know about GetTickCount, I need to find out if a program loaded or not yet.. how would I? The prog loads in like 1 sec on my PC, but like 10 seconds on my other/slow PC.. so the same timer won't work.
    asdf

  9. #9
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    howza bout this...

    Code:
    	// ..........................
    
    	if(WA_hWnd == 0)
    	{
    		cout << "WinAmp is closed, want to open? Y/N\n";
    		char OWA='X';
    
    		cin >> OWA;
    		if (OWA == 'Y')
    		{
    			HINSTANCE ret = ShellExecute(NULL, "open", "C:\\Program Files\\Winamp\\winamp.exe", NULL, NULL, SW_SHOWNORMAL);
    			while ((WA_hWnd = objWA.gethwnd()) == 0)
    			{
    				cout << "Waiting for winamp to start..." << endl;
    				Sleep(500);
    			}
    
    		}
    		else
    		{
    			return(0);
    		}
    	}
    
    	// ..........................

    Oh, and the Winamp 3 classname is "STUDIO" in case you were wondering, although none of these functions work with v3.
    Last edited by sunburnt; Apr 8th, 2003 at 04:56 PM.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Use CreateProcess instead of ShellExecute, then you can use the process handle you've got with WaitForInputIdle (or something like that) which locks you app until WinAmp has loaded.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width