扩展程序调用本地exe

具体可以参考官方例子 那个是python的

这里是c++

#include <iostream>
#include <string>
#include <fstream>
#include <Windows.h>
#include <urlmon.h>
#include <windows.h>
#include <map>
#include <iostream>
#pragma comment (lib,"urlmon")

using namespace std;

void func(int num)
{
    string message = "{\"text\": \"This is a response message\",\"num\": " + std::to_string(num) + " }";
    unsigned int len = message.length();
    //先输出长度
    cout << char(((len >> 0) & 0xFF))
        << char(((len >> 8) & 0xFF))
        << char(((len >> 16) & 0xFF))
        << char(((len >> 24) & 0xFF));
    //再输出内容并结束
    cout << message;
    cout.flush();
}
string getInput()
{
    std::string input;
    unsigned int reqLen = 0;
    cin.read(reinterpret_cast<char*>(&reqLen), 4);
    for (int i = 0; i < reqLen; i++) {
        input += getchar();
    }
    return input;
}

int main(int argc, char* argv[]) {
    string input = getInput();
    MessageBoxA(NULL, input.c_str(), NULL, NULL);
    int num = 0; 
    while (num < 1)
    {        
        //Sleep(2 * 1000);
        func(num++);
    }
    system("pause");
    return 0;
}

 

 

最后:

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;

//json数据, 先长度,再字符串
void func(string message)
{
    unsigned int len = message.length();
    cout.write(reinterpret_cast<char*>(&len), 4);
    cout << message;
    cout.flush();
}

string getInput()
{
    std::string input;
    unsigned int len = 0;
    cin.read(reinterpret_cast<char*>(&len), 4);
    //这里要考虑的是空格问题
    for (int i = 0; i < len; i++) {
        input += getchar();
    }
    return input;
}

int main(int argc, char* argv[]) {
    string input = getInput();
    MessageBoxA(NULL, input.c_str(), NULL, NULL);
    int num = 0; 
    func("{\"text\": \"This is a response message\",\"num\": 5 }");
    system("pause");
    return 0;
}

 

posted @ 2022-04-18 15:16  冰糖葫芦很乖  阅读(40)  评论(0编辑  收藏  举报