版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_22659021/article/details/81134219
dll.h
#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
#define DLLIMPORT __declspec(dllexport)
#else
#define DLLIMPORT __declspec(dllimport)
#endif
#include <string>
#include <vector>
using namespace std;
typedef vector<int> vec;
extern "C" __declspec(dllexport) vec test(vec a);
extern "C" __declspec(dllexport) int* test2();
extern "C" __declspec(dllexport) void test3(char a[], int b[]);
extern "C" __declspec(dllexport) int* test4(int a[]);
extern "C" __declspec(dllexport) int check(char a[]);
extern "C" __declspec(dllexport) void insert(char a[], int b[]);
extern "C" __declspec(dllexport) int find(char a[]);
//extern "C" __declspec(dllexport) void insert(string state, vec actions);
#endif
dllmain.cpp
/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <unordered_map>
BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
{
break;
}
case DLL_PROCESS_DETACH:
{
break;
}
case DLL_THREAD_ATTACH:
{
break;
}
case DLL_THREAD_DETACH:
{
break;
}
}
/* Return TRUE on success, FALSE on failure */
return TRUE;
}
//using namespace std;
//typedef vector<int> vec;
unordered_map<string,vec>q_table;
//typedef unordered_map<string,vec> unm;
//typedef unordered_map<string,int> unm2;
//typedef unordered_map<int, int> map_int_t;
int find(char a[])
{
// cout << a << endl;
string state;
state = a;
unordered_map<string,vec>::iterator it;
it = q_table.find(state);
vec temp = it->second;
int Max = -10000000;
int i=0,j=0;
for(i=0; i<6; i++)
{
// cout << temp[i] << endl;
if (temp[i] > Max)
{
Max = temp[i];
j = i;
// cout << "j: " << j << endl;
}
}
return j;
}
void insert(char a[], int b[])
{
string state;
vec actions;
state = a;
for(int i=0; i<6; i++)
actions.push_back(b[i]);
q_table.insert(make_pair(state, actions));
}
int check(char a[])
{
string state;
state = a;
if (q_table.find(state) != q_table.end())
return 1;
else
return 0;
}
vec test(vec a)
{
cout << "hello" << endl;
a[0] = 1;
return a;
}
int* test4(int a[])
{
for(int i=0; i<6; i++)
a[i] = a[i] + 5;
return a;
}
int* test2()
{
int b[] = {1,2,3,4,5,6};
int *p;
p = b;
return p;
}
void test3(char a[], int b[])
{
cout << a << endl;
string state;
vec actions;
state = a;
for(int i=0; i<6; i++)
actions.push_back(b[i]);
// cout << b[i] << endl;
test2();
}
test.py
from ctypes import *
game = CDLL("aaa.dll")
#字符串
a = "-1 0 2 3 4"
state = c_char_p(bytes(a, 'utf-8'))
#六个元素的整数数组
acitons = (c_int*6)()
acitons[0] = 1
acitons[1] = 2
acitons[2] = 3
acitons[3] = 41
acitons[4] = 100
acitons[5] = 6
#check: state存在则返回值为1,不存在则返回值为0
flag1 = game.check(state)
print(flag1)
if flag1 == 0:
#insert: 插入state,actions
game.insert(state, acitons)
else:
#find: 返回state对应的最大值的actions下标
Max = game.find(state)
print(Max)
flag2 = game.check(state,acitons)
print(flag2)
if flag2 == 0:
game.insert(state,acitons)
else:
Max = game.find(state)
print("Max: " , Max)