External functions

Differences in passing a structure by reference PowerBuilder allows you to declare an external function that has a parameter of type Structure passed by reference. For example:

Subroutine CopyMemory(ref structure s, int size) library "abc.dll"

The s parameter can accept any datatype that is a pointer to something.

A PowerBuilder external function is mapped to the .NET platform Invoke functionality. This functionality requires that the structure passed into the external function be exactly of the type declared. Therefore, when compiling the following PowerScript code, the PowerBuilder .NET compiler issues an error, because the parameter, li, references a LogInfo structure, which is different from the function’s declared structure class.

LogInfo li
CopyMemory(ref li, 20)   // error!

To solve this problem, you can declare an additional external function as follows:

Subroutine CopyMemory(ref LogInfo li, int size) library "abc.dll"

Structures as parameters in .NET Applications External functions that have structures for parameters must be passed by reference rather than value if you call them in a .NET Windows Forms or .NET Web Forms application when the parameter is a const pointer. For example, a PowerScript call to the SystemTimeToFileTime function in kernel32.dll could use the following declaration, with the first parameter being passed by value and the second parameter by reference:

Function boolean SystemTimeToFileTime(os_systemtime lpSystemTime, ref os_filedatetime lpFileTime) library "KERNEL32.DLL"

For .NET Windows Forms or Web Forms applications, you must modify the declaration to pass both parameters by reference:

Function boolean SystemTimeToFileTime(ref os_systemtime lpSystemTime, ref os_filedatetime lpFileTime) library "KERNEL32.DLL"

The SystemTimeToFileTime function is declared as a local external function and used in pfc_n_cst_filesrvunicode, pfc_n_cst_filesrvwin32, and other operating-system-specific classes in the pfcapsrv.pbl in the PFC library. If you use this library in a .NET Windows Forms or Web Forms application, you must change the declaration as described above.

Allocate space before passing a string by reference Before passing a string to an external function by reference in PowerBuilder, you should allocate memory for the string by calling the Space system function. In subsequent calls to the function, if you pass the same string to the function, PowerBuilder continues to work well even if the string becomes empty, because memory allocated for the string is not yet freed by the PowerBuilder VM.

This is not the case in the .NET environment. If the string passed to an external function by reference is empty, and if the external function writes something to the string, an exception is thrown. Therefore, you must make sure to allocate enough space for a string before passing it to an external function by reference.