Software Archive
Read-only legacy content
17061 Discussions

How to export subroutines from Main program to DLL that links with main executable.

Intel_C_Intel
Employee
760 Views
Hi,

I am able to export subroutines from DLL to Main executable that links DLL using DLLEXPORT and DLLIMPORT compiler directive attributes.

But I would like to export symbols from Main executable to DLL that links with Main executable.

Is there any way of doing this?

Thanks for the help
Madhav.

P.S: I know almost all UNIX machines allow this. but I am not able to do this on Win 2000.
0 Kudos
6 Replies
Steven_L_Intel1
Employee
760 Views
I don't know of any way to do this on Windows. The only option I can think of is to pass the address of a routine as a callback to the DLL code.

Steve
0 Kudos
Intel_C_Intel
Employee
760 Views
Exporting functions from EXEs is not that common. Callbacks are much more common.

But anyway, if you must, you can do it with dllexport, or with LoadLibrary/GetProcAddress, or with a .def file too. The same ways you'd export a routine in a DLL. If you don't use LoadLibrary/GetProcAddress, then you'll have to work out the issue of the dependencies between projects. But that's doable too (e.g., you can make the dll project a dependency of the exe project and just add the required .lib to the dll project)

-John

 
!  dll_lib.f90  
! 
function volume(radius) result  
    !dec$ attributes dllexport :: volume 
    implicit none 
    interface  
        function get_pi() result  
        !dec$ attributes dllimport :: get_pi 
            real :: r 
        end function get_pi 
    end interface 
    ! Variables 
    real, intent(in) :: radius 
    real :: r, pi 
    ! Body of dll_lib 
    pi = get_pi() 
    r = 4./3. * pi * radius**3 
end function volume 
 
!  console.f90  
! 
program cons 
    implicit none 
    interface  
        function volume(radius) result  
        !dec$ attributes dllimport :: volume 
        real, intent(in) :: radius 
        real :: r 
        end function 
    end interface 
 
    real v 
    v = volume(10.0) 
    print *, 'The volume of a sphere of radius 10 is ', v 
 
end program cons 
 
function get_pi() result  
    !dec$ attributes dllexport :: get_pi 
    real ::r 
 
    r = 3.14159   ! close enough for this! 
end function get_pi 
0 Kudos
Intel_C_Intel
Employee
760 Views
Hi, John,

I have tried the method you have said. But there is one problem here. Earlier we used to build two static libraries(e.g., main and user) and link them together to create a final executable. Main library will not be changed by users and has calls to user library. users (if need to) write their own subroutines (names are fixed and can not be changed by users) and then link with main library to create their own executable. user subroutines does call subroutines defined in main library. For this we used to send Executable, and Main library and template of user subroutines(which user uses to write their own code).
Now we want to send only one executable and a user dll, which user can change at any time, if needed. Almost all unix machines (IBM, SUM, HP, Linux, DEC, SGI) allow this. I mean user dll (for unix machines shared objects) can call subroutines defined in main program, and vice versa. Users need not build the executable again, all they need is just compile user subroutines and create a shared object out of it. set the path variable to point to current shared object.

I thought similar procedure exists in windows. unfortunately, from john's message I don't think we can acheive the same, without relinking with library every time I write user subroutine.

For IBM, we had to create dummy subroutine calls in user dll first time we create an executable to make all the subroutines available to user dll. Also we need to export and import symbols manually. (similar to dllexport and dllimport)
I am wondering if I can use something similar to dummy subroutine call in windows also.

Any help in this regard is really appreciated.
- Madhav.
0 Kudos
Steven_L_Intel1
Employee
760 Views
Isn't "compile user routines and create a shared object" the same as creating a DLL from these user routines?

Steve
0 Kudos
Intel_C_Intel
Employee
760 Views
Hi, John,

I have tried the method you have said. But there is one problem here. Earlier we used to build two static libraries(e.g., main and user) and link them together to create a final executable. Main library will not be changed by users and has calls to user library. users (if need to) write their own subroutines (names are fixed and can not be changed by users) and then link with main library to create their own executable. user subroutines does call subroutines defined in main library. For this we used to send Executable, and Main library and template of user subroutines(which user uses to write their own code).
Now we want to send only one executable and a user dll, which user can change at any time, if needed. Almost all unix machines (IBM, SUM, HP, Linux, DEC, SGI) allow this. I mean user dll (for unix machines shared objects) can call subroutines defined in main program, and vice versa. Users need not build the executable again, all they need is just compile user subroutines and create a shared object out of it. set the path variable to point to current shared object.

I thought similar procedure exists in windows. unfortunately, from john's message I don't think we can acheive the same, without relinking with library every time I write user subroutine.

For IBM, we had to create dummy subroutine calls in user dll first time we create an executable to make all the subroutines available to user dll. Also we need to export and import symbols manually. (similar to dllexport and dllimport)
I am wondering if I can use something similar to dummy subroutine call in windows also.

Any help in this regard is really appreciated.
- Madhav.
0 Kudos
Intel_C_Intel
Employee
760 Views
Yup. shared object is same as creating DLL. On unix machines it refers to shared object or shared library.
-Madhav
0 Kudos
Reply