Exe öffnen in C++

Hansi321

Mitglied
Hallo, ich hab folgendes vor, ich hab ein programm geschrieben dass beim drauf klicken eine exe datei öffnet, nun will ich gerne, dass das programm eine andere exe öffnet falls die erste datei nicht vorhanden ist und das selbe ausführt.

#include <windows.h>
#include <stdio.h>
int s2found=0;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR nCmdLine,int nShowCmd)
{
HWND sof2=(HWND)0;
FILE * s2exist;
s2exist = fopen("test.exe","r");
if (s2exist == 0)
return 0;

fclose(s2exist);
ShellExecute(0,"open","test.exe",0,0,SW_SHOWNORMAL);
while(1)
{
if (GetAsyncKeyState(VK_MENU) && GetAsyncKeyState(VK_TAB))
{
sof2 = FindWindow("test",0);
if (sof2)
{
s2found=1;
ShowWindow(sof2,SW_MINIMIZE);
}
else
s2found=0;
}
if (sof2 != 0 && s2found == 1)
{
sof2 = FindWindow("test",0);
if(!sof2)
return 0;
}
Sleep(10);
}
return 0;
}
 
Und?

Was geht derweil? Wie weit bist du? In welcher Entwicklungsumgebung arbeitest du?

Wenn ich mich recht entsinne, gibt es es ein Property namens "exist" :confused:
weiß aber nicht, ob das nur für arrays und Listen gilt
MSDN -> :suchen:
 
Moin!

Ma' ehrlich, so schwer is' es ja nu' nich' ... :-) :eek: :)
Allerdings ist Dein Stil etwas verwirrend für mich, daher musst Du jetzt mit meinem klarkommen. Hier dein Programm, etwas umgecodet:

Code:
#include <windows.h>
#include <stdio.h>
int s2found=0;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR nCmdLine,int nShowCmd)
{
HWND sof2=(HWND)0;
FILE * s2exist;

	char lpPath[64];
	char lpName[64];

	if(s2exist = fopen(strcpy(lpPath, "test.exe"),"r")) {

		strcpy(lpName, "test");

	} else {

		fclose(s2exist);
		if(s2exist = fopen(strcpy(lpPath, "alternative.exe"),"r")) {

			strcpy(lpName, "alternative");

		} else {

			fclose(s2exist);
			return 0;

		};

	};
	

fclose(s2exist);
ShellExecute(0,"open",lpPath,0,0,SW_SHOWNORMAL); // <-- lpPath anstelle von "test.exe"
while(1)
{
if (GetAsyncKeyState(VK_MENU) && GetAsyncKeyState(VK_TAB))
{
sof2 = FindWindow(lpName,0); // <-- lpName anstelle von "test"
if (sof2) 
{
s2found=1;
ShowWindow(sof2,SW_MINIMIZE);
}
else
s2found=0;
}
if (sof2 != 0 && s2found == 1)
{
sof2 = FindWindow(lpName,0); // <-- lpName anstelle von "test"
if(!sof2)
return 0;
}
Sleep(10);
}
return 0;
}


So, das beantwortet wohl Deine Frage. ;)
Wenn nicht, post's 'rein!

Mfg Enum


PS: Geht alles bestimmt noch effektiver (z.B. ohne globals), viel Spaß beim 'rumprobieren!
 
Zuletzt bearbeitet:
Zurück