Announcement

Collapse
No announcement yet.

How to paste to console from PBDLL60-program?

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

  • How to paste to console from PBDLL60-program?

    How do you paste to a console from a PBDLL60 program?

    To be more specific: i would like to run a bat-file that is located
    in memory and not on disk (for security reasons). I have observed that
    if I copy the content of the bat-file to the clipboard and then use
    Edit|Paste in the console, then the bat-file is actually excuted. I would
    therefore like to be able to simulate the Edit|Paste action from a
    PBDLL60-program. What I'm looking for is a sub like this:
    Code:
        sub ClipboardToConsole(byval sText as string)
    Regards
    Peter

    ------------------
    [email protected]
    www.dreammodel.dk

  • #2
    Peter,

    I think you can do almost all that you can do in a batch file with the
    SHELL statement, it will not give you loops or labels but it will do
    the rest.

    The logic available in a batch file can easily be simulated in BASIC
    and all you have to do is run SHELL for each specific line where you
    want to interact with the console.

    I tend to write a temp batch file to disk, run it then delete it but
    if that does not suit what you want, I think SHELL will do what you
    need.

    Regards,

    [email protected]

    ------------------
    hutch at movsd dot com
    The MASM Forum

    www.masm32.com

    Comment


    • #3
      Steve--

      Thanks for the reply. I'm trying to use ftp.exe to get a lot of
      files from the mainframe here at work. We are not allowed to use
      ftp-scripts for security reasons because our passwords will be in
      the script-files. I found out that if i pasted the following text
      into the console it actually worked:
      Code:
      ftp server.dst.dk  (the server)
      MyName
      MyPassword
      lcd c:\data
      get 'U060050.NL.DATA.TAB99.SOMETHING1' something1.tab
      get 'U060050.NL.DATA.TAB99.SOMETHING2' something2.tab
      ...
      get 'U060050.NL.DATA.TAB99.SOMETHING1000' something1000.tab
      quit
      I'm trying to automate this aproach using PB.

      I have tried to make my own ftp-client using ftp.zip from
      PowerBasic, but the "'" in 'U060050.NL.DATA.TAB99.SOMETHING1'
      gives me a lot of problems. I can't use mget because of the "'"s.

      Regards
      Peter


      [This message has been edited by Peter P Stephensen (edited October 20, 2000).]
      [email protected]
      www.dreammodel.dk

      Comment


      • #4
        Peter --
        I like keybd_event.
        It's not ready module, design only (see virtual codes + caps lock status).
        Initial state of console should be a window (it's possible to do automatic).

        Code:
           #Compile Exe
           #Dim All
           #Register None
           #Include "WIN32API.INC"
        
           Sub pSetForegroundWindow(hwnd As Long)
             Do
                Dim lForeThreadID As Long, lThisThreadID As Long, hWndF As Long
                hWndF = GetForegroundWindow: If hwnd = hWndF Then Exit Sub
                lForeThreadID = GetWindowThreadProcessId(hWndF, ByVal 0&)
                lThisThreadID = GetWindowThreadProcessId(hwnd, ByVal 0&)
                If lForeThreadID <> lThisThreadID Then _
                   AttachThreadInput lForeThreadID, lThisThreadID, %True
                SetForegroundWindow hwnd
                If lForeThreadID <> lThisThreadID Then _
                   AttachThreadInput lForeThreadID, lThisThreadID, %False
                If IsIconic(hwnd) Then ShowWindow hwnd, %SW_RESTORE Else ShowWindow hwnd, %SW_SHOW
                hWndF = GetForegroundWindow: If hwnd = hWndF Then Exit Sub
                ShowWindow hWndF, %SW_MINIMIZE
                hWndF = GetForegroundWindow: If hwnd = hWndF Then Exit Sub
                ShowWindow hWnd, %SW_MINIMIZE: ShowWindow hWnd, %SW_RESTORE
              Loop
           End Sub
        
           Sub SendKbd (key As Long)
              KeyBd_Event key, MapVirtualKey(key, 0), 0, 0: Sleep 0
              KeyBd_Event key, MapVirtualKey(key, 0), %KEYEVENTF_KEYUP, 0: Sleep 0
           End Sub
        
           CallBack Function DlgProc
              Select Case CbMsg
        
                 Case %WM_INITDIALOG
                      
                    Static hWndConsole As Long
                    ShowWindow CbHndl, %SW_MAXIMIZE
                    Control Add TextBox, CbHndl, 101, "dir", 5, 5, 50, 12
                    Control Add Button, CbHndl, 102, "Send", 5, 20, 50, 12
            
                    Global hWndConsole As Long
                    AllocConsole
                    SetConsoleTitle "Unique"
                    Do
                       hWndConsole = FindWindow("", "Unique")
                       If hWndConsole = 0 Then Sleep 20 Else Exit Do
                    Loop
        
                    Local hDlgE As Long
                    Dialog New CbHndl, "", , , 0, 0, %WS_CHILD Or %WS_CLIPCHILDREN Or %WS_CLIPSIBLINGS To hdlgE
                    Dialog Show Modeless hDlgE
        
                    Local pt1 As POINTAPI, pt2 As POINTAPI, rcC As RECT
                    GetClientRect hWndConsole, rcC
                    SetWindowPos hDlgE, 0, (GetSystemMetrics(%SM_CXSCREEN) - rcC.nRight) / 2, _
                       (GetSystemMetrics(%SM_CYSCREEN) - rcC.nBottom) / 2, rcC.nRight, rcC.nBottom, _
                       %SWP_NOZORDER  Or %SWP_NOACTIVATE
                    SetParent hWndConsole, hDlgE
                    SetWindowPos hWndConsole, 0, 0, 0, 0, 0, %SWP_NOSIZE Or %SWP_NOZORDER Or %SWP_NOACTIVATE
                    ClientToScreen hDlgE, pt1
                    ClientToScreen hWndConsole, pt2
                    SetWindowPos hWndConsole, 0,  pt1.x - pt2.x, pt1.y - pt2.y, 0, 0, _
                       %SWP_NOSIZE Or %SWP_NOZORDER Or %SWP_NOACTIVATE
              
                    Dim i As Long
                    i = Shell("command.com")
                    
                 Case %WM_COMMAND
                    If CbCtl = 102 Then
                       Dim w As String
                       Control Get Text CbHndl, 101 To w$
                       pSetForegroundWindow hWndConsole
                       For i = 1 To Len(w$)
                         SendKbd Asc(UCase$(Mid$(w$, i, 1))) ' ok for letters only !!!
                                                             ' look virtual code
                       Next
                       SendKbd %VK_RETURN
                    End If
              End Select
           End Function
                  
           Function PbMain
              Local hDlg As Long
              Dialog New 0,"Test",,, 0, 0, %WS_CAPTION Or %WS_SYSMENU Or %WS_CLIPCHILDREN To hdlg
              Dialog Show Modal hdlg Call DlgProc
           End Function
        [This message has been edited by Semen Matusovski (edited October 20, 2000).]

        Comment


        • #5
          Semen --

          Cool!

          Regards
          Peter

          ------------------
          [email protected]
          www.dreammodel.dk

          Comment


          • #6
            Semen,
            After executing your example, I lose access to the keyboard.
            I'm using Win98 on a P3 Toshiba notebook.
            Any fixes for this problem?
            Thanks,
            Arthur



            ------------------



            Arthur Kohl

            Comment


            • #7
              Arthur --
              This fragment was written under Win2000.

              I looked, what happends under Win98.
              How stupid and slowly it is ...
              Ok, how to avoid problems (conagent's window works by another way than cmd.exe).
              1) Unlike (!) Win2000, it's necessary to add some actions to release a console window:
              a) In dialog proc
              Case %WM_DESTROY
              SetParent hWndConsole, 0
              FreeConsole
              b) During execution of program before closing main window send a command EXIT.

              Warning: this fragment illustrates usage of keybd_event only and really is able to send correct virtual codes of letters A-Z.
              If you will use another symbols, results could be enough interesting .

              BTW. Because 9x works slowly, WriteConsoleInput should give better results.
              Another question that in this case a code will be much bigger.


              Conagent window


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

              Comment


              • #8
                Semen,
                Thank you for your repply,
                the example is cool, like all your fine examples.
                Arthur

                ------------------



                Arthur Kohl

                Comment


                • #9
                  I don't know which mainframe you are going to, but I have had
                  the same issue, but took care of it on the mainframe end by
                  creating a special user id which could not be used as a sign-on,
                  had restricted access to just the files I wanted (usually I would
                  set up a parking area where files could be deposited waiting
                  transfer). This user password could only be used by FTP, and
                  could only access a specific list of files, and was sufficient
                  to satisfy the security auditors.

                  Just another way to skin the cat.

                  ------------------
                  Thanks,

                  John Kovacich
                  Ivory Tower Software

                  Comment


                  • #10
                    Here is a way to send the clipboard to the console.
                    Is there an api to stop screen-updating to avoid cartoon-effect?

                    Regards
                    Peter
                    Code:
                    #compile exe
                    #dim all
                    #register none
                    #include "win32api.inc"
                     
                    declare function Text2Clipboard(Txt as string) as long
                    declare sub Clipboard2Console(byval ConsTitle as string)
                    declare sub mSendKey (byval key as long, byval nTimes as long)
                     
                    function pbmain
                      
                        local ret as long
                      
                        AllocConsole
                        ret = Shell("command.com")
                     
                        Text2Clipboard "cd.." & $CR & _ 
                                       "cd.." & $CR & _ 
                    	            "dir"  & $CR & _  
                    	            "exit" 
                      
                        SetConsoleTitle   "Testing..."
                        Clipboard2Console "Testing..."
                     
                    end function
                     
                    sub Clipboard2Console(byval ConsTitle as string)
                     
                        local hWndConsole as long
                        local r as RECT, pt as POINTAPI
                        local x as long, y as long
                     
                        Do
                            hWndConsole = FindWindow ("", bycopy ConsTitle)
                            If hWndConsole then exit do else sleep 20
                        Loop
                     
                        pt.x = 1 : pt.y = -1 
                     
                        ClientToScreen hWndConsole, pt 
                     
                        x = 65535 * pt.x / GetSystemMetrics(%SM_CXSCREEN)
                        y = 65535 * pt.y / GetSystemMetrics(%SM_CYSCREEN)
                     
                        mouse_event %MOUSEEVENTF_ABSOLUTE or %MOUSEEVENTF_MOVE, x, y, 0,0 : sleep 0 
                        mouse_event %MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0 : sleep 0
                        mouse_event %MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 : sleep 0
                     
                        mSendKey %VK_DOWN,   7
                        mSendKey %VK_RIGHT,  1
                        mSendKey %VK_DOWN,   2
                        mSendKey %VK_RETURN, 1	
                     
                    end sub
                     
                    function Text2Clipboard(Txt as string) as long
                     
                        local hGlob  as long
                        local hData  as long
                     
                        hData = GlobalAlloc(%GMEM_MOVEABLE OR %GMEM_DDESHARE, len(Txt)+1)
                        hGlob = GlobalLock(hData)
                        poke$ hGlob, Txt
                        GlobalUnlock hData
                     
                        if OpenClipboard(%NULL) = 0 then
                           GlobalFree hData : exit function
                        end if
                     
                        EmptyClipboard
                        SetClipboardData %CF_TEXT, hData
                        CloseClipboard
                     
                        function = 1
                     
                    end function
                     
                    sub mSendKey (byval key as long, byval nTimes as long)
                     
                        local i as long
                    
                        for i = 1 to nTimes
                            KeyBd_Event key, MapVirtualKey(key, 0), 0, 0: sleep 0
                            KeyBd_Event key, MapVirtualKey(key, 0), %KEYEVENTF_KEYUP, 0: sleep 0
                        next i
                     
                    end sub

                    ------------------
                    [email protected]
                    www.dreammodel.dk

                    Comment


                    • #11
                      LockWindowUpdate hWnd works for any window.
                      But don't forget to release (LockWindowUpdate 0&)

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

                      Comment


                      • #12
                        I have tried LockWindowUpdate but it didn't work:
                        Code:
                        sub Clipboard2Console(byval ConsTitle as string)
                         
                            local hWndConsole as long
                            local r as RECT, pt as POINTAPI
                            local x as long, y as long
                         
                            Do
                                hWndConsole = FindWindow ("", bycopy ConsTitle)
                                If hWndConsole then exit do else sleep 20
                            Loop
                         
                            pt.x = 1 : pt.y = -1 
                         
                            ClientToScreen hWndConsole, pt 
                         
                            x = 65535 * pt.x / GetSystemMetrics(%SM_CXSCREEN)
                            y = 65535 * pt.y / GetSystemMetrics(%SM_CYSCREEN)
                         
                            LockWindowUpdate hWndConsole
                         
                            mouse_event %MOUSEEVENTF_ABSOLUTE or %MOUSEEVENTF_MOVE, x, y, 0,0 : sleep 0 
                            mouse_event %MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0 : sleep 0
                            mouse_event %MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 : sleep 0
                         
                            mSendKey %VK_DOWN,   7
                            mSendKey %VK_RIGHT,  1
                            mSendKey %VK_DOWN,   2
                            mSendKey %VK_RETURN, 1	
                         
                            LockWindowUpdate 0
                         
                        end sub
                        ------------------
                        [email protected]
                        www.dreammodel.dk

                        Comment


                        • #13
                          Peter --
                          LockWindowUpdate holds updating of the screen.
                          Nothing less, nothing more.
                          When you release a window, occurs WM_PAINT.
                          That's why try to lock GetDesktopWindow (but select correct place), execute ALL (incl. returning foreground to GUI) and then release.

                          I use LockWindowUpdate enough often and it works fine.

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

                          Comment


                          • #14
                            Thanks...

                            ------------------
                            [email protected]
                            www.dreammodel.dk

                            Comment


                            • #15
                              Peter --
                              Under Win2000 (and, probably, under NT4) it's enough to send to console window
                              [bold]SendMessage hWndConsole, %WM_COMMAND, &HFFF1&, 0[/bold]

                              "Short" sample. Includes preparing of console, because - theoretically - InserMode should be turn on.
                              But my experiments show that at least under Win2000 it's not necessary.

                              So:
                              Code:
                                 #Compile Exe
                                 #Register None
                                 #Dim All
                                 #Include "win32api.inc"
                                 
                                 Global lKeyAddr As Long
                              
                                 Function NT_ConsoleParameters_SetC (szParam As Asciiz, Value As Asciiz) As Long
                              
                                    Function = RegSetValueEx (lKeyAddr, szParam, 0, %REG_SZ, Value, Len(Value))
                              
                                 End Function
                              
                                 Function NT_ConsoleParameters_SetD (szParam As Asciiz, Value As Dword) As Long
                              
                                    Function = RegSetValueEx (lKeyAddr, szParam, 0, %REG_DWORD, Value, 4&)
                              
                                 End Function
                              
                                 Function PrepareConsole As Long
                                    
                                    Dim Section As Asciiz * 268, lResult As Long
                                      ' %MAX_PATH + Console\
                                    If GetModuleFileName (ByVal 0&, Section, SizeOf(Section)) Then Else _
                                       Function = %True: Exit Function
                                       
                                    Replace "\" With "_" In Section
                                    Section = "Console\" + Section
                                    If RegOpenKeyEx (%HKEY_CURRENT_USER, Section, 0, %KEY_ALL_ACCESS, lKeyAddr) = %ERROR_SUCCESS Then
                                    ElseIf RegCreateKeyEx (%HKEY_CURRENT_USER, Section, 0, ByVal 0&, %REG_OPTION_NON_VOLATILE, _
                                       %KEY_ALL_ACCESS, ByVal %NULL, lKeyAddr, lResult) = %ERROR_SUCCESS Then
                                    Else
                                       Exit Function ' Failed
                                    End If
                              
                                    Dim azFontName As Asciiz * 15
                                    azFontName = "Lucida Console"
                              
                                    '------- Colors ------------
                                    NT_ConsoleParameters_SetD "ColorTable00", %Black
                                    NT_ConsoleParameters_SetD "ColorTable01", &H800000&
                                    NT_ConsoleParameters_SetD "ColorTable02", &H008000&
                                    NT_ConsoleParameters_SetD "ColorTable03", &H808000&
                                    NT_ConsoleParameters_SetD "ColorTable04", &H000080&
                                    NT_ConsoleParameters_SetD "ColorTable05", &H800080&
                                    NT_ConsoleParameters_SetD "ColorTable06", &H008080&
                                    NT_ConsoleParameters_SetD "ColorTable07", &HC0C0C0&
                                    NT_ConsoleParameters_SetD "ColorTable08", %Gray
                                    NT_ConsoleParameters_SetD "ColorTable09", %Blue
                                    NT_ConsoleParameters_SetD "ColorTable10", %Green
                                    NT_ConsoleParameters_SetD "ColorTable11", %Cyan
                                    NT_ConsoleParameters_SetD "ColorTable12", %Red
                                    NT_ConsoleParameters_SetD "ColorTable13", %Magenta
                                    NT_ConsoleParameters_SetD "ColorTable14", %Yellow
                                    NT_ConsoleParameters_SetD "ColorTable15", %White
                              
                                    NT_ConsoleParameters_SetD "PopupColors",  245&
                                    NT_ConsoleParameters_SetD "ScreenColors", 7&
                              
                                    ' ------------- Font ---------------
                                    NT_ConsoleParameters_SetC "FaceName",  "Lucida Console"
                                    NT_ConsoleParameters_SetD "FontFamily", &H36&  ' TTF
                                    NT_ConsoleParameters_SetD "FontSize",   MakDwd(0, 14) ' point
                                    NT_ConsoleParameters_SetD "FontWeight", %FW_BOLD
                              
                              
                                    '------------- Screen/Buffer Size ------------
                                    NT_ConsoleParameters_SetD "CurrentPage",       0&
                                    NT_ConsoleParameters_SetD "HistoryBufferSize", 25&
                                    NT_ConsoleParameters_SetD "HistoryNoDup",      0&
                                    NT_ConsoleParameters_SetD "NumberOfHistoryBuffers", 4&
                                    NT_ConsoleParameters_SetD "ScreenBufferSize",  MakDwd(80, 25)
                                    NT_ConsoleParameters_SetD "WindowSize",        MakDwd(80, 25)
                              
                                    '------------- Other --------------------
                                    NT_ConsoleParameters_SetD "CursorSize", 25&
                                    NT_ConsoleParameters_SetD "FullScreen", 0&
                                    NT_ConsoleParameters_SetD "InsertMode", 1&
                                    NT_ConsoleParameters_SetD "LoadConlme", 1&
                                    NT_ConsoleParameters_SetD "QuickEdit",  0&
                              
                                    RegCloseKey lKeyAddr
                              
                                 End Function
                                 
                                 Function PbMain
                                    Dim TmpAsciiz As Asciiz * %MAX_PATH, hWndConsole As Long, i As Long
                                    
                                    If PrepareConsole Then Exit Function
                                    
                                    AllocConsole
                                    i = Shell ("sort.exe")
                                    GetConsoleTitle TmpAsciiz, SizeOf(TmpAsciiz)
                              
                                    hWndConsole = FindWindow ("", TmpAsciiz)
                                    ShowWindow hWndConsole, %SW_SHOW
                              
                                    Dim Info As Asciiz * 1000, kk As Long, hData As Long
                                    
                                    Info = "Peter" + $CR  + "Semen" + $CR + Chr$(26) + $CR
                                    kk = Len(Info) + 1
                              
                                    hData = GlobalAlloc(%GMEM_MOVEABLE Or %GMEM_DDESHARE, kk)
                                    i = GlobalLock(hData)
                                    MoveMemory i, VarPtr(Info), kk
                                    
                                    If IsFalse(OpenClipboard(ByVal 0&)) Then
                                    Else
                                       EmptyClipboard
                                       SetClipboardData %CF_TEXT, hData
                                       CloseClipboard
                                    End If
                                    SendMessage hWndConsole, %WM_COMMAND, &HFFF1&, 0
                                    Sleep 2000
                                       
                                    GlobalUnlock hData: GlobalFree hData
                              
                                 End Function


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

                              Comment

                              Working...
                              X