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

Edit a subroutine at runtime

luksx
Beginner
1,721 Views
Can I edit subroutine at runtime?
For exemple: A program that can edit a source code or some like that!
0 Kudos
10 Replies
mecej4
Honored Contributor III
1,721 Views
Fortran is a compiled language. Explain the relation between "A program that can edit" and "a source code".

What is it that you are concerned with? A text editor written in Fortran? A Fortran program that modifies its own source code?

The next question is what you expect the effect of editing to be.

0 Kudos
JVanB
Valued Contributor II
1,721 Views
Yes, in many senses. For example, it's possible to write a code generator in Fortran that can write a subroutine. If you want the same program to call that subroutine, either you need an interpreter that can (slowly) parse the code and carry out its commands or you need to invoke a compiler, such as ifort, either as a DLL (but I don't think ifort has a DLL form that can be invoked at runtime as some compilers do) or via execute_command_line to compile the subroutine to a DLL that can be loaded via LoadLibrary after which the subroutine's address can be converted into a c_funptr via GetProcAddress which can be converted to a Fortran procedure pointer via c_f_procpointer and finally invoked in Fortran code through said Fortran procedure pointer.

Alternatively you can use VirtualAlloc to get to a chunk of memory that your program can read, write, and execute and then write machine language code directly into that memory and then convert the procedure address of that code from integer(kind=c_intptr_t) to type(c_funptr) via transfer and again use c_f_procpointer to get a Fortran procedure pointer, but if you could write that machine code yourself you probably wouldn't be asking this question...
0 Kudos
luksx
Beginner
1,721 Views
mecej4,

I just need A Fortran program that modifies its own source code. Or a program that can modify a DLL code.

Repeat Offender,

Can you give me an example? How I can modify a DLL with a subroutine? Or how I can use ifort?
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,721 Views
Post #2 gave you an outline

1) run your program
2) from in your program modify some source file
3) from your program run the compiler to produce an object file with exports for use in DLL
4) from your program run the linker to produce the DLL
5) Load the DLL using Windows API
6) Find the entry point using Windows API
7) Call your newly loaded subroutine
---
8) Unload DLL using Windows API
9) repeat steps 2:7 for next go around

Look in the MSDN for "How to load a DLL from your program"
Or use Google.

Examples should be available

Jim Dempsey
0 Kudos
Steven_L_Intel1
Employee
1,721 Views
There's a Fortran sample DLL\DynamicLoad provided with Intel Visual Fortran. You'll need some care in establishing the environment so that you can run the compiler. Probably a .bat file that calls the compilervars.bat and then builds the DLL would be best.
0 Kudos
JVanB
Valued Contributor II
1,721 Views
Searching for the terms Fortran LoadLibrary GetProcAddress seemed to produce appropriate examples.
0 Kudos
luksx
Beginner
1,721 Views
jimdempseyatthecove,

I didn't understand the "2"!
I wanna build DLL from a subroutine!
At runtime, Build a DLL!
I know how i load

How i Can generate a DLL with a compiled program?

I want to create a program that can create a DLL.

For example: The Microsoft Word can create .doc files
My program must create .dll files

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,721 Views
>>I didn't understand the "2"!

Modification of a source file from within your application.

Often these things are done using a program template file, in which you inject your algorithms. If you do not like this route then you can hard-wire in the basic outline code. Having it in a seperate file makes for easier changes (e.g. for environment differences).

Once the resultant souce file(s) is(are) produced, then you use systemqq or some other system call to spawn a process in which to run a Batch file to perform the build of the DLL (this was mentioned by someone else on this forum).

The title ofyour thread is "Edit a subroutine at runtine"(I assume you mean runtime). Which to me means you have existing code that you wish to modify. (i.e. read in, modify, write out, then re-compile, then link into DLL, then, if necessary, unload older version of DLL, load in new version of DLL, make calls to get entry points of interest) Then the new DLL is ready for use.

Jim Dempsey
0 Kudos
luksx
Beginner
1,721 Views

Once the resultant souce file(s) is(are) produced, then you use systemqq or some other system call to spawn a process in which to run a Batch file to perform the build of the DLL (this was mentioned by someone else on this forum).

Thanks for all hints, but can someone give an example?

0 Kudos
luksx
Beginner
1,721 Views
Please, somebody help me. Give me a example, I don't Know how i do this.
("then you use systemqq or some other system call to spawn a process in which to run a Batch file to perform the build of the DLL")
How make a batch file for this...
0 Kudos
Reply