CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Notepad controlled by VC++ 6.0

  1. #1
    Join Date
    Feb 2016
    Posts
    8

    Notepad controlled by VC++ 6.0

    Hi,
    I am writing dialog based MFC application and one of the tasks is to control Notepad...
    I have found some code to open Notepad and specific text file..
    All works fine, but I have two problems...
    1. I donít know how to close text file (there no need to modify it) and quit Notepad...
    2. When I send not existing file name I get an error saying that file is not found and asking me if I want to create a new one.... What I want is my own text box and no Notepad shown.
    Below is the code I am using....
    Please help me to get around....
    Thanks
    Val....
    .........
    HINSTANCE hInst = ShellExecute(0,
    "open", // Operation to perform
    "H:\\WINDOWS\\system32\\notepad.exe", // Application name
    "D:\\Input\\Instructio.txt", // Document location
    0, // Default directory
    1); // SW_SHOW
    if(reinterpret_cast<int>(hInst) <= 32)

    {
    // Could not start application
    switch(reinterpret_cast<int>(hInst))

    {
    case 0:
    // The operating system is out of memory or resources.
    break;

    case ERROR_FILE_NOT_FOUND:
    // The specified file was not found.
    {
    MessageBox(" The specified file was not found.");
    }
    break;

    case ERROR_PATH_NOT_FOUND:
    // The specified path was not found.
    {
    MessageBox(" The specified path was not found.");
    }
    break;

    case ERROR_BAD_FORMAT:
    // The .exe file is invalid (non-Microsoft Win32 .exe or error in .exe image).
    break;

    case SE_ERR_ACCESSDENIED:
    // The operating system denied access to the specified file.
    break;

    case SE_ERR_ASSOCINCOMPLETE:
    // The file name association is incomplete or invalid.
    break;

    case SE_ERR_DDEBUSY:
    // The Dynamic Data Exchange (DDE) transaction could not be completed because
    // other DDE transactions were being processed.
    break;

    case SE_ERR_DDEFAIL:
    // The DDE transaction failed.
    break;

    case SE_ERR_DDETIMEOUT:
    // The DDE transaction could not be completed because the request timed out.
    break;

    case SE_ERR_DLLNOTFOUND:
    // The specified dynamic-link library (DLL) was not found.
    break;
    // case SE_ERR_FNF:
    // The specified file was not found.
    // break;

    case SE_ERR_NOASSOC:
    // There is no application associated with the given file name extension.
    // This error will also be returned if you attempt to print a file that is
    // not printable.
    break;

    case SE_ERR_OOM:
    // There was not enough memory to complete the operation.
    break;

    // case SE_ERR_PNF:
    // // The specified path was not found.
    // break;

    case SE_ERR_SHARE:
    // A sharing violation occurred.
    break;


    }
    } }

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Notepad controlled by VC++ 6.0

    Hi!

    Modifying the actual Notepad behavior would require hooks or similar, and that is an expert topic, not possible to explain in a reasonable amount of text.

    However, what you can do is to make sure that the file always exists:
    Check if the file exists, if not show your own message.
    Create the file if the user wanted to, and then launch notepad.
    Nobody cares how it works as long as it works

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,149

    Re: Notepad controlled by VC++ 6.0

    When posting code, please use code tags to make the code readable! Go Advanced, select the formatted code and click '#'.

    and quit Notepad.
    If you use ShellExecuteEx() with the right combination of masks and notepad isn't already running, then you can obtain a handle to the newly created notepad process. Hence you can close the process. If the returned process handle is NULL, then you can enumerate the processes to find the appropriate notepad process and close it. See
    https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx
    https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.3)

  4. #4
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Notepad controlled by VC++ 6.0

    Quote Originally Posted by 2kaud View Post
    ...If you use ShellExecuteEx()...
    I think CreateProcess() is a better choice if you are interested in process handles/threads.
    Nobody cares how it works as long as it works

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,734

    Re: Notepad controlled by VC++ 6.0

    Quote Originally Posted by Lvalera View Post
    Hi,
    I am writing dialog based MFC application and one of the tasks is to control Notepad...
    ....
    What I want is my own text box and no Notepad shown.
    Well, if you want to have your "own text box and no Notepad shown" then consider using the SDI (or MDI) MFC application with the view class derived from CEditView.
    If, inn contrary, your main task is to control Notepad then implement ShellExecuteEx or CreateProcess calls (as 2kaud and zerver already wrote).
    Note also that to exit Notepad cleanly you would need to PostMessage WM_CLOSE message to its window handle.
    Victor Nijegorodov

  6. #6
    Join Date
    Feb 2016
    Posts
    16

    Re: Notepad controlled by VC++ 6.0

    It's not so hard to close it and deal with save message.But it's not easy too.
    One way is to simply close it like task manager.But i doubt it is right choice.
    Second way is to use hooks ,global edition of SetWindowsHookEx stuff.
    you'll need to deploy your own dll with hookin procedure.
    Install system wide keyboard hook,and send keystrokes like (Alt+F4) then (Left Arrow) and Enter.This all makes anti-malware soft to become disturbed.
    Third way you can install sys message filter hook(also system-wide) to deal with it's messages.

    previous post is a real garbage.Man do you even know that ,after WM_CLOSE received ,you'll see "Do you wanna save file" box?That happens if someone will just press character key .
    Last edited by Alexeyn; March 1st, 2016 at 03:20 PM.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,588

    Re: Notepad controlled by VC++ 6.0

    Quote Originally Posted by Alexeyn View Post
    It's not so hard to close it and deal with save message.But it's not easy too.
    One way is to simply close it like task manager.But i doubt it is right choice.
    Second way is to use hooks ,global edition of SetWindowsHookEx stuff.
    you'll need to deploy your own dll with hookin procedure.
    Install system wide keyboard hook,and send keystrokes like (Alt+F4) then (Left Arrow) and Enter

    previous post is real garbage
    Victor (who happens to be the moderator and long time contributor here, so calling his post "garbage" probably isn't in your best interests) is right. There's no need for hooking here, no need to write your own DLL and especially no reason to use words like "garbage" when referring to a post you disagree with, especially when you're wrong.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,302

    Re: Notepad controlled by VC++ 6.0

    If you want to write characters to a notepad window, you can use SendInput function.

    You can also query notepad to see if it can close without prompting the user to save.

    Code:
    			if(SendMessageTimeout(m_hWnd,
    								WM_QUERYENDSESSION,
    								0,
    								ENDSESSION_LOGOFF,
    								SMTO_ABORTIFHUNG,
    								1000,       
    								&dwResult))
                {
                    // Process will terminate nicely, so tell it to close
                    SendMessageTimeout(m_hWnd, 
    									WM_CLOSE,
    									TRUE,
    									0,
    									SMTO_ABORTIFHUNG,
    									1000,   
    									&dwResult);
                }

  9. #9
    Join Date
    Feb 2016
    Posts
    8

    Re: Notepad controlled by VC++ 6.0

    The information in the links provided looks confusing for me.... I am sorry for saying that.
    If I can run Notepad and open the needed file with this simple code:

    Code:
    CString path="H:\\WINDOWS\\system32\\notepad.exe";
    HINSTANCE  hInst;
    hInst = ShellExecute(0,  "open", path, "D:\\Input\\Instruction.txt", 0,  SW_SHOW     );      //
    why canít I close Notepad with something relatively simple? I do not need to make any changes in the text file and it will be there all the time.
    To answer Victorís question why not to use CEditView.... I have already have code working, it opens text file and all I need is to close it....The user can always close the file, but it would be better it I can do it in the program. If in the future I would need to change the contents of the text file, then it would be easy to do.

    I wanted to use this text file as the instruction for the user..... it is not something I trebly need... It will work without this help option... I can put it aside for the future.

    What I would like to do is to get ShellExecute to run cmd.exe... I need to get the list of the files in the folder... this is how I get it using Command Prompt directly..:

    H:\Documents and Settings\Val>cd ď\Program Files\DropboxĒ
    H:\ Program Files\Dropbox>dir>all.txt

    As the result I have the text file, which I can process...
    I use this code to open Command Prompt:
    Code:
    ShellExecute (NULL, "open", "cmd.exe", "/k  h: ", NULL, SW_SHOW );
    The needed folder at H drive, but I still need to send second command dir>all.txt and I donít know how to do this... So the problem is similar to Notepad: how to send another command and quit Command Prompt...
    If someone can give me the sample code that would be great...

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,302

    Re: Notepad controlled by VC++ 6.0

    Quote Originally Posted by Lvalera View Post
    why can’t I close Notepad with something relatively simple? I do not need to make any changes in the text file and it will be there all the time
    You simply need to send a message to close notepad (you need to find the handle to the main notepad window first).

    The code I've posted with the first SendMessageTimeout just asks notepad if it can close (if the user doesn't make any file changes the method will return true). The 2nd SendMessageTimeout sends a close message to notepad.

    It's a good practice to do this because even if you think your users won't make any changes in notepad, they may accidently make changes. Do you want your program to handle this, or fail?

    If the user has made changes, the above code won't close notepad. Your only option now is to use TerminateProcess to close notepad (and for this, you'll need the current process id for notepad).

  11. #11
    Join Date
    Mar 2001
    Posts
    2,527

    Re: Notepad controlled by VC++ 6.0

    ahoodin
    To keep the plot moving, that's why.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)