有没有一种方法可以将SendMessage从控制台发送到另一个控制台?

时间:2020-11-09 07:31:01

标签: c++ winapi

我有一个项目,在这个项目中有2个控制台应用程序。是否有可能存在一种方法(使用SendMessage()或剪贴板方法)从一个控制台向另一个控制台发送消息?

我的意思是,如果我在控制台1中有一个值,那么在更改值时如何将其传递给控制台2?

2 个答案:

答案 0 :(得分:0)

这两个过程彼此独立。因此,您需要找到在这些分离过程之间进行通信的适当方法。

以下是有关如何执行此操作的示例:

执行此操作的方法有很多,选择取决于您的要求。

答案 1 :(得分:0)

有很多方法可以满足您的需求,例如共享内存,管道,消息队列等。

我在这里使用管道向您介绍进程间通信的具体步骤。

这是发件人代码:

#define BUF_SIZE 4096
HANDLE h_Mypipe = NULL;
//Step 1: Define the pipe name, dot means current host, pipe means pipe
#define MY_NAMED_PIPE   L"\\\\.\\pipe\\Named_Pipe"
int main(int argc, char** argv)
{
    //Step 2: Create a named pipe
    h_Mypipe = CreateNamedPipe(
        MY_NAMED_PIPE, //Create a name for the named pipe
        PIPE_ACCESS_DUPLEX, //Pipeline access mode: PIPE_ACCESS_DUPLEX refers to two-way mode
        PIPE_TYPE_MESSAGE | //Write method of named pipe handle: write to the pipe as a data block
        PIPE_READMODE_MESSAGE | //Read method of named pipe handle: read from the pipe in the form of a data block
        PIPE_WAIT, //Waiting mode of named pipe handle: blocking mode
        PIPE_UNLIMITED_INSTANCES, //The maximum number of instances that the pipeline can create: 1~255,
        0, //The output buffer capacity of the pipeline, 0 means the default size
        0, //The input buffer capacity of the pipeline, 0 means the default size
        1000, //The default waiting timeout of the pipeline, in milliseconds
        NULL); //The security of the pipeline, NULL means the default security provided by windows
    if (h_Mypipe == INVALID_HANDLE_VALUE)
    {
        cout << "Create Named_Pipe Failed..." << endl;
        return 1;
    }
    //Step 3: Wait for the client to connect
    if (!ConnectNamedPipe(h_Mypipe, NULL))
    {
        cout << "Connect Failed..." << endl;
        return 1;
    }
    else
        cout << "Connect Successed..." << endl;

    DWORD wLen = 0;
    DWORD rLen = 0;
    char szBuffer[BUF_SIZE] = { 0 };

    //Step 4: Read and write pipeline
    while (1)
    {
        //Send data to the client
        cin.getline(szBuffer, BUF_SIZE);
        cout << "Server sends data:" << szBuffer << endl;
        if (!WriteFile(h_Mypipe, szBuffer, strlen(szBuffer) + 1, &wLen, NULL))
            cout << "Write Failed..." << endl;
        //Clear buffer
        memset(szBuffer, 0, BUF_SIZE);
    }
    //Step 5: Close the pipeline
    DisconnectNamedPipe(h_Mypipe);
    CloseHandle(h_Mypipe);
    return 0;
}

这是接收代码:

#define BUF_SIZE 4096
HANDLE h_Mypipe = NULL;
//Step 1: Define the pipe name, dot means current host, pipe means pipe
#define MY_NAMED_PIPE   L"\\\\.\\pipe\\Named_Pipe"
int main(int argc, char** argv)
{
    //Step 2: Determine whether there is a named pipe available
    //Function WaitNamedPipe: Wait for a certain pipeline to become available
    //Formal parameter 1: indicates the name of the named pipe
    //Formal parameter 2: NMPWAIT_USE_DEFAULT_WAIT uses the default timeout setting when the pipe is created; NMPWAIT_WAIT_FOREVER waits forever
    if (!WaitNamedPipe(MY_NAMED_PIPE, NMPWAIT_USE_DEFAULT_WAIT))
    {
        cout << "No Named_Pipe Accessible..." << endl;
        return 1;
    }
    else
        cout << "Named_Pipe Accessible..." << endl;
    //Step 3: Open the specified named pipe
    //Function CreateFile: Create or open an object (here the object refers to the pipeline)
    h_Mypipe = CreateFile(
        MY_NAMED_PIPE, //The name of the object (pipe) created or opened
        GENERIC_READ | //Object access method: read access
        GENERIC_WRITE, //Object access method: write access
        0, //Whether the object is shared: 0 means not shared
        NULL, //Pointer to a SECURITY_ATTRIBUTES structure
        OPEN_EXISTING, //
        FILE_ATTRIBUTE_NORMAL, //Set the attributes and flags of the object
        NULL);
    if (h_Mypipe == INVALID_HANDLE_VALUE)
    {
        cout << "Open Named_Pipe Failed..." << endl;
        return 1;
    }
    DWORD wLen = 0;
    DWORD rLen = 0;
    char szBuffer[BUF_SIZE] = { 0 };

    //Step 4: Read and write pipeline
    while (1)
    {
        //Read server-side data
        if (!ReadFile(h_Mypipe, szBuffer, BUF_SIZE, &rLen, NULL))
            cout << "Read Failed..." << endl;
        else
            cout << "The client receives server-side data:" << szBuffer << endl;
        //Clear buffer
        memset(szBuffer, 0, BUF_SIZE);
    }
    //Step 5: Close the pipeline
    CloseHandle(h_Mypipe);
    return 0;
}

它们的工作方式如下:

enter image description here

相关问题