- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Can I edit subroutine at runtime?
For exemple: A program that can edit a source code or some like that!
For exemple: A program that can edit a source code or some like that!
Link Copied
10 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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...
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Searching for the terms Fortran LoadLibrary GetProcAddress seemed to produce appropriate examples.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
>>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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting jimdempseyatthecove
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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...
("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...

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page