-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt_ans_get.cpp
84 lines (69 loc) · 1.54 KB
/
gpt_ans_get.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "gpt_ans_get.h"
using namespace std;
const int BUF_SIZE = 65536;
string head = "python chat.py ";
string pipename = "\\\\.\\pipe\\my_pipe";
void python_s(string _in)
{
_in = head + _in;
system(_in.c_str());
return;
}
string gpt3_5_ans_get(string s,int t)
{;
thread py(python_s, to_string(t));
py.detach();
string pipename = "\\\\.\\pipe\\my_pipe";
pipename = pipename + to_string(t);
// 创建命名管道
cout << "start create" << endl;
HANDLE hPipe = NULL;
WCHAR wcharTemp[256];
MultiByteToWideChar(CP_ACP, 0, pipename.c_str(), -1, wcharTemp, sizeof(wcharTemp) / sizeof(wcharTemp[0]));
hPipe = CreateNamedPipe(
//L"\\\\.\\pipe\\my_pipe",
wcharTemp,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE |
PIPE_READMODE_MESSAGE |
PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
BUF_SIZE,
BUF_SIZE,
0,
NULL);
if (hPipe == INVALID_HANDLE_VALUE)
{
cout << "Create Read Pipe Error" << endl;
return FALSE;
}
// 等待python端的连接
if (!ConnectNamedPipe(hPipe, NULL))
{
cout << "Connect Failed" << endl;
return FALSE;
}
cout << "connected" << endl;
DWORD dwReturn = 0;
char szBuffer[BUF_SIZE] = { 0 };
//写入python数据
if (!WriteFile(hPipe, s.c_str(), s.size(), &dwReturn, NULL))
{
cout << "Write Failed" << endl;
}
string ans = "";
// 读取python端数据
memset(szBuffer, 0, BUF_SIZE);
if (ReadFile(hPipe, szBuffer, BUF_SIZE, &dwReturn, NULL))
{
szBuffer[dwReturn] = '\0';
ans = szBuffer;
cout << "Read succesfully" << endl;
cout << "cans:" << ans << endl;
}
else
{
cout << "Read Failed" << endl;
}
return ans;
}