- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
VBArray INOUT length known
integer(C_INTPTR_T) :: dptr
...
iRes=SafeArrayAccessData(VBArray,dptr)
dptr address is starting point of one dimensional c array of real{8}
I want to put it in a Fortran array so I can see it in debugger for one reason, but I want the Fortran array to reference the underlying data and not be a copy. Intention would be changes to data in Fortran array would be preserved in dptr array.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Two errors. First, you omitted the size argument I specified to C_F_POINTER. Second, you treated the array as if it were zero-origin, but the array you get from C_F_POINTER is one-origin. (This could be finessed with pointer assignment redeclaring the bounds, but that seems unnecessary.)
I attach my modified df.f90. I didn't change anything else.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Something like this:
use, intrinsic :: iso_c_binding
...
real(c_double), pointer :: array_data(:)
...
call c_f_pointer(transfer(dptr,c_null_ptr),array_data,[array_size])
where array_size is the number of elements in the array (can be a variable or a constant). After this, array_data is your one-dimensional array, no copy done.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I seem to be getting an Exception thrown at 0x00007FF88E912D83 (df.dll) in EXCEL.EXE: 0xC0000005: Access violation writing location 0x0000000000000000 because of the "c_null_ptr" in the transfer. Data is "adptr"(changed from dptr as dptr used as another pointer).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
C_NULL_PTR as the second argument to TRANSFER is just the "mold" - it provides the type to interpret the source (first argument) as. It is not accessed as data. If you got an access violation, then adptr was zero. I suggest adding some logging to your code (write to a file perhaps.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I attached a vs2022 folder with all inside. Excel is 64 bit and projects.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Two errors. First, you omitted the size argument I specified to C_F_POINTER. Second, you treated the array as if it were zero-origin, but the array you get from C_F_POINTER is one-origin. (This could be finessed with pointer assignment redeclaring the bounds, but that seems unnecessary.)
I attach my modified df.f90. I didn't change anything else.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you! I spent all day trying for magic incantations. Intention here was to show that a Excel vba array of doubles used in calculations could be easily sent to a fortran dll and the results sent back in a position(s) in the array for Re: Reading Data from an Excel Spreadsheet? - Page 2 - Intel Communities. One change was made in formula
array_data(4)=PI*array_data(1)*array_data(1)*array_data(2)*array_data(3)/4.0 for weight of cylinder agree with others I had Option 0 on in vba, but array in fortran was [1,4]. Without mucking it up, how to set array_data to be [0,3]?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Add a new array pointer declaration as follows:
real(8), pointer :: array_data(:), array_data1(:)
then replace the C_F_POINTER call with these two lines:
call c_f_pointer(cptr,array_data1,[4])
array_data(0:) => array_data1
I had thought I could do:
array_data(0:) => array_data
but that didn't work, for reasons still unclear to me (it caused array_data to become disassociated - this MIGHT be a bug, and I will investigate further.)
Edit: I can't reproduce the problem in a simple test. Shrug....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you! Sometimes I'm "cset" in my ways if permitted, and I would have still been looking for Rosetta stone.
While trying to find things that applied in books, I was thinking that most write to the overview, not that there is anything wrong with that, but a compendium of use questions and answers would be more useful at times. It seems forums nearly do the job, but there are problems with that like too much archiving or deleting of still relevant material, besides signal to noise.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Is it possible to do this for the two dimensional array? So far I have had no success?
The bounds part works.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Look again at the VB.NET-Safearrays sample, from which you borrowed some code. It illustrates how to access arrays of any dimension.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The looping into each one array goes fine, but combining or assigning those to a Fortran 2d array is where I am not getting.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Please zip and attach what you have now, and I'll take a look.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I've been using x1.xlsm, dfad and dcpp1, no progress with fortran.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You have a lot of code commented out, but I see this:
cptr=transfer(adptr,c_null_ptr)
call c_f_pointer(cptr,array_data,[12])
array_data(0:) => array_data1
That should be array_data1 in the second line.
Where is the code where you're "assigning to a 2D Fortran array"?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I was getting nowhere but locking exceptions, so this was what I was doing . Trying to do in fortran what was done in dcpp1 and so far getting nowhere.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have not tried to edit your sources, but 1) You still have that error on the call to c_f_pointer, and 2) unlike the C++ code, you unlock the SafeArray before accessing it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
When I use
readloop: do
ires=SafeArrayGetElemsize(VBArray)
ires = SafeArrayGetElement (VBArray, indexes(1), loc(adptr))
!
cptr=transfer(adptr,c_null_ptr)
call c_f_pointer(cptr,array_data,[ilen])
call c_f_pointer(cptr,array_data1,[ilen])
array_data(0:) => array_data1
!
! array_data1(3)=PI*array_data1(0)*array_data1(0)*array_data1(1)*array_data1(2)
do i = nbounds, 1, -1
indexes(i) = indexes(i) + 1
if (indexes(i) <= bounds(i)%ub) exit
indexes(i) = bounds(i)%lb
if (i == 1) exit readloop
end do
end do readloop
iRes=SafeArrayUnlock(VBArray)
I get an element size of 8, which does not seem to be an array size. Maybe I'm reading it wrong but GetElement may be just a double.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Got a fortran array with such but maybe order wrong:
iRes=SafeArrayLock(VBArray)
indexes = bounds%lb ! Initialize to all lower bounds
ilen=bounds(2)%ub-bounds(2)%lb
ires=SafeArrayGetElemsize(VBArray)
!ires = SafeArrayPtrOfIndex(VBArray, indx, cptr0)
ires=SafeArrayAccessData(VBArray,adptr)
!ires = SafeArrayGetElement (VBArray, indexes(1), loc(adptr))
!
cptr=transfer(adptr,c_null_ptr)
call c_f_pointer(cptr,array_data,[12])
!call c_f_pointer(cptr,array_data1,[12])
!array_data(0:) => array_data1
!
! array_data1(3)=PI*array_data1(0)*array_data1(0)*array_data1(1)*array_data1(2)
iRes=SafeArrayUnlock(VBArray)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm working on a general dimensional case now, but I found what I needed to do provided I know the ub,lb of each dimension with the following working for the 3X4:
call c_f_pointer(cptr,array_data,[ilen])
call c_f_pointer(cptr,array_data1,[ilen])
array_data(0:) => array_data1
a2d(0:2,0:3)=>array_data
do i=0,2
a2d(i,3)=PI*a2d(i,0)*a2d(i,0)*a2d(i,1)*a2d(i,2)/4
end do
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page