CreateProcess auf EXE-Datei trotz geöffneten DateiHandle



  • Hallo,
    ich muß umbedingt irgendwie einen Prozeß öffnen, also eine EXE-Datei ausführen, obwohl auf die Datei ein offenes Handle ist.
    Ich betone nochmal, das Dateihandle MUSS offen sein, da sonst die Datei sofort gelöscht wird und das soll erst nach dem Beenden des Prozeßes erfolgen.
    Wie kann ich trotz dieses verdammten Handles die EXE-Datei starten?

    Hier der Code:

    void __fastcall TFormSetup::ButtonDeinstallierenClick(TObject *Sender)
    {
    	// Variablen
    	UnicodeString usProgDir, usDelFile, usSetupOrig, usSetupTmp;
    	WideString wsProgDir;
    	char cRegServ[MAX_PATH], cParameter[MAX_PATH];
    	UnicodeString usRegServ, usParameter;
    	AnsiString asRegServ, asParameter;
    	TShellExecuteInfoA ShExecInfo;
    	STARTUPINFO si;
    	PROCESS_INFORMATION pi;
    	wchar_t wc_Setup[MAX_PATH];
    	DWORD dwProcessId;
    	HANDLE hToken, hSetupCopy;
    	void* lpBuffer;
    	UCHAR uca_SetupEXE[102400]; // 100 KB
    	UCHAR uc_SetupEXE[1]; // 1n Byte
    	ifstream fSourceDat;
    	fstream fInstallDat;
    	int iCount1, iCount2, iLoops, iRest;
    	unsigned long ul_written, ul_SetupLength;
    
    	// Code
    	// Datenbankprovider Registrierung aufheben
    	memset(cRegServ, '\0', MAX_PATH);
    
    	usRegServ = L"regsvr32.exe";
    	asRegServ = usRegServ.c_str();
    
    	usParameter = L" \"";
    	usParameter += usProgDir;
    	usParameter += L"\\ /u _IBProvider_v3_vc9_i.dll\"";
    	asParameter = usParameter.c_str();
    
    	strcpy(cRegServ, asRegServ.c_str());
    	strcpy(cParameter, asParameter.c_str());
    	ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    	ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    	ShExecInfo.hwnd = Application->Handle;
    	ShExecInfo.lpVerb = NULL;
    	ShExecInfo.lpFile = cRegServ;
    	ShExecInfo.lpParameters = cParameter;
    	ShExecInfo.lpDirectory = NULL;
    	ShExecInfo.nShow = SW_SHOWNORMAL;
    	ShExecInfo.hInstApp = NULL;
    
    	///////////////////////////////////////////////////////////////////////////
    	// Setup löscht sich selbst über die WIN-API
    	///////////////////////////////////////////////////////////////////////////
    //http://www.catch22.net/tuts/self-deleting-executables
    //https://www.tutorials.de/threads/deletefile-sich-selbst-loeschen.239256/
    // http://stackoverflow.com/questions/1583040/createfile-with-the-file-flag-delete-on-close-flag
    // https://social.msdn.microsoft.com/Forums/vstudio/en-US/d0c68c5a-7b9e-4fdc-ba74-96f8e98204b7/createfile-using-fileflagdeleteonclose?forum=vclanguage
    //https://technet.microsoft.com/de-de/sysinternals/aa364221
    //https://msdn.microsoft.com/en-us/library/windows/desktop/aa365539(v=vs.85).aspx
    //usSetupOrig, usSetupTmp;
    	usSetupOrig = usProgDir + "Setup.exe";
    	usSetupTmp = usProgDir + "SetupTmp.exe";
    
    	usDelFile = usProgDir + "SetupTmp.exe";
    	ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
    	ZeroMemory( &pi, sizeof(pi) );
    //	CreateFile(
    // FILE_FLAG_DELETE_ON_?CLOSE ... FILE_SHARE_DELETE
    //DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
    //DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE;
    //HANDLE hFile = ::CreateFile(strTempFile.c_str(), GENERIC_WRITE, dwShareMode, &sa, CREATE_NEW, dwFlagsAndAttributes, NULL);
    
    	hSetupCopy = CreateFile(PChar(usSetupTmp.c_str()),
    							GENERIC_READ|GENERIC_WRITE,
    							0,
    							NULL,
    							CREATE_NEW,
    							FILE_FLAG_DELETE_ON_CLOSE,
    							NULL);
    //	hSetupCopy = CreateFile(PChar(usSetupTmp.c_str()),
    //							GENERIC_READ|GENERIC_WRITE,
    //							FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
    //							NULL,
    //							CREATE_NEW,
    //							FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
    //							NULL);
    //	hSetupCopy =  CreateFile(PChar(usSetupTmp.c_str()),
    //							GENERIC_READ|GENERIC_WRITE,
    //							0,
    //							NULL,
    //							CREATE_NEW,
    //							FILE_ATTRIBUTE_NORMAL,
    //							NULL);
    	if (hSetupCopy ==((HANDLE)-1)) {
    		  FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
    		  NULL,
    		  ::GetLastError(),
    		  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    		  (LPTSTR) &lpBuffer,
    		  0,
    		  NULL );
    		  return;
    	}
    	fInstallDat.open(usSetupTmp.c_str(), ios::out | ios::binary);
    	fInstallDat.seekg(0, ios_base::beg);
    	fSourceDat.open(usSetupOrig.c_str(), ios::in | ios::binary);
    	fSourceDat.seekg(0, ios_base::end);
    	ul_SetupLength = fSourceDat.tellg();
    	fSourceDat.seekg(0, ios_base::beg);
    	iCount1 = 0;
    	while (iCount1 < ul_SetupLength) {
    		iCount1 = iCount1 + 102400;
    		fSourceDat.read(uca_SetupEXE, 102400); // 100 Kilobyte
    		//fInstallDat.write(uca_SetupEXE, 102400);
    		if (!WriteFile( hSetupCopy, &uca_SetupEXE, sizeof(uca_SetupEXE), &ul_written, 0 )) {
    			return;
    		}
    		if (SetupSize - iCount1 < 102400) {
    			for (iRest = iCount1; iRest < ul_SetupLength; iRest++) {
    				fSourceDat.read(uc_SetupEXE, 1);
    				//fInstallDat.write(uc_SetupEXE, 1);
    				if (!WriteFile( hSetupCopy, &uc_SetupEXE, 1, &ul_written, 0 )) {
    					return;
    				}
    			}
    			iCount1 = iRest;
    		}
    	}; // Ende While
    
    	fSourceDat.close();
    	fSourceDat.clear();
    	fInstallDat.close();
    	fInstallDat.clear();
    
       if (!CreateProcess(NULL, PChar(usSetupTmp.c_str()),
    					  NULL,
    					  NULL,
    					  FALSE,
    					  0,
    					  NULL,
    					  NULL,
    					  &si,
    		 			  &pi )) {
    		  FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
    		  NULL,
    		  ::GetLastError(),
    		  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    		  (LPTSTR) &lpBuffer,
    		  0,
    		  NULL );
    	}
    	Application->Terminate();
    }
    //---------------------------------------------------------------------------
    


  • skynet2016 schrieb:

    ich muß umbedingt irgendwie einen Prozeß öffnen, also eine EXE-Datei ausführen, obwohl auf die Datei ein offenes Handle ist.
    Ich betone nochmal, das Dateihandle MUSS offen sein, da sonst die Datei sofort gelöscht wird

    Blöd aber auch.
    Musst du jetzt ins Gefängnis?


Anmelden zum Antworten