# include <fstream>
using namespace std;
extern "C" {
int write(char* filename, char* all_lines) {
ofstream file;
file.open(filename);
if (file.is_open()) {
file << all_lines << endl;
file.close();
return 1;
} else {
file.close();
return 0;
}
}
}
from ctypes import *
from os import getcwd
from sys import platform
if platform in ['linux', 'linux2']:
end = '.so'
elif platform in ['win32', 'cygwin', 'msys']:
end = '.dll'
writer = CDLL(f'./writer{end}')
a = c_char_p(f'{getcwd()}\\file.txt'.encode())
b = c_char_p(b'Help me pls')
writer.write.argtypes = [c_char_p, c_char_p]
writer.write.restype = c_int
print(writer.write(a, b))
g++ -c -fextended-identifiers -std=c++20 -fPIC writer.cxx -o writer.o
g++ -shared -fextended-identifiers -std=c++20 -Wl,-soname,writer.dll -o writer.dll writer.o
from os import getcwd
from ctypes import *
dll = cdll.LoadLibrary("./writer.dll")
dll.write.argtypes = [c_char_p, c_char_p]
dll.write(f"{getcwd()}\\filename.txt".encode(), b"Hello world!")
# include <fstream>
using namespace std;
extern "C" __declspec(dllexport) void write(char* filename, char* lines) {
ofstream file;
file.open(filename, ios_base::out);
if (file.is_open()) {
file << lines << endl;
file.close()
}
}
clang++ -c -o writer.o writer.cxx
clang++ -shared -v -o writer.dll writer.o
a = c_char_p(f'{getcwd()}\\file.txt'.encode())
b = c_char_p(b'Help me pls')