What's wrong with my code?

Hey guys
My english isn't the best, so I'll try only the best ;)
I've written the following code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdlib>
#include <iostream>
#include <windows.h>


using namespace std;

int main(int argc, char *argv[])
{


ShellExecute(NULL,"Open","www.google.de",NULL ,NULL,1);
HWND hConsoleWnd = GetConsoleWindow();

system("PAUSE");
 
DestroyWindow(hConsoleWnd);
 
 

    system("PAUSE");
    return EXIT_SUCCESS;
}

I wanna open a website an close it after it've opened.
I can compile the code with Dev C++, but when I start the programm it just start the website but doesn't close after.
What do I wrong?
Thx for helping :)
Last edited on
You don't need to destroy the console window.
You are adding a pause after running the website. Remove the pause.

Remove the pause in the end once you are done debugging.
debugging works now fine :)
But there's still the problem that the website just opened but doesn't close :(
Do you want the website opened and closed, or do you want to open the website, then close the console application that opened the website?
I wanna the website opened and closed
Then your current method is not correct. You must change ShellExecute with CreateProcess() to receive a process handle. You'll need it.

After the process is started, you wait for its message pump to be idle with WaitForInputIdle() (http://msdn.microsoft.com/en-us/library/windows/desktop/ms687022(v=vs.85).aspx ). Once it is done waiting, you can safely assume you can ask the main window to close. Find the main window using EnumWindows() and then using (in the callback function) GetWindowThreadProcessId(). If you get a window with the same thread and process id of those returned by CreateProcess(), you have found the window (at least one of them anyway). Use the window handle in a call to PostMessage(); the message to send: WM_CLOSE. Repeat for any other matching window, in case the browser opened multiple windows.

Finally wait on the process handle using WaitForSingleObject(). If after some X amount of time the process hasn't finished, terminate it using TerminateProcess().

That would be my take on this.
Ok
I've written this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <cstdlib>
#include <iostream>
#include <windows.h>


using namespace std;

int main(int argc, char *argv[])
{
	BOOL CALLBACK TheWebsite(HWND hWnd, LPARAM lParam) 
	{

CreateProcess(NULL, "C://scheiss//iexplore2.exe", "www.google.com");

DWORD WINAPI WaitForInputIdle(
  __in  HANDLE hProcess,
  __in  DWORD dwMilliseconds
);

	}

EnumWindows(TheWebsite); 

GetWindowThreadProcessId(TheWebsite);

PostMessage(WM_CLOSE);
  
    return EXIT_SUCCESS;
}


I'm just beginner in C++ programming. So I don't know if this code is just a sh*t ;)
Visual C++ say while debugging:

error C2601: 'TheWebsite': Local function definitions are illegal (9): This line contains a '{', which has no equivalent

error C2660: 'EnumWindows': function doesn't accept 1 argument

error C2660: 'GetWindowThreadProcessId':function doesn't accept 1 argument

error C2660: 'PostMessageA': function doesn't accept 1 argument

error C2660: 'CreateProcessA': function doesn't accept 3 arguments
So, now I've tried another thing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdlib>
#include <iostream>
#include <windows.h>


using namespace std;

int main(int argc, char *argv[])
{


ShellExecute(NULL,"Open","www.google.de",NULL ,NULL,1);
 
 
system("taskkill /f /im C://Program Files//Internet Explorer//iexplore.exe");
 
 

    system("PAUSE");
    return EXIT_SUCCESS;
}

I wanna open the website google and close them later. Debugging works fine, but when I start the application it open the website but doesn't close. It display the message:
Argument/Option illegal - "Files/Internet". Put in "Taskkill/?" to show the Syntax.
What do I wrong? :(
You better use WinInet API to open the website instead of launching internet explorer.
Ok, now I've tried to write the code with WinAPI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <Windows.h>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{

WinExec("explorer http://google.com", SW_SHOW);

HWND hWnd = ::GetConsoleWindow( ) ;

PostMessage(hWnd, WM_CLOSE, 0, 0 ) ;


    system("PAUSE");
    return EXIT_SUCCESS;
}


It debuggs and start the programm. But there are a lot of fault messages:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"yt1.exe": "C:\Windows\SysWOW64\ntdll.dll" geladen, Cannot find or open the PDB file
"yt1.exe": "C:\Windows\SysWOW64\kernel32.dll" geladen, Cannot find or open the PDB file
"yt1.exe": "C:\Windows\SysWOW64\KernelBase.dll" geladen, Cannot find or open the PDB file
"yt1.exe": "C:\Windows\SysWOW64\user32.dll" geladen, Cannot find or open the PDB file
"yt1.exe": "C:\Windows\SysWOW64\gdi32.dll" geladen, Cannot find or open the PDB file
"yt1.exe": "C:\Windows\SysWOW64\lpk.dll" geladen, Cannot find or open the PDB file
"yt1.exe": "C:\Windows\SysWOW64\usp10.dll" geladen, Cannot find or open the PDB file
"yt1.exe": "C:\Windows\SysWOW64\msvcrt.dll" geladen, Cannot find or open the PDB file
"yt1.exe": "C:\Windows\SysWOW64\advapi32.dll" geladen, Cannot find or open the PDB file
"yt1.exe": "C:\Windows\SysWOW64\sechost.dll" geladen, Cannot find or open the PDB file
"yt1.exe": "C:\Windows\SysWOW64\rpcrt4.dll" geladen, Cannot find or open the PDB file
"yt1.exe": "C:\Windows\SysWOW64\sspicli.dll" geladen, Cannot find or open the PDB file
"yt1.exe": "C:\Windows\SysWOW64\cryptbase.dll" geladen, Cannot find or open the PDB file
"yt1.exe": "C:\Windows\SysWOW64\msvcr100d.dll" geladen, Symbole wurden geladen.
"yt1.exe": "C:\Windows\SysWOW64\imm32.dll" geladen, Cannot find or open the PDB file
"yt1.exe": "C:\Windows\SysWOW64\msctf.dll" geladen, Cannot find or open the PDB file
"yt1.exe": "C:\Windows\SysWOW64\apphelp.dll" geladen, Cannot find or open the PDB file

geladen = loaded ;)
When the programm starts, it just start the website but doens't close the website :(
What do I wrong?
That are warning messages, if you want to dissapear then setup Visual Studio to use Microsoft symbol servers.
It's not a problem, those 'errors' are there because, if you had those PDB files, you could get your OS's Source Code, thing Microsoft does not want to happen at all.
Hmmm ok
But what is wrong with my code ? :(
Still you're not understanding much of what you are doing, i see.
1
2
HWND hWnd = ::GetConsoleWindow( ) ;
PostMessage(hWnd, WM_CLOSE, 0, 0 ) ;

You are trying to close the console. No matter WinExec.
webJose wrote:
Then your current method is not correct. You must change ShellExecute with CreateProcess() to receive a process handle. You'll need it.

After the process is started, you wait for its message pump to be idle with WaitForInputIdle()

Stick to above thing.
Use CreateProcess, ShellExecute, WaitForInputIdle...
Many time harder things are the best things.
Topic archived. No new replies allowed.