Python Forum
Do I better to give up to use ctypes?!?! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Do I better to give up to use ctypes?!?! (/thread-647.html)



Do I better to give up to use ctypes?!?! - Yuji3131 - Oct-26-2016

I've been trying to call Fortran.dll file with ctypes in Python 2.7. 

however I've struggled with how to send and get Array and Matrix. Strictly speaking, I'd like to know how to set argtypes and restypes for array and matrix.

I probably know how to do it with numpy but at this time I can't use numpy_modules but I can only use Python standard modules like ctypes. 
Actually I want to work with Rhino IronPython though, my Rhino IronPython is something wrong so I can't install numpy.

By the way, this is my Fortran and Python code to exchange array.
subroutine add_array(ii)
    implicit none
    real(8),dimension(0:1),intent(inout) :: ii(0:1)
    dimension b(0:1)
    integer i
    
    do i =1,2
         b(i)=1
         ii(i) = ii(i) + b(i)
    end do
    
end subroutine
stop
end
------------------------------------------------------------------------
from ctypes import*

addmodule = cdll.LoadLibrary("C:\\Users\\Owner\\add_arraytest.dll")
addmodule.test_.argtypes = [ POINTER(c_int),POINTER(c_int)]
addmodule.test_.restype = c_void_p

twoIntegers = c_int * 2
ii = twoIntegers(1,2)

ii(0) = c_int(ii[0])
ii(1) = c_int(ii[1])

addmodule.test_(byref(ii[0]),byref(ii[1]))

for i in ii: print i,
these program are collapsed... doesn't work at all...
I know C doesn't have executable statement to control whole array ,it should be controlled one by one
if there is no way to  to send and get Array and Matrix, please tell me!!!

Thanks a million in advance...


RE: Do I better to give up to use ctypes?!?! - Larz60+ - Oct-26-2016

why don't you just convert the FORTRAN to python?


RE: Do I better to give up to use ctypes?!?! - Yuji3131 - Oct-27-2016

because there are so many Fortran programs in my laboratory in my university, and my professor and classmate in my laboratory still using it.

the reason why i cling fortran is because I try to connect Grasshopper Python in Rhinoceros to Fortran analysis program so it can analyze object while I making object.
I believe my attempt is gonna be benefit for my classmate to display their works clearly.