Silverfrost Forums

Welcome to our forums

Accesing module variables in a DLL

2 Jul 2019 9:55 #23887

It doesn't seem possible to access global variables in a module in a DLL. Module (in a separate file, to be made as a DLL)

!ftn95$free
MODULE TESTMOD	   
 INTEGER*4		:: QT_NWIN	=  99
END MODULE

Main program (separate file, loads previous DLL)

!ftn95$free

program main
  USE TESTMOD

  SAVE
  print*,' QT_NWIN =', QT_NWIN
	
END
 

When linking, it says:

WARNING the following symbols are missing:
MODULE TESTMOD

and when I run it, I can see that it has a random value of QT_NWIN.

3 Jul 2019 5:57 #23891

When you compile a 32 bit module you get an interface in a .mod file and (usually) object code in a .obj file. Similarly for 64 bit code.

When you build something that accesses the module then the access at compile time is via a USE statement the uses the .mod file whilst the module .obj file is used at link time.

The process that you use to build a DLL is essentially the same as building an executable but there is no main program. A module still works via a .mod file and a .obj file. When linking you indicate that you want a DLL.

Routines that are in the DLL (within modules or not) are linked to the executable when it is loaded for running but there is no equivalent linking of module data. So the address of QT_NWIN in the DLL will not automatically be the same as the address of QT_NWIN in the executable. If you need to share data then the sharing must be done in some other way.

3 Jul 2019 8:16 #23894

Thanks for that. But why is there the warning

WARNING the following symbols are missing: MODULE TESTMOD

Thanks

3 Jul 2019 8:19 #23895

I don't understand that. I would need a demonstration from your code and build process.

3 Jul 2019 8:36 #23896

If you copy the code above and make it into two files testmod.for app_main.for

and you do ftn95 /windows /sparam 0 /zero /f2k /cfpp testmod.for slink /3gb -dll -exportall testmod.obj ftn95 /windows /sparam 0 /zero /f2k /cfpp app_main.for slink /3GB testmod.dll app_main.obj

You will see the above warning

3 Jul 2019 8:46 #23897

I just realised the last line should be

slink /3GB app_main.obj testmod.dll

i.e. first the object file and then the DLL file, but the warning is still there.

3 Jul 2019 9:39 #23898

I don't know exactly what is happening but there is nothing to export from this module. You can only export subprograms. You can't export variables.

3 Jul 2019 9:43 #23899

Even if you add a subroutine the same thing happens. It does work (i.e. you can call the subroutine), but you get this warning. I am hoping it is not an indication of something more sinister...

!ftn95$free

MODULE TESTMOD
 
 INTEGER*4		:: QT_NWIN	=  99

 contains

subroutine testmodinit
  QT_NWIN = 100
end subroutine

END MODULE
3 Jul 2019 12:59 #23901

I don't know why there is this report of a missing symbol.

If there are no runtime errors then I would treat it as benign.

The QT_NWIN in the main program will not have the same address as the QT_NWIN in the DLL and in my testing the QT_NWIN in the main program cannot be accessed.

So if you need to put a module into a DLL then you will have to do something special in order to access the module data from the executable.

Please login to reply.