Thread: SetFocus error

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    24

    SetFocus error

    I have just recieved the handle of an Edit box in another application. I want to put some text in there.

    Does the control have to have focus before i place text in there?

    I looked up MSDN, it gave me the syntax:
    Code:
    SetFocus(handle);
    when i use this i gives me the error:
    [C++Error] my_IP_suite_keygen.cpp(61): Extra parameter in call to _fastcall Forms::TCustomForm::SetFocus().

    so i tried:
    Code:
    SetFocus(handle);
    when i use this i gives me the error:
    [C++Error] my_IP_suite_keygen.cpp(61): Pointer to structure required on left side of -> or ->*.

    I use Borland C++ Builder 3, win 2000 Pro

    Also what function do i use to set the text?

    SetWindowText()
    PosMessage()

    Is one more appropiate that the other?

    Thanks.

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    1. How did you get the edit control's handle?

    2. Both of your codes are the same

    3. I think you have to use PostMessage() or SendMessage(), because the edit control is in another process. SetWindowText() and GetWindowText() prevent inter-process communication.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    24
    i used

    Code:
    hBut = FindWindowEx(hWnd, hBut, "TEdit", NULL);
    i used spy++ yo check the handle is right

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I don't think you need to have the second parameter as hBut, unless you're enumerating the controls.

    I looked up MSDN, it gave me the syntax:

    code:--------------------------------------------------------------------------------
    SetFocus(handle);
    --------------------------------------------------------------------------------

    when i use this i gives me the error:
    [C++Error] my_IP_suite_keygen.cpp(61): Extra parameter in call to _fastcall Forms::TCustomForm::SetFocus().

    so i tried:

    code:--------------------------------------------------------------------------------
    SetFocus(handle);
    --------------------------------------------------------------------------------
    What should the second code fragment actually be?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    24
    ah isee.

    the second bit of code
    Code:
    ::SetFocus(hBut)
    i also tried:
    Code:
    hBut->SetFocus()
    none worked.

    to set the text i tried
    Code:
    PostMessage(hBut, WM_SETTEXT, NULL, (LPARAM)(LPCTSTR)"hey");
    which also didn't work

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Post all your code.

    What's the target application? It may require certain security privileges.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    24
    Code:
    //---------------------------------------------------------------------------
    #include <process.h>
    #include <stdlib.h>
    #include <time.h>
    #include <vcl.h>
    #include <Windows.h>
    #pragma hdrstop
    
    #include "my_IP_suite.h"
    
    //---------------------------------------------------------------------------
    
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    
    //---------------------------------------------------------------------------
    
    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
    {
        //const char *appPath = "C:\\Program Files\\My IP Suite\\MyIPSuite.exe";
    
        //execl(appPath, appPath, NULL);
    
        HWND hWnd;      //window handle
        HWND hBut;      //control handle
    
        // get handle of Reminder window
        hWnd = FindWindow(NULL, "Reminder");
    
        if(! hWnd) exit(0);     //if window don't found, exit program
    
        //move through controls, get handle of command button
        hBut = FindWindowEx(hWnd, NULL, "TButton", NULL);
        hBut = FindWindowEx(hWnd, hBut, "TButton", NULL);
    
        //click command button
        PostMessage(hBut, BM_CLICK, NULL, NULL);
    
    
        //get title of Options window
        hWnd = FindWindow(NULL, "Options");
    
        char buff[] = "HOI";
    
    
        if(! hWnd) exit(0);     //if window don't found, exit program
    
        //move through controls, get handle of edit box
        hBut = FindWindowEx(hWnd, NULL, "TButton", NULL);
        hBut = FindWindowEx(hWnd, hBut, "TButton", NULL);
        hBut = FindWindowEx(hWnd, hBut, "TEdit", NULL);
        hBut = FindWindowEx(hWnd, hBut, "TEdit", NULL);
        hBut = FindWindowEx(hWnd, hBut, "TEdit", NULL);
    
    
        PostMessage(hBut, WM_SETTEXT, NULL, (LPARAM)(LPCTSTR)"hey");
    
    
        //PostMessage(hBut, WM_SETTEXT, 0, (LPARAM)(LPCTSTR)"HI");
        //::SetFocus(hBut);
        //SetWindowText(hBut, buff);
    
    }
    
    //---------------------------------------------------------------------------

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    1. You're sure the final value of hBut is the correct handle?

    2. Try using SendMessage() instead of PostMessage()

    3. Try SetFocus(hBut) not ::SetFocus(hBut)

    4. Maybe the class TEdit can't be used in this way. Can you give me some info on that class?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    24
    ok, sendmessage worked. how simple.

    thanks for your help

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What about SetFocus(hBut)? Did you get that to work?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    24
    no i didn't. I just though the control might to have focus before i could set the text.

    But there is no need for setfocus anymore

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I suspect that you can't use SetFocus() on a control in another process. I think using AttachThreadInput() would allow this, but I guess you don't need it.

    I find it interesting that you were able to send a pointer to a string between processes, and it still worked.

    Is this what you did?
    Code:
    SendMessage(hBut, WM_SETTEXT, NULL, (LPARAM)(LPCTSTR)"hey");
    And it actually displayed "hey"?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  13. #13
    Registered User
    Join Date
    Sep 2003
    Posts
    24
    ok ill look up AttachThreadInput() . no doubt ill need it sometime.

    yeah, thats the code, and yeah it worked

  14. #14
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    It shouldn't have worked. That pointer points to a text string in YOUR process's virtual memory area. The other process shouldn't have access to that memory area. What version of Windows are you running?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  15. #15
    Registered User
    Join Date
    Sep 2003
    Posts
    24
    Windows 2000 Pro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM