CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 43
  1. #16
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: How Can I Close exe files with C++ code ?

    Open
    Code:
        ::ShellExecute(NULL,
                       "open",
                       "Calc.exe",
                       "",
                       "",
                       SW_SHOWNORMAL);
    Close
    Code:
        CWnd *window = CWnd::FindWindow(NULL,     LPCTSTR("Calculator"));
    
        if (window != NULL)
        {
            window->PostMessage(WM_CLOSE);
        }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  2. #17
    Join Date
    Feb 2009
    Posts
    80

    Re: How Can I Close exe files with C++ code ?

    Hey,
    thanks John. Examples really seem to help me grasp this stuff quicker.
    thanks a million !

    cheers.

  3. #18
    Join Date
    Feb 2009
    Posts
    80

    Re: How Can I Close exe files with C++ code ?

    hey John,
    the open calc.exe code works great !

    the close one I'm doing something wrong.... perhaps I need to include another header ?
    I get

    error `CWnd' was not declared in this scope.

    here's what I tried....

    Code:
    #include <windows.h>
    #include <stdio.h>
    int main()
    {
        CWnd *window = CWnd::FindWindow(NULL,     LPCTSTR("Calculator"));
    
        if (window != NULL)
        {
            window->PostMessage(WM_CLOSE);
        }
    }


    I'm sure I'm missing something being a newbie and all.
    any clues are appreciated.

    Thanks everyone. : )

  4. #19
    Join Date
    Feb 2009
    Posts
    80

    Re: How Can I Close exe files with C++ code ?

    *window is a pointer right ?

    so I guess I have to declare that first...

    again... I'm so " newbie " I don't know, that's my best guess.

    any insight is appreciated.

    cheers.

    : )

  5. #20
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: How Can I Close exe files with C++ code ?

    My example was for an MFC application. If you are using straight Win32 then you will have to change it to the following.

    Actually both methods should work with MFC.
    Code:
    HWND hwnd = ::FindWindow(NULL, LPCTSTR("Calculator"));
    
    if (hwnd != NULL)
    {
        ::PostMessage(hwnd, WM_CLOSE);
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #21
    Join Date
    Feb 2009
    Posts
    80

    Exclamation Re: How Can I Close exe files with C++ code ?

    Hey thanks again John.
    I'm still having trouble getting it to work. get an error now about bool too few arguments for HWND_*

    I obviously need more knowledge to figure this stuff out.

    Can You recommend a good book for me perhaps ? there seems to be so frickin' many I can't tell which is a good one.

    I need one that starts from the beginning. However I am a good problem solver, I understand electronics very well, and I am good with HTML and a little PHP so....
    not tooooooo beginner either.

    any insight on a good book would be greatly appreciated.

    cheers.

  7. #22
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: How Can I Close exe files with C++ code ?

    Can you post some minimal code to replicate the compiler error?
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  8. #23
    Join Date
    Feb 2009
    Posts
    80

    Re: How Can I Close exe files with C++ code ?

    sure thanks for the help John.

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <windows.h>
    using namespace std;
    
    int main()
    {
      ::ShellExecute(NULL,
                       "open",
                       "calc.exe",
                       "",
                       "",
                       SW_SHOWNORMAL);
    
     cout<< " alright ! ";
    
     cin.get();
    
    
    
     HWND hwnd = ::FindWindow(NULL, LPCTSTR("Calculator"));
    
    if (hwnd != NULL)
    {
        ::PostMessage(hwnd, WM_CLOSE);
    }
    
    }

  9. #24
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: How Can I Close exe files with C++ code ?

    PostMessage takes four arguments, not two. Pass zeros for WPARAM and LPARAM.

  10. #25
    Join Date
    Feb 2009
    Posts
    80

    Re: How Can I Close exe files with C++ code ?

    hey thanks plasmator,
    that's got it.

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <windows.h>
    using namespace std;
    
    int main()
    {
      ::ShellExecute(NULL,
                       "open",
                       "calc.exe",
                       "",
                       "",
                       SW_SHOWNORMAL);
    
     cout<< " alright ! ";
    
     cin.get();
    
    
    
     HWND hwnd = ::FindWindow(NULL, LPCTSTR("Calculator"));
    
    if (hwnd != NULL)
    {
        ::PostMessage(hwnd, WM_CLOSE, 0 , 0 );
    }
    }
    however....

    I don't underSTAND it. what should I read up on to understand
    the -
    Code:
     HWND hwnd = :FindWindow(NULL, LPCTSTR("Calculator"));
    part...
    and the 4 arguments you mentioned.

    what " topic " would this be considered ?

    thanks.

  11. #26
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How Can I Close exe files with C++ code ?

    Quote Originally Posted by Jeff++ View Post
    Hey thanks again John.
    I'm still having trouble getting it to work. get an error now about bool too few arguments for HWND_*

    I obviously need more knowledge to figure this stuff out.
    The issue you need to realize is that you have two stumbling blocks. The first is learning the C++ language. The second is learning Windows programming. Learning the C++ language itself is difficult. Learning the Windows API is a totally different topic, and should be undertaken once you know the language you will be using. It makes no sense learning the API of an operating system using a computer language you don't know how to use properly.
    Can You recommend a good book for me perhaps ? there seems to be so frickin' many I can't tell which is a good one.
    See above. Get a good C++ only book. There is no need to introduce Windows programming.

    Afterwards you use that knowledge to interface to the OS, in this case Windows. You then get Windows programming books. Most of these books are not language tutorial books -- they assume you know how to use 'C' or C++ already.
    I need one that starts from the beginning. However I am a good problem solver,
    But it seems you're just throwing random things together to learn, and learning C++ doesn't work that way.
    wit I understand electronics very well, and I am good with HTML and a little PHP so....
    I have seen very bad programmers who supposedly knew "eletronics", and HTML and PHP are a far cry from C++ in terms of learning and complexity.

    Regards,

    Paul McKenzie

  12. #27
    Join Date
    Feb 2009
    Posts
    80

    Re: How Can I Close exe files with C++ code ?

    hey Paul. I'm actually a teacher, so I know that everyone learns different. I will take your advice, however I am better at reverse engineering to see how something works. I learn much faster that way. I didn't want to use windows programming, it was the only short code way of opening the calculator in my first game. I'm sure my knowledge of C++ will get better.

  13. #28
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How Can I Close exe files with C++ code ?

    Quote Originally Posted by Jeff++ View Post
    hey Paul. I'm actually a teacher, so I know that everyone learns different. I will take your advice, however I am better at reverse engineering to see how something works.
    Unfortunately, you will soon realize that C++ cannot be learned properly by reverse engineering.

    Regards,

    Paul McKenzie

  14. #29
    Join Date
    Feb 2009
    Posts
    80

    Re: How Can I Close exe files with C++ code ?

    maybe so, but trying things out bring up problems and errors. Then I learn why there's a problem or error. Then when I do come across that situation in a tutorial... I understand what they're saying more... that's all. In other words, it helps me learn LATER by making mistakes now. It's just how I work. Might not be for everyone. I do listen to people when they tell me to try it a better way. thanks for your help though.
    cheers.

  15. #30
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: How Can I Close exe files with C++ code ?

    Quote Originally Posted by Plasmator View Post
    PostMessage takes four arguments, not two. Pass zeros for WPARAM and LPARAM.
    Ah, I see. I don't usually use the C API. The CWnd member function has the last two parameters with default zero values.
    Code:
    BOOL PostMessage(
       UINT message,
       WPARAM wParam = 0,
       LPARAM lParam = 0 
    );
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured