Results 1 to 24 of 24

Thread: Ways of changing code during runtime

  1. #1

    Thread Starter
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    Ways of changing code during runtime

    Absolutley any suggestions on this matter will be good, cuz i have no idea how to do this
    thx

  2. #2
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Ways of changing code during runtime

    Hi,

    I'm afraid it is in no way clear what you are talking about. Are you talking about editing your code in Debug mode, as you can do in VB6? In that case, to the best of my knowledge, it is not possible at all. You need to stop, edit and recompile.
    Or maybe you are talking about adding code to an event, a button click for example. You would use the Addhandler to accomplish this - it is very useful whne you create objects at runtime.

    Please clarify.

    zaza

  3. #3

    Thread Starter
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    Re: Ways of changing code during runtime

    Hi,
    here's an example:
    i have an app running on a remote pc which is controlled by me from my pc, it can do stuff like delete file, copy file, but i accidently forgot to put in a move file code, so now i need to add code to an already compiled and working app.
    I can send it a file which will contain the needed code, but how do i get the app to run it?

  4. #4
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Ways of changing code during runtime

    You have a few options.

    1) You could replace the executable with another which contains the updated code. i.e. an update for your program.

    2) You could put it in a second executable and run that one whenever you need to move a file.

    There's no way to tell code that you want it to do something different unless you change the code. To do that, once it is compiled, is very much non-trivial unless you've been crafty enough to build it in in advance. For example, if your app works by opening a text file and extracting a list of the functions that are available to it and the locations of various sets of code to run these properties, then doing just that. In other words, if your main app is just a frontend for a load of different code in separate files and it pulls these in in order to function. But I suspect you're talking about modifying an exe, in which case you'd be better of just replacing it with an updated version.

    zaza

  5. #5

    Thread Starter
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    Re: Ways of changing code during runtime

    Well i dont nesseserly have to modify the exe, and extracting functions from files sounds promissing, could you plz give me an example on this
    Thx

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Ways of changing code during runtime

    Don't release half-finished software. Thats the easiest way.

  7. #7

    Thread Starter
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    Re: Ways of changing code during runtime

    Quote Originally Posted by wossname
    Don't release half-finished software. Thats the easiest way.
    i wrote: "Here's an example:"
    I dont have no exe on a remote pc, it was just an example

  8. #8
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Post Re: Ways of changing code during runtime

    I think there is a way to re-compile an existing exe.
    Not sure if this is what your looking for but here is what I've got for vb6


    EXE Compiler - Readme
    .: Introduction
    This code sample demonstrates how to create your own exe files.
    Not a direct approach. The program writes to the end of an existing exe file. The exe file can then access this data and work with it.

    The compiled exe does not require any extra files for working (except the vb runtimes, of course!)

    .: In more detail... :.
    prjRdfce.vbp is the program (when compiled) that acts as the template for the exe. It is compiled to 'rdfce.ext'.
    prjExeCompiler is the program used to create new exe files. It first makes a copy of rdfce.ext to the file name supplied by the user, say test.exe. It then writes user supplied data to the end of test.exe.
    When test.exe is run, it reads the data contained in itself and work accordingly (all that code is done in prjRdfce)
    Hopefully, that explains the working.

    .: Uses :.
    For creating apps like self extracting archives, stand alone photo album, etc etc. If you have done something with it then please write to me.
    .: Copyright and stuff
    © 2002, Anoop Sankar
    For distribution and usage see the notice on the source.

    .: Contact
    e-mail : [email protected]
    Website : www.smilehouse.cjb.net


    Title: [ Exe Compiler ]
    Description: This code demonstrates how to compile exe files using vb code, like the winzip self extractor.
    This file came from Planet-Source-Code.com...the home millions of lines of source code
    You can view comments on this code/and or vote on it at: http://www.Planet-Source-Code.com/vb...40144&lngWId=1

    The author may have retained certain copyrights to this code...please observe their request and the law by reviewing all copyright conditions at the above URL.




    '--------------------------------------------------------
    ' Copyright 2002, Anoop Sankar
    'You may freely use, modify and distribute this source
    'code, provided that you do not remove this message.
    'But, you are NOT allowed to distribute the compiled
    'version (.EXE,.DLL,.OCX etc etc.) of this program
    'or any program which uses the below code without my
    'consent.
    '
    'If you modified something, put your name below..
    '
    'Orginal Code : Anoop Sankar ([email protected])
    'Modified by : No one so far
    '
    'Last Update : Oct 25,2002
    'Visit www.smilehouse.cjb.net for more source code
    '-------------------------------------------------------
    '
    'Purpose of the project is to create a stand alone exe
    'file from VB code. It doesn't do this directly, but uses
    'a simple work around. I think this is quite a useful
    'way to do this. If you think otherwise or if you have
    'other methods, I would love to hear from you.
    '
    'The method is .. write to the end of a pre-created exe
    'file, in binary mode.
    '
    'Check 'readme.html' for more details.
    '
    '-------------------------------------------------------
    VB Code:
    1. Private Sub cmdCompile_Click()
    2.  
    3.     'This is were all the action takes place
    4.  
    5.     On Local Error GoTo errTrap
    6.  
    7.     Dim BeginPos As Long            'variable to store the start of data
    8.     Dim PropBag As New PropertyBag  'property bag to store the data
    9.     Dim varTemp As Variant          'for file writing
    10.    
    11.     'Below section loads data into the property bag.
    12.     With PropBag
    13.         .WriteProperty "Caption", txtCaption.Text
    14.         .WriteProperty "Text", txtText.Text
    15.         .WriteProperty "Picture", imgPic.Picture
    16.         .WriteProperty "Protected", chkPass.Value
    17.         .WriteProperty "Password", txtPass.Text
    18.         'You may add your own propery using the syntax
    19.         'PropBag.WriteProperty "<property name>",<property value>
    20.         'As you might have noticed, property value can be anything
    21.         'string, picture, or numerical.
    22.     End With
    23.    
    24.     'rdfce.ext is the template we use to create our exe.
    25.     'RDFCE = Renamed Dummy For Creating Executable ;-)
    26.     '(source of that file is included too)
    27.    
    28.     'first copy that file to the user provided file name.
    29.     FileCopy App.Path & "\rdfce.ext", App.Path & "\" & txtExeFile.Text
    30.    
    31.     'now open the file in binary mode
    32.     Open App.Path & "\" & txtExeFile.Text For Binary As #1
    33.         BeginPos = LOF(1)   'the point were we add extra data
    34.                
    35.         varTemp = PropBag.Contents
    36.                
    37.         Seek #1, LOF(1)
    38.         Put #1, , varTemp   'write data
    39.         Put #1, , BeginPos  'write starting point of extra data
    40.    
    41.     Close #1
    42.  
    43.     MsgBox "Exe File created without a problem", vbInformation, "Compilation Done"
    44.     Exit Sub
    45.  
    46.     'Thats it! The exe is compiled.
    47.     'Read the prjExeDummy (prjRdfce.vbp) to find how the
    48.     'compiled exe works.
    49.  
    50. errTrap:
    51.     'to err is electronic
    52.     Msg = "There was an error during compilation" & vbCrLf
    53.     Msg = Msg & vbCrLf & Err.Description
    54.     MsgBox Msg, vbCritical, "Error"
    55. End Sub
    56.  
    57.  
    58. Private Sub lblAddPic_Click()
    59.  
    60.     On Local Error GoTo errTrap
    61.    
    62.     ComDLG.CancelError = True
    63.     ComDLG.ShowOpen
    64.  
    65.     imgPic.Picture = LoadPicture(ComDLG.FileName)
    66.  
    67. errTrap:
    68.  
    69. End Sub
    70.  
    71. Private Sub chkPass_Click()
    72.    
    73.     On Local Error Resume Next
    74.    
    75.     If chkPass.Value > 0 Then
    76.         txtPass.Enabled = True
    77.         txtPass.SetFocus
    78.     Else
    79.         txtPass.Enabled = False
    80.     End If
    81.    
    82. End Sub


    In the other form

    VB Code:
    1. Dim PropBag As New PropertyBag      'the property bag
    2.  
    3. Private Sub Form_Load()
    4.     'On Local Error Resume Next
    5.    
    6.     Dim BeginPos As Long
    7.     Dim varTemp As Variant
    8.    
    9.     Dim byteArr() As Byte
    10.    
    11.     Open App.Path & "\" & App.EXEName & ".exe" For Binary As #1
    12.         Get #1, LOF(1) - 3, BeginPos    'get the start position of data
    13.  
    14.         Seek #1, BeginPos               'seek to data start
    15.         Get #1, , varTemp               'get property bag contents
    16.        
    17.         byteArr = varTemp
    18.         PropBag.Contents = byteArr      'load property bag
    19.    
    20.         PropBag.WriteProperty "LOF", LOF(1) 'a few extra props
    21.         PropBag.WriteProperty "BeginPos", BeginPos
    22.     Close #1
    23.        
    24.    
    25.     'password protection
    26.     'I know that this is not tight, but just for a demo
    27.     If Val(PropBag.ReadProperty("Protected", "0")) > 0 Then
    28.         Dim PassInp As String
    29.        
    30.         PassInp = InputBox("Enter password:", "Password Required")
    31.        
    32.         If PassInp <> PropBag.ReadProperty("Password") Then
    33.             MsgBox "Password not valid", vbCritical, "Nice Try!"
    34.             End
    35.         End If
    36.     End If
    37.    
    38.     With PropBag
    39.         txtText.Text = .ReadProperty("Text")
    40.         Set imgPic.Picture = .ReadProperty("Picture")
    41.         Me.Caption = .ReadProperty("Caption")
    42.     End With
    43.  
    44. End Sub
    45.  
    46. Private Sub cmdExeInfo_Click()
    47.     'display exe stats
    48.    
    49.     lblInfo(0).Caption = App.EXEName & ".exe"
    50.     lblInfo(1).Caption = PropBag.ReadProperty("LOF") & " bytes"
    51.     lblInfo(2).Caption = PropBag.ReadProperty("BeginPos") & " bytes"
    52.     lblInfo(3).Caption = (PropBag.ReadProperty("LOF") - PropBag.ReadProperty("BeginPos")) & " bytes"
    53.     lblInfo(4).Caption = Format((PropBag.ReadProperty("BeginPos") / PropBag.ReadProperty("LOF")) * 100, "0.00") & " %"
    54.  
    55.     picInfo.Visible = True
    56. End Sub
    57.  
    58. Private Sub Label3_Click()
    59.     picInfo.Visible = False
    60. End Sub
    Last edited by TTn; Oct 9th, 2005 at 07:18 AM.

  9. #9

    Thread Starter
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    Re: Ways of changing code during runtime

    thx, but in this matter recompiling an exe isn't the solution (thou i can use this later )
    anyway, like zaza wrote, i need a code that can read code from file and somehow procces it

  10. #10
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: Ways of changing code during runtime

    Hmmm are you sure?
    I dont think you can do it that way, but my example fits your request.
    it can do stuff like delete file, copy file, but i accidently forgot to put in a move file code, so now i need to add code to an already compiled and working app.
    I can send it a file which will contain the needed code, but how do i get the app to run it?
    The existing app needs it's "file content" added unto, so that it can run it.
    Unless, you had originally designed it to accept files, to execute as code.
    You did not. So

    Maybe I missunderstood your objectives.

  11. #11

    Thread Starter
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    Re: Ways of changing code during runtime

    Well the reason why i dont want to recompile an exe in this situation is because the app is supposed to run on a remote machine without shutdown all the time, and becides, recompiling an exe file on a remote machine will require the user to do it as i understood, thats why i want it to accept files with code and execute them, so all i need is an explanation how to make it execute code from file
    Thx

  12. #12
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: Ways of changing code during runtime

    Well the reason why i dont want to recompile an exe in this situation is because the app is supposed to run on a remote machine without shutdown all the time,
    I think you'd better think about doing so.


    becides, recompiling an exe file on a remote machine will require the user to do it as i understood,
    I dont think this example would require the user to do it.
    You would be adding an application, to do the recompilation of the original.

    thats why i want it to accept files with code and execute them,
    so all i need is an explanation how to make it execute code from file
    Thx
    If you want an existing app, that was not designed to do this, to somehow know what to execute from a file... good luck I can't help you with that...
    Anyone.... anyone.... na didn't think so.

    If you are planning for future conditions, then it should be possible to design the original application. Not easy but plausible.
    For example I use an App.ini file, to execute startup code.
    This code is essentially saved during runtime by the user, in the ini file.
    When they choose preferences etc.
    I could elaborate on this example?
    Last edited by TTn; Oct 9th, 2005 at 08:19 AM.

  13. #13

    Thread Starter
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    Re: Ways of changing code during runtime

    Quote Originally Posted by TTn
    For example I use an App.ini file, to execute startup code.
    You mean the code that's supposed to be in the form_load() is in the app.ini file and it executes when the app starts?

  14. #14
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Ways of changing code during runtime

    I think TTn means that user preferences, such as background colour, font size etc can be savd in an ini file and then read in when the user executes the app. That way, user-defined preferences can be maintained.

    What I was talking about earlier would have to be built in from scratch but, as Wossy says, usually it's better to actually define your objectives and finish your app rather than make it too open-ended. Of course, if you wanted the user to be able to import exe's as well, then that's a bit different...

    So, what you might do is write a text file:

    "Bobbins", "c:\Bobbins.exe"
    "Cobblers", "c:\Cobblers.exe"
    "doodah", "c:\doodah.exe"

    and use the streamreader or a simple sequential file access (Fileopen, Fileclose methods) to get a name and a file path out of a settings file. Then you could match up the function that you have selected with the exe file you've extracted from the settings file and run that exe.
    Of course, if you wanted to pass things to the executable to get it to perform actions on your data, you'd need to build that in or have a separate text-file writing system to export the data...

    It's all starting to get a bit complicated. There are a lot of things that you won't be able to foresee and then you'll wish you'd just replaced the exe.

    zaza

  15. #15

    Thread Starter
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    Re: Ways of changing code during runtime

    well i guess you're right, it is starting to get complicated, and i guess replacing the exe is the way to go here, though i still cant understand, is it really impossible to import code from file into a sub or a function?

  16. #16
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Ways of changing code during runtime

    Once it is compiled into an executable, things don't exist in the same way. It doesn't just take a text file and rename the tag as ".exe". You can't just tack an extra Sub onto the end and expect things to work as normal. It'd involve editing the exe itself which is not a particularly enticing proposition. It really is easier to replace the file entirely.
    Anyway, at some stage you'd have to stop executing the file in order to edit it so why not just replace it instead?

    zaza

  17. #17

    Thread Starter
    Addicted Member Filik's Avatar
    Join Date
    Aug 2005
    Posts
    208

    Re: Ways of changing code during runtime

    Hmm...
    Have you ever heard of net bot's? the code of a net bot can be modified at runtime with outside scripts, the problem is i don't know what language are the bots written or anything else bout them, so i guess until i found out im stuck with replacing exe's

  18. #18
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: Ways of changing code during runtime

    I think TTn means that user preferences, such as background colour, font size etc can be savd in an ini file and then read in when the user executes the app. That way, user-defined preferences can be maintained.
    Well almost, It would similarly be built from scratch.


    Anyway, at some stage you'd have to stop executing the file in order to edit it so why not just replace it instead?.
    Indeed.

  19. #19
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Ways of changing code during runtime

    Sounds a lil suspicious to me.. and what are these net bots you are talking about?? Chat bots??? All chatbots do is post a text file or strings of data into random rooms... just a matter of loading a different text file with different responses...

  20. #20
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Re: Ways of changing code during runtime

    Quote Originally Posted by Filik
    Hi,
    here's an example:
    i have an app running on a remote pc which is controlled by me from my pc, it can do stuff like delete file, copy file, but i accidently forgot to put in a move file code, so now i need to add code to an already compiled and working app.
    I can send it a file which will contain the needed code, but how do i get the app to run it?

    I could be mistaken or drunk, but...

    Move = Copy + Delete


    Copy the file to the new location, then delete the original.

    Also, if the source and destination is the same partition, rename might work as well.

  21. #21
    Member
    Join Date
    Sep 2005
    Posts
    54

    Re: Ways of changing code during runtime

    Think he was using the move thing as an example of where the source code would have to be updated. Still, the nature of the example seems a bit... how shall I say... suspicious...

    But anyway, I've written an application where I work and there are two exe's... the first one runs when the user clicks the icon on the desktop and displays a 'please wait' dialog and checks the server for an updated application - if there is an updated exe it downloads it and runs it, if there is no updated one then it runs the existing exe.

    Another project I worked on I developed my own scripting language with a syntax similar to BASIC that didn't compile, instead it was 'interpreted'. This meant I could write add-ins and new functions at a later date. Obviously this code ran slower and there were lots of limitations.

    You mentioned web bots... I think they're a bad comparison as their behaviour/construction is somewhat different to the VB app you described in your example.

  22. #22
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Re: Ways of changing code during runtime

    If I were to write a web bot, it would use MSScript to do the update. It would output a pre-created script to a .vbs file, run it, and then the bot's process would terminate (leaving the script running), the script would download a new exe over the existing one. When done, it would run the updated exe. Upon running, the exe would delete the script file as the file would probably be outdated.

    Any time you create a remote file managment system of any kind, (when I design it) it always has these three features: a) file transfer, b) file execution, and c) authentication.

    Using some combination of those features, you'll always be able to remotely add/remove features.

  23. #23
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Ways of changing code during runtime

    This is a breach of the AUP by the way, just thought you guys should know that.
    I don't live here any more.

  24. #24
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Ways of changing code during runtime

    In alot of cases where constant update is required a scripting language is created for the program (an interpreted language), or just advanced settings, depending how dynamic these changes need to be.

    I have once written a program to read a language sort of hybrid to the old mIRC, VB and C

    a small game written in it
    Code:
    game{
    $a{0}
    $r{$rnd * 9 + 1}
    ref{1}
    $a{%a + 1}
    ref{2}
    input{guess the number between 1-10 to win}
    $g{$input}
    if(%g < 1){msgbox{number must be between 1-10}goto{2}}
    if(%g > 10){msgbox{number must be between 1-10}goto{2}}
    $d{%g - %r}
    if(%d = 0){msgbox{you win! it took you %a attempts}if(%a < 3){msgbox{well done!}}goto{0}}
    if(%d > 0){msgbox{[%g] lower}}
    if(%d < 0){msgbox{[%g] higher}}
    goto{1}
    ref{0}
    }
    very ugly, but works and gets the job done. Gets very tricky to code the interpretor though, especially with loading of functions etc. But point being, these scripts can be modified during runtime of the application.

    If you need to frequently recode your program, you need this to be either settings, or script.

    replacement of exe's all the time is never nice, as wossname said, debug your programs before you release them to avoid bug fixing. If something MUST change in your program then it shouldn't be hard coded.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width