An Example

A very simple example is a DLL with a function that will add one to any Long-integer passed to it as a parameter:

#COMPILE DLL

FUNCTION AddOne ALIAS "AddOne" (BYVAL x AS LONG) EXPORT AS LONG

  AddOne = x + 1

END FUNCTION

The ALIAS keyword is used to indicate the capitalization that PowerBASIC will assign the function. In Win32, all exported (and imported) Sub and Function names are case-sensitive. If the ALIAS keyword was omitted, PowerBASIC will capitalize the exported name and this could cause " Missing DLL entry point" errors if the calling code did not match the capitalization exactly.

By default, all Subs and Functions in PowerBASIC are private, which means they cannot be seen outside of the DLL. The EXPORT keyword is used on the Sub or Function definition line to indicate that the routine is to be exported, i.e., made accessible to applications and other DLLs.

When compiled into a DLL, AddOne is visible to outside applications. A Visual Basic program needs only include a prototype, or a DECLARE statement for the function, in order to call it as if it were a VB function:

DECLARE FUNCTION AddOne LIB "ADDONE.DLL" ALIAS "ADDONE" (BYVAL x&) AS LONG

AddOne is then accessible from within your Visual Basic code:

a& = 4

b& = AddOne( a& ) ' returns 5

If AddOne were not exported, Visual Basic would generate a run-time error when the example code attempts to call it.

If the EXPORT keyword is not used in the Sub or Function definition, the procedure will not be visible to outside applications. See the Visual Basic documentation for more information on calling DLLs from within Visual Basic code.

By using the ALIAS keyword in the DLL source code, you can have PowerBASIC export the Sub or Function using any capitalization you want. You can use the ALIAS clause to export the Sub or Function with a completely different name, in order to enhance or disguise the internal Sub or Function name:

' Exported as "ADDONE1"

FUNCTION AddOne1 (BYVAL x&) EXPORT AS LONG

 

' Exported as "AddOne2"

FUNCTION AddOne2 ALIAS "AddOne2" (BYVAL x&) EXPORT AS LONG

 

' Exported as "ExprtFnctn1"

FUNCTION AddOne3 ALIAS "ExprtFnctn1" (BYVAL x&) EXPORT AS LONG

Because the name after the ALIAS keyword is in quotes, the compiler will not convert it to upper case. Note that the name in the ALIAS clause is the name that you would use to access the Sub or Function from Visual Basic. Likewise, when importing Subs and Functions from external DLLs into PowerBASIC, the ALIAS clause must exactly match the capitalization of the exported name in the DLL.

 

See Also

What is a Dll?

Creating a Dynamic Link Library

Private and Exported Procedures

LibMain