1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Calling Functions From DLLs in 4NT8

Discussion in 'T&T - Scripting' started by Joe Caverly, Aug 29, 2009.

  1. Joe Caverly

    Joined:
    Aug 28, 2009
    Messages:
    730
    Likes Received:
    8
    4NT8 has a very powerful batch language.

    While I reference 4NT8 in this message, as this is the version that I use, you should be able to perform the same tasks in newer versions of the JPSoftware product.

    This batch language shields one from the rigors of developing solutions in C/C++, or other development languages.

    There may be others, like myself, who do enjoy developing solutions by writing source code, compiling, debugging, etc.

    There may be times when the 4NT8 user wants to call a function in a DLL, but does not know how to do this.

    The 4NT8 batch language contains the @WINAPI function to perform this task.

    First, start up 4NT8, and type HELP @WINAPI. This is the function to use to call a function in a Windows DLL.

    (Note also there is an @CAPI function. This will be discussed in another message.)

    4NT8 has a command called DELAY, which allows a batch program to pause for a specified length of time.

    For example, if you type at the prompt;

    Code:
    DELAY 5
    
    the prompt will not be available for 5 seconds.

    With the DELAY command, 4NT8 saves you the trouble of having to call the Sleep function from the kernel32 DLL.

    As an example, type the following at the 4NT8 prompt to do the exact same thing as the DELAY command;

    Code:
    echo %@winapi[kernel32,Sleep,5000]
    
    Note that we used 5000 instead of 5. The sleep function in the DLL accepts the time in milliseconds.

    To do the exact same thing with DELAY, type at the prompt;

    Code:
    DELAY /M 5000
    
    Therefore, 5000 Milliseconds equals 5 seconds.

    Now, let’s make this into a 4NT8 function by typing the following at the prompt;

    Code:
    function Sleep=`%@winapi[kernel32,Sleep,%1]`
    
    To ensure that the function is indeed available from the 4NT8 prompt, type the following;

    Code:
    function
    
    You should see the following;

    Code:
    Sleep=%@winapi[kernel32,Sleep,%1]
    
    Of course, you may already have other functions that you have created, which will also appear.

    Now, at the 4NT8 prompt, type the following;

    Code:
    echo %@Sleep[5000]
    
    Note that we typed 5000, which is 5000 milliseconds, or 5 seconds.

    Note also that a 0 was echoed to the screen. Since this is a function from a DLL, it will always return a value, in this case 0, indicating success.

    There are a multitude of functions available in the Windows API that can be called from 4NT8. Much of the same functionality is already built into 4NT8, so best to check the 4NT8 help file first before re-inventing the wheel.

    Using @WINAPI to call functions from your own DLL

    FreeBASIC is a QuickBASIC-like language for several platforms, including Windows32 systems.

    FreeBASIC makes it easy to create a DLL that you can call from 4NT8.

    Code:
    '' 
     '' mydll -- simple dll test 
     '' 
     '' compile as: fbc -dll mydll.bas 
     '' 
     
    EXTERN "windows-ms" 
     
    '' 
     '' simple exported function 
     '' 
     function AddNumbers ( byval operand1 as integer, byval operand2 as integer ) as integer export 
     
            function = operand1 + operand2 
     
    end function 
     
    END EXTERN
    
    This is a sample that comes with FreeBASIC, that I have modified for use with the Windows32 environment.

    Once the DLL has been compiled, and placed in the C:\WINDOWS\SYSTEM32 directory, you can call the AddNumbers function from 4NT8.

    To create the function;

    Code:
    AddNumbers=`%@winapi[mydll,AddNumbers,%1,%2]`
    
    To call the function;

    Code:
    echo %@AddNumbers[5,6]
    
    If all goes well, the answer 11 will appear on the screen.

    SUMMARY
    While it is very rare that you would need to access a function from the Windows API via 4NT8, it’s nice to know that the functionality is available.

    While the 4NT8 batch language allows you to develop useful functions, developing those same functions in a DLL that can be called from 4NT8 allows you to share your functions with others, without having to provide the source code to the DLL. Note also that functions you develop for a DLL can also be called from Visual Basic, PowerBASIC, Delphi, C/C++, etc.

    I have developed plugins for 4NT8 using Delphi 2.0, and plugins work great for me. No muss, no fuss, just call from the 4NT8 prompt as you would any other function or command.

    If you are not comfortable writing plugins, or using C/C++, Delphi, etc., creating your functions in a DLL may be a solution for you.

    FreeBASIC is the best low-cost method that I know of for doing so.

    Now, if one could write a plugin using FreeBASIC…

    Joe
     

Share This Page