Link to home
Start Free TrialLog in
Avatar of BI5HOP
BI5HOP

asked on

Delete on reboot

Can anyone explain how to delete a file on reboot/restart

I have a listbox (lstfiles) and i search for files by there extenstions (.txt) returns all text files on the disk and loads them into my listbox. I doubleclick on one and it opens i added a command button (cmd3) to delete a file I set the file attributes to
disable readonly and hidden then try to run the kill function if the file will not delete i want to remove it on next startup. Thats the part im at now. I have read about movefileex but i dont seem to be able to get it working.

Any help?
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

you can create an entry in Registry in RunOnce key:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce

The command you insert in the registry will be executed next time, could be a simple command prompt order.
Avatar of BI5HOP
BI5HOP

ASKER

yea but if the file loads first it wont remove it :( some programs (spyware) start from other places
Avatar of BI5HOP

ASKER

This one could do it the only problem is how to knw what process your terminating

Private Sub Command1_Click()
    For Each Process In GetObject("winmgmts:").ExecQuery("select * from Win32_Process where Name='app.exe'") ' app.exe is the processname ( Usually the exe name )
        Process.Terminate
    Next
    Doevents
    Kill "c:\app.exe" ' Full path must be specified here
End Sub


since a file i am deleting could be .dll .exe .txt .anything
dll files that need deleting dont run under any specific name thats why i thought deleting on reboot is best.
HKLM:
\SYSTEM\CurrentControlSet\Control\Session Manager
Value:  PendingFileRenameOperations
ASKER CERTIFIED SOLUTION
Avatar of cookre
cookre
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BI5HOP

ASKER

Yea thats the key i have been reading about for movefileex but i dont seem to be doing it write is there an example of how it works? I looked at my key and it is empty so i can get an example for there.
Avatar of BI5HOP

ASKER

Ok i got most of it but i need to know how i can make the below write as MULTISZ instead of REGSZ

Public Function RegWrite(Key1, svalue As String)
Dim WSHShell As Variant
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.RegWrite Key1, svalue

End Function
Private Sub Command2_Click()
Dim svalue As String
svalue = dfile.Text
RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations", "\??\" + svalue
End Sub
Well, that's the trick.
Alas, I'm not enough of a VBer to know without fiddling around a bit how to get the nulls embedded at the right spots.  

Although WSHShell.RegWrite has an optional 3rd parm that let's you specify data type, REG_MULTI_SZ is not among the supported types.  Now, the .NET value set method will look at the data and try to figure out its type, but you still have to get those nulls in there.

Also, you should check to see if the value already exists so you can add your text at the end of what's there already.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BI5HOP

ASKER

Ok this is alot better "almost there" (that first link wont work for MULTISZ its the same as above that i was using)


The second link will work i think but im not sure what is wrong with it i have not see some of it before these are new two me i have made it right to the proper key but it is still as regsz i think it because i dont know what to put in the parts i marked.


Dim aStrings() As String
Dim lngRet As Long, lngIndex As Long

  Redim aStrings(0 to 2)

  aStrings(0) = "String1" <<-== what goes here
  aStrings(1) = "String2" <<-== what goes here
  aStrings(2) = "String3" <<-== what goes here
 
  '---> Write the Multi String Value
  lngRet = SetValue(HKEY_LOCAL_MACHINE, "Software\MyApp", "MultiStringValue", aStrings)

  '---> Read the Multi String Value
  aStrings = GetValue(HKEY_LOCAL_MACHINE, "Software\MyApp", "MultiStringValue", Split(""))

   For lngIndex = 0 To UBound(aStrings)
   
      Debug.Print aStrings(lngIndex)
   
   Next
I'll dust off my VB6 and fiddle with it later today - but don't let that stop anyone else...
This worked:

Option Explicit
Option Base 0

Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
                                      Alias "RegOpenKeyExA" _
                                     (ByVal hKey As Long _
                                     , ByVal lpSubKey As String _
                                     , ByVal ulOptions As Long _
                                     , ByVal samDesired As Long _
                                     , phkResult As Long) _
                                     As Long
                                   
Private Declare Function RegQueryValueEx Lib "advapi32.dll" _
                                         Alias "RegQueryValueExA" _
                                         (ByVal hKey As Long _
                                         , ByVal lpValueName As String _
                                         , ByVal lpReserved As Long _
                                         , lpType As Long, ByVal lpData As String _
                                         , lpcbData As Long) _
                                         As Long

Private Declare Function RegSetValueEx Lib "advapi32.dll" _
                                       Alias "RegSetValueExA" _
                                       (ByVal hKey As Long _
                                       , ByVal lpValueName As String _
                                       , ByVal Reserved As Long _
                                       , ByVal dwType As Long _
                                       , ByVal lpValue As String _
                                       , ByVal cbData As Long) _
                                       As Long
                                     
Private Declare Function RegCloseKey Lib "advapi32.dll" _
                                    (ByVal hKey As Long) _
                                    As Long
                                         


Const ERROR_SUCCESS = 0
Const ERROR_MORE_DATA = 234
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_MULTI_SZ = 7

Const STANDARD_RIGHTS_ALL = &H1F0000
Const KEY_QUERY_VALUE = &H1
Const KEY_SET_VALUE = &H2
Const KEY_CREATE_SUB_KEY = &H4
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const KEY_CREATE_LINK = &H20
Const SYNCHRONIZE = &H100000
Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL _
                         Or KEY_QUERY_VALUE _
                         Or KEY_SET_VALUE _
                         Or KEY_CREATE_SUB_KEY _
                         Or KEY_ENUMERATE_SUB_KEYS _
                         Or KEY_NOTIFY _
                         Or KEY_CREATE_LINK) _
                    And (Not SYNCHRONIZE))

Dim RC As Long
Dim SessionKey As Long
Dim ValType As Long
Dim ValLen As Long
Dim ValDAta(2) As String
Dim strMULTISZ As String
Dim arrStrings() As String
Dim i As Integer


Private Sub Form_Load()
Dim DeleteLine As String

DeleteLine = "\??\c:\dummy.dat"

' Get the Session Manager key
RC = RegOpenKeyEx(HKEY_LOCAL_MACHINE _
               , "System\CurrentControlSet\Control\Session Manager" _
               , 0 _
               , KEY_ALL_ACCESS _
               , SessionKey)
If RC <> ERROR_SUCCESS Then
   MsgBox ("Unable to open Session Manager key")
   End
   End If
   
' See if any operations are pending
RC = RegQueryValueEx(SessionKey _
                   , "PendingFileRenameOperations" _
                   , 0 _
                   , ValType _
                   , ByVal 0 _
                   , ValLen)
If RC = ERROR_MORE_DATA Then
   ' Value is already there - get it and merge in ours
   strMULTISZ = Space(ValLen + 1)
   RC = RegQueryValueEx(SessionKey _
                      , "PendingFileRenameOperations" _
                      , 0 _
                      , ValType _
                      , ByVal strMULTISZ _
                      , ValLen)
   ' Split out the individual strings into a string array
   strMULTISZ = Left(strMULTISZ, InStr(1, strMULTISZ, Chr(0) & Chr(0)) - 1)
   arrStrings = Split(strMULTISZ, Chr(0))
     
   ' Make room for our new pair
   ReDim Preserve arrStrings(UBound(arrStrings) + 2)
   arrStrings(UBound(arrStrings) - 1) = DeleteLine
   arrStrings(UBound(arrStrings)) = ""
     
   ' Turn it back into a REG_MULTI_SZ
   strMULTISZ = Join(arrStrings, Chr(0)) & Chr(0)
     
   ' And update it
   RC = RegSetValueEx(SessionKey _
                     , "PendingFileRenameOperations" _
                     , 0 _
                     , REG_MULTI_SZ _
                     , strMULTISZ _
                     , Len(strMULTISZ) + 1)

Else
   ' Its either there, so just add our new pair
   ReDim arrStrings(1)
   arrStrings(0) = DeleteLine
   arrStrings(1) = ""
     
   ' Turn it back into a REG_MULTI_SZ
   strMULTISZ = Join(arrStrings, Chr(0)) & Chr(0)
     
   ' And update it
   RC = RegSetValueEx(SessionKey _
                     , "PendingFileRenameOperations" _
                     , 0 _
                     , REG_MULTI_SZ _
                     , strMULTISZ _
                     , Len(strMULTISZ) + 1)
   End If

RegCloseKey (SessionKey)
End
End Sub