Thread: Text output into other applications

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    3

    Exclamation Text output into other applications

    is it possible to write a C program that can input text into other programs?

    Eg. i have a program that generates random number chains, and i want it to be able to put the text into an application like notepad or word. but i want this to happen in real-time, not saving the text into a .txt file

  2. #2
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    You could use some win32 api calls like SendMessage, FindWindow and FindWindowEx these 3 should be all that you need.

    try search MSDN

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    could you give an example of how to use these functions with word? the MSDN site is confusing

  4. #4
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Code:
    HWND D, E, R;
    char s[45] = { 0 }; 
    
    system( "notepad"  ); // not sure if this ill work, never tried this before : )
    
    D = FindWindow( "Notepad", NULL );
    
    E = FindWindowEx( D, 0, "Edit",  NULL );
    
    SendMessage( E, WM_SETTEXT, sizeof( s ),  (LPARAM) s ); // not sure if this should be sizeof s or strlen s. havent program win32 in a while.
    Edited: this part added:

    Have you tried win32 programming before?

    if not, you need to add windows.h header file for that code to work. What compiler do you use?
    Last edited by loko; 09-20-2005 at 11:02 PM.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    no i never tried win32 programming before, but i knew about windows.h for some reason
    Im compiling with MinGW.

    With your code, notepad opens, but where in the code do i place the text that gets sent to notepad?

    Thanks for the help btw
    Last edited by melchior; 09-20-2005 at 11:45 AM.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That code wont work, here is an example of how to send text to notepad:
    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
    	PROCESS_INFORMATION pi;
    	STARTUPINFO si;
    	HWND hNotepad, hEdit;
    
    	memset(&si,0,sizeof(si));
    
    	/* Start up notepad */
    	CreateProcess(NULL,"notepad",NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
    	Sleep(1000);	/* Wait for notepad to open */
    	hNotepad = FindWindow(_T("Notepad"),NULL);
    	if(!hNotepad)
    	{
    		printf("Could not find notepad window\n");
    		return EXIT_FAILURE;
    	}
    	hEdit = FindWindowEx(hNotepad,NULL,_T("Edit"),NULL);
    	if(!hEdit)
    	{
    		printf("Could not find edit window\n");
    		return EXIT_FAILURE;
    	}
    	SendMessage(hEdit,WM_SETTEXT,0,(LPARAM)_T("This text will appear in notepad"));
    
    	/* Close handles */
    	CloseHandle(pi.hThread);
    	CloseHandle(pi.hProcess);
    	return EXIT_SUCCESS;
    }

  7. #7
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Ofcourse it will. Have you tried it? Try and see

  8. #8
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by melchior
    no i never tried win32 programming before, but i knew about windows.h for some reason
    Im compiling with MinGW.

    With your code, notepad opens, but where in the code do i place the text that gets sent to notepad?

    Thanks for the help btw
    You put your text in variable char s. After the notepad opens itll set Edit control in notepad whatever text is in char s.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Ofcourse it will. Have you tried it? Try and see
    First of all, you edited the code since I posted. Second of all, it still wont work the way you have it. Execution will block at the system() call, and your FindWindow() functions wont get called until after the notepad window is closed.

  10. #10
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by bithub
    First of all, you edited the code since I posted. Second of all, it still wont work the way you have it. Execution will block at the system() call, and your FindWindow() functions wont get called until after the notepad window is closed.
    Yes i did edited my reply but not the code only the comment i made coz the grammar was kinda incorrect( im very consciuous about my spelling and grammar ) .

    What do you mean be blocked? the findWindow and Findwindowex call will only execute if notepad is already open. So itll perform what it is suppose to do.

    He can use WinExec or ShellExecute if theres something wrong with system().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. Weird special character text after output
    By Dan17 in forum C++ Programming
    Replies: 13
    Last Post: 12-07-2005, 03:41 PM
  3. text output in text editor
    By realjag in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2005, 06:51 PM
  4. sorting characters and output to text file
    By odb1 in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2003, 04:46 PM
  5. Recording console window output to a text file?
    By HolySmiter in forum C++ Programming
    Replies: 3
    Last Post: 02-24-2002, 03:13 PM