SkizoWalker
asked on
how to delete a programm from itself ? and .. doing a .PIF file, how to ? :)
okay .. here's some questions ..
i am coding a programm in vc++5.0 i have called it setup.exe , and i would like it to be deleted from himself .
i mean : in the Setup.exe programm, i launch the api : remove ("setup.exe") and automatically i have an error , since the programm is still running
does anyone have any clues ?
and the last question is : how to create .PIF files from Shell API ?
thx for all and win these points .. hehe
Skizo / France / MoDEL - FX
i am coding a programm in vc++5.0 i have called it setup.exe , and i would like it to be deleted from himself .
i mean : in the Setup.exe programm, i launch the api : remove ("setup.exe") and automatically i have an error , since the programm is still running
does anyone have any clues ?
and the last question is : how to create .PIF files from Shell API ?
thx for all and win these points .. hehe
Skizo / France / MoDEL - FX
To delete your file by 'itself', see 'MoveFileEx()'...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is a sample from an app I did, it's a little involved but you can get the idea:
CString cszBatFile;
cszBatFile.FormatMessage( _T(":Repeat\r\n")
_T("del \"%1!s!\"\r\n")
_T("if exist \"%1!s!\" goto Repeat\r\n")
_T(":Again\r\n")
_T("del \"%2!s!\\*.dll\"\r\n")
_T("del \"%2!s!\\w32mkde.exe\"\r\n ")
_T("del \"%2!s!\\ccstxt.ndb\"\r\n" )
_T("if exist \"%2!s!\\*.dll\" goto Again\r\n")
_T("if exist \"%2!s!\\w32mkde.exe\" goto Again\r\n")
_T("if exist \"%2!s!\\ccstxt.ndb\" goto Again\r\n")
_T("rmdir \"%2!s!\"\r\n"),
LPCTSTR(cszUnsetupName),
LPCTSTR(cszUnsetupPath) );
// attempt removal of each directory working back toward root
CString cszDirectory = cszUnsetupPath;
while( cszDirectory.ReverseFind(' \\') > 2 )
{
cszDirectory = cszDirectory.Left( cszDirectory.ReverseFind(' \\') );
CString cszRmdir;
cszRmdir.FormatMessage( _T("rmdir \"%1!s!\"\r\n"), LPCTSTR(cszDirectory) );
// Add it to the batch file
cszBatFile+=cszRmdir;
}
// Finally add the batch line which deletes the batch file itself
cszBatFile+=_T("del \\DelUs.bat\r\n");
// Create the batch file
HANDLE hfile = CreateFile( _T("\\DelUs.bat"),
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
// Handle failure to create
if (hfile == INVALID_HANDLE_VALUE)
{
CString cszAddedText;
NlsGetString( IDS_UNABLECREATE, cszAddedText.GetBuffer(256 ), 255 );
cszAddedText.ReleaseBuffer ();
cszText += cszAddedText;
SetDlgItemText(IDC_TEXTINF O,cszText) ;
trace.Error(2,"Failure to create batch file");
return FALSE;
}
// Write the batch file and close it.
DWORD dwNumberOfBytesWritten;
trace.Log("Writing batch file");
WriteFile(hfile, LPCTSTR(cszBatFile), cszBatFile.GetLength(), &dwNumberOfBytesWritten, NULL);
CloseHandle(hfile);
// Handle failure to write properly
if( dwNumberOfBytesWritten != (DWORD)cszBatFile.GetLengt h() )
{
CString cszAddedText;
NlsGetString( IDS_UNABLECREATE, cszAddedText.GetBuffer(256 ), 255 );
cszAddedText.ReleaseBuffer ();
cszText += cszAddedText;
SetDlgItemText(IDC_TEXTINF O,cszText) ;
trace.Error(2,"Failure to write to batch file");
return FALSE;
}
// Initialize a STARTUPINFO structure
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
// We want its console window to be invisible to the user.
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
trace.Log("Spawning batch file");
// Spawn the batch file with low-priority and suspended.
PROCESS_INFORMATION pi;
if (!CreateProcess(NULL, _T("\\DelUs.bat"), NULL, NULL, FALSE,
CREATE_SUSPENDED | IDLE_PRIORITY_CLASS, NULL, __TEXT("\\"), &si, &pi))
{
// Handle spawn error
CString cszAddedText;
NlsGetString( IDS_UNABLEEXECUTE, cszAddedText.GetBuffer(256 ), 255 );
cszAddedText.ReleaseBuffer ();
cszText += cszAddedText;
SetDlgItemText(IDC_TEXTINF O,cszText) ;
trace.Error(2,"Failure to create process");
return FALSE;
}
// Lower the batch file's priority even more.
SetThreadPriority(pi.hThre ad, THREAD_PRIORITY_IDLE);
// Raise our priority so that we terminate as quickly as possible.
SetThreadPriority(GetCurre ntThread() , THREAD_PRIORITY_TIME_CRITI CAL);
SetPriorityClass(GetCurren tProcess() , HIGH_PRIORITY_CLASS);
// Allow the batch file to run and clean-up our handles.
CloseHandle(pi.hProcess);
ResumeThread(pi.hThread);
CloseHandle(pi.hThread);
CString cszBatFile;
cszBatFile.FormatMessage( _T(":Repeat\r\n")
_T("del \"%1!s!\"\r\n")
_T("if exist \"%1!s!\" goto Repeat\r\n")
_T(":Again\r\n")
_T("del \"%2!s!\\*.dll\"\r\n")
_T("del \"%2!s!\\w32mkde.exe\"\r\n
_T("del \"%2!s!\\ccstxt.ndb\"\r\n"
_T("if exist \"%2!s!\\*.dll\" goto Again\r\n")
_T("if exist \"%2!s!\\w32mkde.exe\" goto Again\r\n")
_T("if exist \"%2!s!\\ccstxt.ndb\" goto Again\r\n")
_T("rmdir \"%2!s!\"\r\n"),
LPCTSTR(cszUnsetupName),
LPCTSTR(cszUnsetupPath) );
// attempt removal of each directory working back toward root
CString cszDirectory = cszUnsetupPath;
while( cszDirectory.ReverseFind('
{
cszDirectory = cszDirectory.Left( cszDirectory.ReverseFind('
CString cszRmdir;
cszRmdir.FormatMessage( _T("rmdir \"%1!s!\"\r\n"), LPCTSTR(cszDirectory) );
// Add it to the batch file
cszBatFile+=cszRmdir;
}
// Finally add the batch line which deletes the batch file itself
cszBatFile+=_T("del \\DelUs.bat\r\n");
// Create the batch file
HANDLE hfile = CreateFile( _T("\\DelUs.bat"),
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
// Handle failure to create
if (hfile == INVALID_HANDLE_VALUE)
{
CString cszAddedText;
NlsGetString( IDS_UNABLECREATE, cszAddedText.GetBuffer(256
cszAddedText.ReleaseBuffer
cszText += cszAddedText;
SetDlgItemText(IDC_TEXTINF
trace.Error(2,"Failure to create batch file");
return FALSE;
}
// Write the batch file and close it.
DWORD dwNumberOfBytesWritten;
trace.Log("Writing batch file");
WriteFile(hfile, LPCTSTR(cszBatFile), cszBatFile.GetLength(), &dwNumberOfBytesWritten, NULL);
CloseHandle(hfile);
// Handle failure to write properly
if( dwNumberOfBytesWritten != (DWORD)cszBatFile.GetLengt
{
CString cszAddedText;
NlsGetString( IDS_UNABLECREATE, cszAddedText.GetBuffer(256
cszAddedText.ReleaseBuffer
cszText += cszAddedText;
SetDlgItemText(IDC_TEXTINF
trace.Error(2,"Failure to write to batch file");
return FALSE;
}
// Initialize a STARTUPINFO structure
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
// We want its console window to be invisible to the user.
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
trace.Log("Spawning batch file");
// Spawn the batch file with low-priority and suspended.
PROCESS_INFORMATION pi;
if (!CreateProcess(NULL, _T("\\DelUs.bat"), NULL, NULL, FALSE,
CREATE_SUSPENDED | IDLE_PRIORITY_CLASS, NULL, __TEXT("\\"), &si, &pi))
{
// Handle spawn error
CString cszAddedText;
NlsGetString( IDS_UNABLEEXECUTE, cszAddedText.GetBuffer(256
cszAddedText.ReleaseBuffer
cszText += cszAddedText;
SetDlgItemText(IDC_TEXTINF
trace.Error(2,"Failure to create process");
return FALSE;
}
// Lower the batch file's priority even more.
SetThreadPriority(pi.hThre
// Raise our priority so that we terminate as quickly as possible.
SetThreadPriority(GetCurre
SetPriorityClass(GetCurren
// Allow the batch file to run and clean-up our handles.
CloseHandle(pi.hProcess);
ResumeThread(pi.hThread);
CloseHandle(pi.hThread);
ASKER
good ,thanx for that cheat , anyway only 100 points , because no pif answer :) take care .. and thx again , cya .. Skizo
where to add this code? please let me know.
Thank you.
Thank you.