I've been investigating this for a while now and I feel that I've come to a brick wall therefore I'd like to request your help.
I've done a cpp DLL that calls a python script as soon as it is injected into the metin2 client.
I've successfully printed some messages in the chat with using the chat import.
But I've not been successful with any other function.
This is a basic example that print in the chat a message but blows up afterwards:
Code:
chat = __import__("XXsRHxy")
chr = __import__("chr")
chat.AppendChat(7, "test")
for i in xrange(100000):
if(chr.HasInstance(i) != 0):
if(chr.GetInstanceType(i) == 6):
chr.SelectInstance(i);
chat.AppendChat(7, str(i));
Code:
if (PyErr_Occurred())
PyErr_Print();
I wanted to do a simple scan for other players and in the future interact with them. Any suggestion is appreciated
I think it's not relevant but here is the cpp code:
Code:
#include "pch.h"
#include <Python.h>
#include <thread>
#include <iostream>
#include <vector>
bool executePythonFile(const char* file) {
int result = 0;
char path[256] = { 0 };
strcat_s(path, file);
printf("Executing Python file: %s\n", path);
PyObject* PyFileObject = PyFile_FromString(path, (char*)"r");
if (PyFileObject == NULL) {
printf("%s is not a File!\n", path);
goto error_code;
}
result = PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "MyFile", 1);
if (result == -1) {
printf("Error executing python script!\n");
goto error_code;
}
else {
printf("Python script execution complete!\n");
Py_DECREF(PyFileObject);
return true;
}
if (PyErr_Occurred())
PyErr_Print();
error_code:
PyErr_Print();
Py_DECREF(PyFileObject);
return 0;
}
void init() {
AllocConsole();
freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
std::cout << "### START ###" << std::endl;
executePythonFile("C:\\code\\testing.py");
FreeConsole();
}
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH: {
// sdk::utilities::h_module = (HMODULE)hDllHandle;
init();
FreeLibraryAndExitThread(hModule, 0);
}break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}






says and in addition, you can call python functions directly using c++ with certain python commands like "PyObject_CallObject"

