I am trying to call C++ functions from a dll using ctypes in python. My current issue is the function seems to return a large int, either positive or negative, instead of the char pointer I expect it to. If I convert that int to a c_char_p and call .value on it, it kills my kernel every single time. I've looked all over this site and in the docs too and can't figure this out. A lot of the things I have seen on this site even throw errors for me, like passing in strings to ctypes opjects and functions when they should be byte objects or something similar. Below is my c++ code that is turned into a dll and the python code I am using to call functions from the dll. Please if anyone can help me, that would be awesome. SaySomething is the function in question. Thanks.
TestLibrary.h
#pragma once
#ifdef TESTLIBRARY_EXPORTS
#define TESTLIBRARY_API __declspec(dllexport)
#else
#define TESTLIBRARY_API __declspec(dllexport)
#endif
#include <windows.h>
#include <cstring>
#ifdef __cplusplus
extern "C"
{
#endif
TESTLIBRARY_API char* SaySomething(const char* phrase);
#ifdef __cplusplus
};
#endif
TestLibrary.cpp
#include "stdafx.h"
#include "TestLibrary.h"
#include <iostream>
TESTLIBRARY_API char* SaySomething(const char* phrase)
{
char* p = new char [100];
p = "string something";
return p;
}
tester2.py
import ctypes
dbl = ctypes.c_double
pChar = ctypes.c_char_p
pVoid = ctypes.c_void_p
libName = (r"D:\Documents\Coding Stuff\Visual Studio 2017\Projects\TestLibrary"
r"Solution\x64\Debug\TestLibrary.dll")
x = ctypes.windll.LoadLibrary(libName)
x.SaySomething.argtypes = [pChar]
x.SaySomething.restypes = pChar
phrase = b"Hi"
phrase = pChar(phrase)
res = x.SaySomething(phrase)