interfacing with a program

This is a discussion on interfacing with a program within the Windows Programming forums, part of the Platform Specific Boards category; Hi My question is, how can a program manipulate some other program, for starters I'm thinking something really basic, just ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25

    interfacing with a program

    Hi

    My question is, how can a program manipulate some other program, for starters I'm thinking something really basic, just accessing a console program( where as it stands and waits for a gets() input) so I could then "manage" it from my program. I am wondering around API functions to do this, looking into what can be done with window handles, but can't find anything to grasp on, so if there is any knowledge on the subject, a link or anything.

    Thanks very much

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Depends on what you want to do, but the usual way is to send messages via SendMessage or use other techniques such as DDE or the like.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    hmmm, thats what I thought the SendMessage between progs, I just made a small little brute force prog. just for sports and another very simple as I said program that waits for a gets input, so I'm trying to access the gets program from the brute_force prog., and as you said and as I thought the SendMessage function comes into play, I'll try it now, be back with feedback

    and thank you

  4. #4
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    back so quick, I looked at the SendMessage and the first param HWND is pretty simple a handle which I got, then the UINT msg, now it is obvious that it takes a unsig. int as one of the predifined macros, now an onward question so I don't coplicate it all too much, how can I send some text to another program, cuz I can't quite visualize it with the basic SendMessage or are there any secrets in the WPARAM and LPARAM?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Well, sending text to another process can be somewhat difficult.
    You'll need to send the WM_SETTEXT message.
    However, since each process has its own virtual memory, you can't just create a string and send it. No, no.
    I think the best way here is to send a custom message to your other process for internal communications. Shared memory could be used to hold the string you want to send. The receiving process can open the shared memory and use the string how it likes.

    Now, this is a very easy task if you are developing using C++. Are you? Or is it C?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    I don't think this would work cuz the simple (gets) prog. has no GetMessage funct. so I don't see a way for him to handle the messages, please correct me if I'm wrong. (and with that being said, like a gazillion questions arose in my head)

  7. #7
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    it's C

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Sending messages is probably the easier way of communicating between processes. There are other ways, I'm sure, though not as simple.
    Generally, however, I find it bad to use two processes for something. And application is one process, not more.
    If you need to execute code while at some other place in your program, the code is busy, such as reading, then you need threads.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Quote Originally Posted by Crazed View Post
    back so quick, I looked at the SendMessage and the first param HWND is pretty simple a handle which I got, then the UINT msg, now it is obvious that it takes a unsig. int as one of the predifined macros, now an onward question so I don't coplicate it all too much, how can I send some text to another program, cuz I can't quite visualize it with the basic SendMessage or are there any secrets in the WPARAM and LPARAM?
    How about using InterProcess Communications (IPC)? An example follows. Start the server first.

    Code:
    //Common.h
    #pragma comment( lib, "user32.lib" )   
    #include <stdio.h>
    #include <windows.h>
    
    #ifndef __MYIPC__
    #define __MYIPC__
    
    #define MAX_MESSAGE_SIZE     0x10000
    #define HEADER_SIZE (2 * sizeof(DWORD) + sizeof(HWND))
    #define SERVERWNDCLASSNAME      "MYSERVERWND001"
    #define WINSERVERNAME       "MYSERVER001"
    #define SETSTRINGLEN(szInputString,iIndex)    ((szInputString)[(iIndex)]='\0')
    #define WM_SEND_DATA           		WM_APP 
    #define WM_SHUTDOWN_SERVER          WM_APP + 1
    
    typedef struct _MYDATASTRUCTURE
    {
        DWORD cbSize;
        DWORD iMessage;
        HWND hClient;
        CHAR Data[MAX_MESSAGE_SIZE - HEADER_SIZE];  
    }MYDATASTRUCTURE,*LP_MYDATASTRUCTURE;
    
    #endif __MYIPC__
    Code:
    //Client.cpp
    #include "common.h"
    
    HWND hServer;
    DWORD pID;
    
    BOOL WINAPI TransferData(DWORD dwMessage, const char *pBuffer, DWORD dwBytes) 
    {
        BOOL bSend;
        COPYDATASTRUCT cpStructData;
        LP_MYDATASTRUCTURE lpMsg;
        cpStructData.cbData = dwBytes + HEADER_SIZE;
        lpMsg = (LP_MYDATASTRUCTURE)LocalAlloc(LPTR,cpStructData.cbData);
        lpMsg->hClient = NULL;
        lpMsg->iMessage = dwMessage;
        lpMsg->cbSize = dwBytes;
        cpStructData.lpData = lpMsg;
        if(pBuffer!=NULL)
        {
            SETSTRINGLEN(lpMsg->Data,dwBytes);
            memcpy((void *) lpMsg->Data,(const void*)pBuffer,dwBytes);
        }
        bSend = SendMessage(hServer,WM_COPYDATA, (WPARAM)hServer,(LPARAM)&cpStructData);
        return(bSend);
    }  
    
    int main(void)
    {
        CHAR szWndName[256] = {0};    
        CHAR szData[256] = {0};
    
        pID = GetCurrentProcessId();
        sprintf(szWndName,"PROCESSID %.X",pID);
        SetConsoleTitle(szWndName);
        if((hServer = FindWindow((LPCTSTR)SERVERWNDCLASSNAME,NULL)) == NULL)
            printf("Please start the server!\n");
        else
        {
            strcpy(szData, "Testing, testing one, two three!!");
            TransferData(WM_SEND_DATA  ,szData, strlen(szData));
            Sleep(5000);
            strcpy(szData, "Let's shutdown the server.");
            TransferData(WM_SHUTDOWN_SERVER ,szData,strlen(szData));
    
        }
        return 0;
    }
    Code:
    //Server.cpp
    #pragma warning( disable : 4715 )
    #include "common.h"
    
    LRESULT CALLBACK WMIPCWNDPROC(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        LP_MYDATASTRUCTURE lpMessage;
    
        if(uMsg == WM_COPYDATA)
        {
            lpMessage = (LP_MYDATASTRUCTURE)((COPYDATASTRUCT*)lParam)->lpData;
            SETSTRINGLEN(lpMessage->Data,(lpMessage->cbSize));
            switch(lpMessage->iMessage)
            {
                case    WM_SEND_DATA:
                    printf("\nOK, we got a message from the other program, Let's print it\n");
                    printf(lpMessage->Data);                
                    break;
                case   WM_SHUTDOWN_SERVER:
                    printf("\nWell, the other program wants us to shutdown. Let's print the message and shut down\n");
                    printf(lpMessage->Data); 
                    PostQuitMessage(0);
                    break;
            }
        }     
        else
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    
    int main(void)
    {
        MSG iMessage;
        BOOL bVal;
        WNDCLASS WMClass;
    
        SetConsoleTitle(WINSERVERNAME);
        WMClass.hInstance = GetModuleHandle(NULL);
        WMClass.lpszClassName = SERVERWNDCLASSNAME;
        WMClass.cbClsExtra = 0;
        WMClass.cbWndExtra = 0;
        WMClass.hbrBackground = NULL;
        WMClass.hCursor = NULL;
        WMClass.hIcon = NULL;
        WMClass.lpszMenuName = NULL;
        WMClass.style = CS_GLOBALCLASS;
        WMClass.lpfnWndProc = WMIPCWNDPROC;
        if(RegisterClass(&WMClass) == 0)
            printf("Class registration failed!\n");
        else
        {
            if(CreateWindow((LPCTSTR)SERVERWNDCLASSNAME, NULL, 0, 0, 0, 0, 0,
                NULL, NULL, WMClass.hInstance, NULL) == NULL)
            {
                printf("Can't create server window\n");
            }
            else
            {
                printf("Server successfully started\n");
                while((bVal = GetMessage(&iMessage, NULL, 0, 0)) && (bVal != -1))
                    DispatchMessage(&iMessage);
            }               
        } 
        return 0;
    }

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Quote Originally Posted by Crazed View Post
    back so quick, I looked at the SendMessage and the first param HWND is pretty simple a handle which I got, then the UINT msg, now it is obvious that it takes a unsig. int as one of the predifined macros, now an onward question so I don't coplicate it all too much, how can I send some text to another program, cuz I can't quite visualize it with the basic SendMessage or are there any secrets in the WPARAM and LPARAM?
    WM_COPYDATA is what you want - google it!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Quote Originally Posted by iMalc View Post
    WM_COPYDATA is what you want - google it!
    The IPC example I posted does use the WM_COPYDATA method.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21