COM Server Array Passing

COM Server Array Passing

I'm trying to create a COM server passing dynamically sized arrays back and forth - i.e. the server doesn't know how big the arrays will be before they arrive and return arrays are dynamically within the server.

I don't see how this can be done using the COM Server wizard which requires that the bounds of arrays are specified.

Does anyone have any idea how to do this or even better any example code. The examples in the documentation do not cover the case of array properties at all.



14 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Does this link help?

thanks very much, I'll get a copy and try it.

I'm puzzled by the wording of your question. At _some_ point, the array size _is_ know at which time you can pass it here and there with ease.

If your arrays (or data structures for that matter) are large using COM involves significant overhead. Stuff all into a a byte array and then use the CopyMemory API to pass a pointer to the byte array on to the recipient.

Gerry T.

I'm sure you're right and that it is possible. I think its more a limitation in the COM Sever Wizard for Visual Fortran than a fundamentally impossible task.

The COM server wizard seems to require that the rank and bounds of array properties are specified at compile time. I can compile a com server where I dynamically allocate the property that I am trying to pass but get a RankMismatch exception when the .NET runtime I'm using to communicate with the server attempts to read the property.

The COM wizard rightly assumes that you're going to use SafeArrays. The RankMismatch error is probably because you're passing rectangular arrays to .NET whose rows are Fortran columns. Pass the transpose and cross your fingers.

Gerry T.

But as far as I can tell fortran doesn't support safe arrays so I'm attempting to pass fundamentally different types across the interface? Even the one-dimensional case only works if the array I pass is of fixed size at compile time - any attempt to dynamically allocate an array and pass it (both as an allocatable array or a pointer) and I just get zeros passed across the interface.


Just because SafeArrays are not part of Fortran doesn't mean you can't use them. CVF has a number of modules with interfaces to the appropriate APIs.

Also, look at its modification at

Gerry T.


> Just because SafeArrays are not part of Fortran
> doesn't mean you can't use them. CVF has a number of
> modules with interfaces to the appropriate APIs.

Where would I find this, as part of the installation? Don't seem to have it.

> Also, look at its modification at

Couldn't get here as anonymous user. Anywhere else I might find it?

> HTH,
> Gerry T.



The samples are installed under Program FilesMicrosoft Visual StudioDF98SAMPLES. You may or may not have the samples installed to your hard disk, depending on what version of CVF you first installed and whether you selected a standard or custom install. The samples are on the CD-ROM under INFODFSAMPLES.


Retired 12/31/2016


I've attached the, courtesy of John Termine.

Gerry T.

Where is John, anybody?

Thanks very much.

Seem to have lost or not installed my samples. Will be playing hunt the CD for the rest of the day.


Re John: Hope I'm not too discourteous by publishing a private e-mail from last September:

Hi Jugoslav,

How are you doing? And how are all my VF friends? Hope everyone is well. I am alive! Well? Well that's a tougher question! Nah, everything's fine. Just doing different stuff. Over the last year I became responsible for the I/T departments for my division in the US. Haven't written any code in a long while. Bummer. Actually, I wrote a small C++ program a little while ago (something to do with Windows Terminal Server API), nothing all that cool really. Hope you are well. Apolgies for the SEVEN MONTH lag in replying.


We miss you John.


I've looked at the ARRAYS example and come up with this simple bit of code f90 code which I build into a dll...

! mc1.f90
! FUNCTIONS/SUBROUTINES exported from mc1.dll:
! mc1 - subroutine
subroutine mc1(a)

!dec$ attributes dllexport, stdcall, alias : "mc1" :: mc1
!dec$ attributes reference :: a

use dfcom

implicit none

! Interface.
pointer (a, SADummy) !Pointer to a SafeArray structure
integer SAdummy

! Locals
integer :: iRes, lbound, ubound

iRes = SafeArrayGetUBound(a, 1, ubound)
iRes = SafeArrayGetLBound(a, 1, lbound)

open(99, file="c:temptest.txt", status='unknown')
write(99,*)"upper bound = ",ubound
write(99,*)"lower bound = ",lbound

end subroutine mc1

When I declare the interface to be Double[] and attempt to call it with the .NET runtime by passing it a Double[] I get two zeros in the log file for the upper and lower bounds and the array back intact.

If I declare the interface to be "ByRef a As Double[]" I get the same result in the log file but the array a is returned truncated down to only the first element.

Can anyone shed some light on this?



Leave a Comment

Please sign in to add a comment. Not a member? Join today