BYVAL: Difference between revisions
Jump to navigation
Jump to search
Code by Galleon
Using ALIAS to create a program SUB or FUNCTION using QB64 SDL ONLY
QB64 version 1.000 and up produce standalone executables. External DLL files must be distributed with your program.
Note: QB64 versions prior to 1.000 require all default DLL files to either be with the program or in the C:\WINDOWS\SYSTEM32 folder.
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
m (Removed protection from "BYVAL") |
No edit summary Tag: Reverted |
||
Line 15: | Line 15: | ||
** It's always a good idea to try declaring Windows API libraries without a {{Parameter|Library_filename}} first, as most Windows headers are already included in QB64 source. | ** It's always a good idea to try declaring Windows API libraries without a {{Parameter|Library_filename}} first, as most Windows headers are already included in QB64 source. | ||
* Begin the {{Parameter|Library_filename}} with '''./''' or '''.\''' to make it relative to the path where your source file is saved, so you can keep all your project files together. | * Begin the {{Parameter|Library_filename}} with '''./''' or '''.\''' to make it relative to the path where your source file is saved, so you can keep all your project files together. | ||
* {{Parameter|Procedure_name}} is any program procedure name you want to designate by using [[ALIAS]] with the {{Parameter|Library_procedure}} name. | * {{Parameter|Procedure_name}} is any program procedure name you want to designate by using [[ALIAS]] with the {{Parameter|Library_procedure}} name. | ||
* {{Parameter|Library procedure}} is the actual procedure name used inside of the library or header file. | * {{Parameter|Library procedure}} is the actual procedure name used inside of the library or header file. | ||
Line 39: | Line 39: | ||
{{PageExamples}} | {{PageExamples}} | ||
''Example 1:'' Using an '''SDL''' library procedure as a program SUB procedure to move the mouse pointer to a coordinate (works in versions prior to 1.000): | ''Example 1:'' Using an '''SDL''' library procedure as a program SUB procedure to move the mouse pointer to a coordinate (works in versions prior to 1.000): | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|DECLARE LIBRARY}} | {{Cl|DECLARE LIBRARY}} | ||
{{Cl|SUB}} SDL_WarpMouse ({{Cl|BYVAL}} column {{Cl|AS}} {{Cl|LONG}}, {{Cl|BYVAL}} row {{Cl|AS}} {{Cl|LONG}}) 'SDL procedure name | {{Cl|SUB}} SDL_WarpMouse ({{Cl|BYVAL}} column {{Cl|AS}} {{Cl|LONG}}, {{Cl|BYVAL}} row {{Cl|AS}} {{Cl|LONG}}) 'SDL procedure name | ||
Line 56: | Line 56: | ||
{{Cl|SUB}} MouseMove (x {{Cl|AS}} {{Cl|LONG}}, y {{Cl|AS}} {{Cl|LONG}}) | {{Cl|SUB}} MouseMove (x {{Cl|AS}} {{Cl|LONG}}, y {{Cl|AS}} {{Cl|LONG}}) | ||
SDL_WarpMouse x, y 'call SDL library procedure | SDL_WarpMouse x, y 'call SDL library procedure | ||
{{Cl|END SUB}} | {{Cl|END SUB}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{small|Code by Galleon}} | {{small|Code by Galleon}} | ||
:''Explanation:'' The SDL Library is included and loaded with QB64 versions prior to 1.000, so these procedures are directly available for use. | :''Explanation:'' The SDL Library is included and loaded with QB64 versions prior to 1.000, so these procedures are directly available for use. | ||
<center>'''Using [[ALIAS]] to create a program SUB or FUNCTION''' using '''QB64 SDL ONLY'''</center> | <center>'''Using [[ALIAS]] to create a program SUB or FUNCTION''' using '''QB64 SDL ONLY'''</center> | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|SCREEN}} 12 | {{Cl|SCREEN}} 12 | ||
{{Cl|DECLARE LIBRARY}} | {{Cl|DECLARE LIBRARY}} | ||
Line 71: | Line 71: | ||
MouseMove 100, 100 | MouseMove 100, 100 | ||
{{Cl|_DELAY}} 2 | {{Cl|_DELAY}} 2 | ||
MouseMove 200, 200 | MouseMove 200, 200 | ||
{{CodeEnd}} | {{CodeEnd}} | ||
:''Explanation:'' When a Library procedure is used to represent another procedure name use [[ALIAS]] instead. Saves creating a SUB! | :''Explanation:'' When a Library procedure is used to represent another procedure name use [[ALIAS]] instead. Saves creating a SUB! | ||
Line 77: | Line 77: | ||
''Example 2:'' Don't know if a C function is defined by C++ or QB64? Try using empty quotes. | ''Example 2:'' Don't know if a C function is defined by C++ or QB64? Try using empty quotes. | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|DECLARE LIBRARY}} "" | {{Cl|DECLARE LIBRARY}} "" | ||
{{Cl|FUNCTION}} addone& ({{Cl|BYVAL}} value&) | {{Cl|FUNCTION}} addone& ({{Cl|BYVAL}} value&) | ||
{{Cl|END}} DECLARE | {{Cl|END}} DECLARE | ||
{{CodeEnd}} | {{CodeEnd}} | ||
:''Explanation:'' The C function 'addone' exists in a library QB64 already links to, but it hasn't been defined as a C function or a QB64 function. By using "" we are telling QB64 the function exists in a library which is already linked to and that it must define the C function before calling it, as well as allowing QB64 code to call it. Trying the above code without the "" will fail. | :''Explanation:'' The C function 'addone' exists in a library QB64 already links to, but it hasn't been defined as a C function or a QB64 function. By using "" we are telling QB64 the function exists in a library which is already linked to and that it must define the C function before calling it, as well as allowing QB64 code to call it. Trying the above code without the "" will fail. |
Revision as of 17:01, 22 January 2023
The DECLARE LIBRARY declaration allows the use of external library SUB and FUNCTION procedures supported by QB64.
Syntax
- DECLARE [DYNAMIC|CUSTOMTYPE|STATIC] LIBRARY [{"Library_filename"|"Headerfile"}]
- {SUB|FUNCTION} [procedure_name Template:KW] library_procedure ([[[:Template:KW]]] parameter Template:KW, ...)
- .
- . 'other SUBs or Functions as required
- .
- END DECLARE
- The Library_filename is needed if a Library is not already loaded by QB64. Do not include the .DLL, LIB or .H file extension.
- It's always a good idea to try declaring Windows API libraries without a Library_filename first, as most Windows headers are already included in QB64 source.
- Begin the Library_filename with ./ or .\ to make it relative to the path where your source file is saved, so you can keep all your project files together.
- Procedure_name is any program procedure name you want to designate by using ALIAS with the Library_procedure name.
- Library procedure is the actual procedure name used inside of the library or header file.
Library Types
- DYNAMIC links a program to functions in dynamically linkable libraries. At present, only .DLL files are supported
- CUSTOMTYPE is already implied when using DECLARE DYNAMIC LIBRARY. This type of library just allows the same flexibility to apply when referencing STATIC libraries that are used to refer to dynamic libraries. Supports shared object (*.so) libraries.
- STATIC is the same as DECLARE LIBRARY except that it prioritizes linking to static libraries (*.a/*.o) over shared object (*.so) libraries, if both exist. As Windows doesn't use shared libraries (DLLs are different) this does not affect Windows users.
Description
- The declaration can be used with C++ sub-procedures, Windows API and QB64 SDL (versions prior to 1.000)/OpenGL (version 1.000 and up) Libraries.
- Library filenames can be listed to combine more than one DLL or Header file name or path into one DECLARE LIBRARY block.
- C procedures can use a header file name. File code must be included with program code. Do not include the .h extension.
- Parameters used by the Library procedure must be passed by value (BYVAL) except for STRING characters.
- When using a procedure from an unsupported Dynamic Link Library (DLL file) use DECLARE DYNAMIC LIBRARY.
- The _OFFSET in memory can be used in CUSTOMTYPE, STATIC and DYNAMIC LIBRARY declarations.
- Declarations can be made inside of SUB or FUNCTION procedures. Declarations do not need to be at program start.
- NOTE: It is up to the user to document and determine the suitability of all Libraries and procedures they choose to use! QB64 cannot guarantee that any procedure will work and cannot quarantee any troubleshooting help.
Examples
Example 1: Using an SDL library procedure as a program SUB procedure to move the mouse pointer to a coordinate (works in versions prior to 1.000):
DECLARE LIBRARY SUB SDL_WarpMouse (BYVAL column AS LONG, BYVAL row AS LONG) 'SDL procedure name END DECLARE SCREEN _NEWIMAGE(640, 480, 256) 'simulate screen 12 with 256 colors RANDOMIZE TIMER DO _DELAY 1 x = RND * 640: y = RND * 480 LINE (x, y)-STEP(10, 10), RND * 100 + 32, BF MouseMove x + 5, y + 5 LOOP UNTIL LEN(INKEY$) 'any keypress quits END SUB MouseMove (x AS LONG, y AS LONG) SDL_WarpMouse x, y 'call SDL library procedure END SUB |
- Explanation: The SDL Library is included and loaded with QB64 versions prior to 1.000, so these procedures are directly available for use.
SCREEN 12 DECLARE LIBRARY SUB MouseMove ALIAS SDL_WarpMouse (BYVAL column&, BYVAL row&) END DECLARE _DELAY 2 MouseMove 100, 100 _DELAY 2 MouseMove 200, 200 |
- Explanation: When a Library procedure is used to represent another procedure name use ALIAS instead. Saves creating a SUB!
Example 2: Don't know if a C function is defined by C++ or QB64? Try using empty quotes.
DECLARE LIBRARY "" FUNCTION addone& (BYVAL value&) END DECLARE |
- Explanation: The C function 'addone' exists in a library QB64 already links to, but it hasn't been defined as a C function or a QB64 function. By using "" we are telling QB64 the function exists in a library which is already linked to and that it must define the C function before calling it, as well as allowing QB64 code to call it. Trying the above code without the "" will fail.
- Note: What libraries are or aren't automatically used in the linking process is not formally defined, nor is it guaranteed to stay that way in future versions of QB64.
See also:
- DECLARE DYNAMIC LIBRARY
- SUB, FUNCTION
- BYVAL, ALIAS
- C Libraries, DLL Libraries, Windows Libraries
- Port Access Libraries
- SQL Client