Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

using module in dll

jungwoong
Beginner
875 Views
I defined a module in main program and then I want to use the module in dll.
And then I want to return the calulated results in dll to main program.
The following code does not work, please let me know how to do that.
please correct the following code workable.

The following is the code in DLL file.
subroutine test
!DEC$ ATTRIBUTES DLLEXPORT :: test
use input
integer i, j
do i = 1, 5
do j = 1, 5
arr(i, j) = i + j
enddo
enddo
end subroutine

The following is the code in main program uses the dll.
module input
real arr(5,5)
end module
interface
subroutine test
!DEC$ ATTRIBUTES DLLIMPORT :: input
use input
end subroutine
end interface

program main
call test
do i=1,5
write(*,*) (arr(i,j), j=1,5)
end do
stop
end program
0 Kudos
8 Replies
hweisberg
Beginner
875 Views

The DLLIMPORT statement should probably read

!DEC$ ATTRIBUTES DLLIMPORT :: test

Also, in the project linker settings,you need to add the import .LIB file with its path and library name.

0 Kudos
jungwoong
Beginner
875 Views
This program can be run, but the result value are not correct.
I added the import .LIB file with its path in the project linker settings.
I want to use module in dll file.
Please let me know how to transfer the value from dll to main program.
!========================
! source code in dll file
!========================
module input
integer k
end module
subroutine test
!DEC$ ATTRIBUTES DLLEXPORT :: test
use input
k=9999
end subroutine
!============================
! source code in main program
!============================
module input
integer k
end module
program q
use input
interface
subroutine test
!DEC$ ATTRIBUTES DLLIMPORT :: test
use input
end subroutine
end interface
call test
write(*,*) k
pause "Press Return to Exit"
end
0 Kudos
hweisberg
Beginner
875 Views

The CVF documentation under Creating Fortran Dlls, and itsassociated examples in the Samples folder, shows how to transfer valiables between a Dll and a main program using exported common blocks. The easiest way to do what you want might be to emulate one of the examples.

You can also transfer data using modules, or using arguments of exported functions, but I'm not sure whether there is any example for you to copy. In your current code, the main program and the Dll each have their own copies of the module. The CVF documentation section "Exporting and Importing Data Objects in Modules" discusses how to do what you want by giving data objects in the module the DLLEXPORT property.

0 Kudos
jungwoong
Beginner
875 Views

Icannot find out the detailed explanation and example code in the CVF documentation section "Exporting and Importing Data Objects in Modules".

please anyone let me know where I can find the sample code about Exporting and Importingmodule in dll.

0 Kudos
Jugoslav_Dujic
Valued Contributor II
875 Views

My first advice would be: don't do that. Dllexporting/importing variables is a bad idea, error-prone and with nointerface separation (which is one ofprimary reasons why you create a dll). Global data in general are a bad thing, and shared global data between a dll and exe are an even worse thing. See examples here, and here.

Afar better approach is to use argument lists of dll routines for data exchange. If there are a lot ofvariables, you can pack them into a derived TYPE to reduce long argument-lists.

Jugoslav

0 Kudos
Steven_L_Intel1
Employee
875 Views
In general, you can't have a DLL reference a variable in a main program - at least not directly. The way to approach this is to move the module to the DLL and add an ATTRIBUTES DLLEXPORT directive for the variables. Build the main program against the .mod file created when the DLL is built and link to the DLL's export library.
0 Kudos
faderibera
Beginner
875 Views

I have a similar problem, but my main program is in Java.

The main program calls a Fortran dll (dll_1) which contains a module, and then this dll (dll_1) calls another Fortran dll (dll_2) which needs to use a lot of variablesfrom the module of dll_1.

Do I need to create a third Fortran dll to share the variables?

Can I load dll_1 from dll_2?

I am veryworried of efficiency and velocity of the program so I prefer the fastest solution for this problem.

I can't joint both dll into one, because dll_2 is generated in the execution time of the main program in Java.

Thanks

Message Edited by faderibera on 06-23-2004 07:02 PM

0 Kudos
Steven_L_Intel1
Employee
875 Views
The best way to do this, in any case, is to pass the variables you want to reference as an argument, or have routines in the DLL that set and retrieve values.
In the case of Java, I don't think you can get at the DLL exported variables at all. You will have to do this with callable routines or have the variables passed as arguments.
0 Kudos
Reply