Skip to content

Instantly share code, notes, and snippets.

@udzura
Created December 11, 2019 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save udzura/dbd7d93dcca069b5ad9a063b157dc744 to your computer and use it in GitHub Desktop.
Save udzura/dbd7d93dcca069b5ad9a063b157dc744 to your computer and use it in GitHub Desktop.
from ctypes import *
lib = CDLL("./libupdateptr.so")
i = c_int()
print(lib.update_int(byref(i)))
print(i.value)
print(lib.update_int2(byref(i)))
print(i.value)
j = c_long()
print(lib.update_long(byref(j)))
print(j.value)
k = c_ulong()
print(lib.update_ulong(byref(k)))
print(k.value)
l = c_longlong()
print(lib.update_long_long(byref(l)))
print(l.value)
m = c_double()
print(lib.update_double(byref(m)))
print(m.value)
require 'fiddle/import'
module UpdatePtr
extend Fiddle::Importer
dlload "./libupdateptr.so"
extern "int update_int(*int)"
extern "int update_int2(*int)"
extern "int update_long(*long)"
extern "int update_ulong(*unsigned long)"
extern "int update_long_long(*long long)"
extern "int update_double(*double)"
end
i = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
p UpdatePtr.update_int(i)
p [i[0, i.size], i[0, i.size].unpack("i!")]
p UpdatePtr.update_int2(i)
p [i[0, i.size], i[0, i.size].unpack("i!")]
j = Fiddle::Pointer.malloc(Fiddle::SIZEOF_LONG)
p UpdatePtr.update_long(j)
p j[0, j.size].unpack("l!")
k = Fiddle::Pointer.malloc(Fiddle::SIZEOF_LONG)
p UpdatePtr.update_ulong(k)
p k[0, k.size].unpack("L!")
l = Fiddle::Pointer.malloc(Fiddle::SIZEOF_LONG * 2)
p UpdatePtr.update_long_long(l)
p l[0, l.size].unpack("q!")
m = Fiddle::Pointer.malloc(Fiddle::SIZEOF_DOUBLE)
p UpdatePtr.update_double(m)
p m[0, m.size].unpack("d")
default: libupdateptr.so updateptr.o
updateptr.o: updateptr.c
gcc -fPIC -c ./updateptr.c -o updateptr.o
libupdateptr.so: updateptr.o
gcc -shared updateptr.o -o libupdateptr.so
#define INT_VALUE 5
#define INT_VALUE2 1234567
#define LONG_VALUE 68719486735L
#define ULONG_VALUE 1125899906852623UL
#define LONG_LONG_VALUE 1125899906852624LL
#define DOUBLE_VALUE 3.14159265
int update_int(int* ptr) {
*ptr = INT_VALUE;
return 0;
}
int update_int2(int* ptr) {
*ptr = INT_VALUE2;
return 0;
}
int update_long(long* ptr) {
*ptr = LONG_VALUE;
return 0;
}
int update_ulong(unsigned long* ptr) {
*ptr = ULONG_VALUE;
return 0;
}
int update_long_long(long long* ptr) {
*ptr = LONG_LONG_VALUE;
return 0;
}
int update_double(double* ptr) {
*ptr = DOUBLE_VALUE;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment