Solved

CreateProcess & ShellExecute & WinExec??

Posted on 2000-04-24
3
437 Views
Last Modified: 2012-05-04
hi all,
I previously asked how to start a console application/ external application from a FORM and i have succeeded in doing this. My question now is :: Is it possible to manipulate where the window pops up and the size of the window. I have found nothing in the help menu that is in anyway informative. Can someone tell me how to do this?

I would have thought that there is simply some parameters i must set !!??!!

Thanks Liam

0
Comment
Question by:Leo79
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2743514
surprisingly, windows does not provide a direct way to do this.  The way you have to do so is to use FindWindow() or EnumWindows() to find the window handle of the console window you created.  (Windows does not provide a direct way to do this, I don't know why.)  Then once you have the window handle, you can use MoveWindow() to move the window where you want it.  (Or you can use other API functions to move it, hide it, maximize it, etc.)

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2743535
The following article from the VC help shows how to get a window handle from a console using FindWindow().
********************************
Obtaining a Console Window Handle (HWND)
Last reviewed: September 29, 1995
Article ID: Q124103  
The information in this article applies to:
Microsoft Win32 Application Programming Interface (API) included with:


    - Microsoft Windows NT versions 3.1 and 3.5
    - Microsoft Windows 95 version 4.0




SUMMARY
It may be useful to manipulate a window associated with a console application. The Win32 API provides no direct method for obtaining the window handle associated with a console application. However, you can obtain the window handle by calling FindWindow(). This function retrieves a window handle based on a class name or window name.

Call GetConsoleTitle() to determine the current console title. Then supply the current console title to FindWindow().



MORE INFORMATION
Because multiple windows may have the same title, you should change the current console window title to a unique title. This will help prevent the wrong window handle from being returned. Use SetConsoleTitle() to change the current console window title. Here is the process:


Call GetConsoleTitle() to save the current console window title.

Call SetConsoleTitle() to change the console title to a unique title.

Call Sleep(40) to ensure the window title was updated.

Call FindWindow(NULL, uniquetitle), to obtain the HWND this call returns the HWND -- or NULL if the operation failed.

Call SetConsoleTitle() with the value retrieved from step 1, to restore the original window title.

You should test the resulting HWND. For example, you can test to see if the returned HWND corresponds with the current process by calling GetWindowText() on the HWND and comparing the result with GetConsoleTitle().
The resulting HWND is not guaranteed to be suitable for all window handle operations.



Sample Code
The following function retrieves the current console application window handle (HWND). If the function succeeds, the return value is the handle of the console window. If the function fails, the return value is NULL. Some error checking is omitted, for brevity.

HWND GetConsoleHwnd(void) {

    #define MY_BUFSIZE 1024 // buffer size for console window titles
    HWND hwndFound;         // this is what is returned to the caller
    char pszNewWindowTitle[MY_BUFSIZE]; // contains fabricated WindowTitle
    char pszOldWindowTitle[MY_BUFSIZE]; // contains original WindowTitle

    // fetch current window title

    GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);

    // format a "unique" NewWindowTitle

    wsprintf(pszNewWindowTitle,"%d/%d",
                GetTickCount(),
                GetCurrentProcessId());

    // change current window title

    SetConsoleTitle(pszNewWindowTitle);

    // ensure window title has been updated

    Sleep(40);

    // look for NewWindowTitle

    hwndFound=FindWindow(NULL, pszNewWindowTitle);

    // restore original window title

    SetConsoleTitle(pszOldWindowTitle);

    return(hwndFound);

}

*****************************
Let me know if you have any questions.
0
 

Author Comment

by:Leo79
ID: 2745157
Thanks a Million,
The answer was perfect, even good enough for me to understand first time round. I have implement it and everything works the way i wanted it to.
Cheers Mate
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now