Extending REALbasic


A shared library is compiled code that can be used by another application. There are two types of libraries: static libraries and shared libraries. A static library is compiled into your application and does not exist as a separate file. A shared library, on the other hand, is compiled code that resides outside your actual application.

You can access shared libraries in REALbasic by creating Declares. There are different shared library formats, depending on the platform you are using. In Linux, the shared libraries always end in .so. If you're a Windows users, you've no doubt seen files that end with .dll, which stands for Dynamic Link Library, which is just another word for shared library. The old shared library format for Macintosh was PEF (which is what REALbasic will create if the application needs to run on Macintosh "Classic" systems). The newer formats are .dylib and frameworks.

Declares

To use a function in a shared library, it must first be declared. You can declare a function in two ways. There's the plain old "hard" declare, and there's the new soft declare. The hard declare told the application that the function being declared will always be available in the library given, and that the library given will always be available. That's not always the case, which is why REALbasic now has a Soft Declare, which lets you declare functions that may or may not be available at runtime.

The format of a Declare follows. The first is an example of a function, and the second is an example of a subroutine:

[View full width]

[Soft] Declare Function FunctionName Lib LibraryName [Alias AliasName] ([parameters]) as ReturnType [Soft] Declare Sub SubroutineName Lib LibraryName [Alias AliasName]([parameters])


There are special types that you can use in constructing the Declare statement itself. In some instances, the MemoryBlock class provides a method for accessing the data of one of these types.

Table 11.6. MemoryBlock Types

Type

Description

Byte

Signed 8-bit Integer.

CFStringRef

A CFString reference (String).

CString

A null-terminated string.

OSType

A four char code, like a Mac Creator Code (4 byte integer).

PString

A "Pascal" string. 256 bytes. The first byte represents the length of the String. The remaining (up to 255 bytes) space is the String itself.

Ptr

4-byte pointer to a location in memory .

Short

2-byte signed Integer.

UByte

Unsigned 8-bit Integer.

UShort

Unsigned 2-byte Integer.

WindowPtr

4-byte Integer to a handle (which is like a pointer).


Next, I'll show you examples of Declares written for all three platforms. All of these examples are based on Declares created by developers in the REALbasic community that they have graciously shared with others. All the examples are ways to find out the process id of the current application.

Windows

A good source of Windows declares is Aaron Ballman's Windows Functionality Suite, which can be downloaded at the following address:

http://www.aaronballman.com/programming/REALbasic/Win32FunctionalitySuite.php


The Declare is written like this:

[View full width]

Function GetCurrentProcessId as Integer #if TargetWin32 Declare Function MyGetCurrentProcessId Lib "Kernel32" Alias "GetCurrentProcessId" () as Integer return MyGetCurrentProcessID #endif return 0 End Function


There are a few things to note about this declare. The first is that the library it accesses is called Kernel32. This Declare also uses an alias. The real name of the function in Kernel32 is GetCurrentProcessID, but this Declare references it as MyGetCurrentProcessID. This is so that the Declare can be wrapped in a function called GetCurrentProcessId and avoid any namespace collisions.

Macintosh

There are several ways of getting the process id on a Macintosh. There is a Carbon Declares module that is circulated in the community that accesses the Macintosh Carbon library to get it. In this instance, it's a two-step process. First, you get a reference to the current process serial number, which is a unique data structure that is not, technically, the process id.

[View full width]

Function GetCurrentProcess as MemoryBlock #if targetCarbon then Declare Sub GetMe lib "CarbonLib" Alias "GetCurrentProcess" (PSN As Ptr) #else Declare Sub GetMe Lib "InterfaceLib" Alias "GetCurrentProcess" (PSN As Ptr) Inline68K ("3F3C0037A88F") #endif // Returns the reference to the current process Dim proc As MemoryBlock, err as integer proc = NewMemoryBlock(8) GetMe proc Return proc End Function


After you have the process, you can then call another Carbon function that takes the current process in the parameter and returns the process id:

[View full width]

Dim i, pid As Integer #if TargetCarbon then Declare Function GetPID Lib "CarbonLib" Alias "GetProcessPID" (PSN As Ptr, byref pid As Integer) As Short i = GetPID(PSN, pid) Return pid #endif


If you are going to be accessing Mac OS X only, you can use a different approach, this time relying on the System framework:

   #if TargetMachO Declare Function getpid Lib "/System/Library/Frameworks/System.framework/System" _ () as Integer pid = getpid return pid #endif


Linux

There are quite a few Linux Declares available at the following address:

http://forge.novell.com/modules/xfmod/project/?rblinux


After a quick scan of the available functions, we find a function that allows you to get a process id on Linux. The function is coded as follows:

Function getpid as Integer Dim i As Integer #if TargetLinux then       Declare Function getpid Lib "libc.so.6" () As Integer     i = getpid       Return i #endif End Function


One thing you may notice is that the actual library includes a version number. That's because libc on Linux is an ld loader script and is used to locate the current version of LibCit's not LibC itself. Because this was a "hard" declare, it could be written only against a specific library with a specific name. However, using soft declare means that you can avoid using the version number.

Function getpid as Integer Dim i As Integer #if TargetLinux then       Soft Declare Function getpid Lib "LibC" () As Integer     i = getpid       Return i #endif End Function


Online Documentation

Of necessity, this has been only a brief introduction to the topic. Do not despair, however, because there is already an excellent resource available online, by Charles Yeoman:

http://www.declaresub.com/iDeclare/


Charles has an online book that delves in glorious detail into the ins and outs of using Declares, and because he's done such an excellent job, I will point you in his direction to learn from his expertise.

Plug-ins

Plug-ins are static libraries that get compiled into your REALbasic application; you can create your own plug-ins using REALbasic's Plug-in SDK. If you want to create cross-platform plug-ins, you will need to use Metrowerks Code Warrior because it can compile code for Windows and all of Macintosh's library formats.

You use plug-ins by putting them in REALbasic's Plug-ins directory. After they are installed, you can get access to the plug-in's documentation in the Help menu, under Plug-in Reference. The plug-ins themselves can consist of the following types:

Functions

These are global methods, such as Len, and so on.

Classes

A regular REALbasic class. It can be a subclass of any other REALbasic class.

Class Extensions

A class extension adds functionality to an existing class, much like you can do when using the Extends keyword.

Class Interfaces

An interface, just like you can create directly.

Controls

A control that can be dragged onto a Window.

Database Engines

REALbasic's databases are driven by plug-ins. The SDK can help you create your own.

Modules

A module is a collection of functions with a common namespace.


After the plug-in is installed, you use it as if it were a native REALbasic class, module, function, and so on, with code completion and all the other features available to you.

Creating plug-ins requires an intimate knowledge of C++ and the underlying operating systems. If you want to write cross-platform plug-ins, you need to know a lot about all three platforms. Describing how to write a plug-in is beyond the scope of this book, but that's okay because you can still benefit from them; several third-party plug-ins provide valuable functionality for REALbasic. Some are commercial applications and others are freeware.

Third-Party Plug-ins

There's a good chance you'll never need to write a plug-in yourself. Not only has REALbasic matured to the point that it provides just about everything you need for most applications, but there is also a market for commercial and free plug-ins that can be used in your applications. Here are a few highlights.

MonkeyBread Software (http://monkeybreadsoftware.de)

MonkeyBread Software offers the mother of all plug-insa plug-in that, at last count, added more than 7,000 functions to REALbasic. Some are Macintosh-specific and others are Windows-specific, but the vast majority work on either platform. One of the more interesting features of this plug-in is that it provides classes that allow you to execute Java from within REALbasic.

Einhugur (http://einhugur.com/)

Einhugur software provides a series of plug-ins. One of the more interesting is e-Cryptit, which provides a comprehensive set of tools for encrypting and decrypting data.

Einhugur also offers a series of Grid controls, which are more flexible versions of REALbasic's ListBox control. Figure 11.7 is a screenshot of Einhugur's StyledGridControl:

Figure 11.7. Einhugur's StyleGrid control.


Einhugur's TreeView control provides a control that works similar to a hierarchical ListBox, but that retains the look and feel of the native application. REALbasic's ListBox does not use the underlying native controls on each platform, but is part of REALbasic. Because of this, it doesn't look exactly like a native control. TreeView solves this problem, which you can see in Figure 11.7.

Valentina (http://paradigmsoft/)

Valentina is a database application offered by Paradigmasoft. Until REALbasic began to use SQLite for the default database, Valentina was a clearly superior database. In addition to working with REALbasic, Valentina also has libraries that work with Macromedia Directory, Revolution, PHP, Java, and C++.

Component X Graphics (http:/ / www.componentx.com/ CXGraphics/ )

Component X is a free plug-in that extends REALbasic's Picture class. With it you can stretch, flip, rotate, and trim pictures. You can also adjust a picture's contrast, gamma, HSV, and RGB values. You can even rotate text along an arc.

FTPSuite for REALbasic (http:/ / www.pyramiddesign.us/ ftpsuite/ index.html)

This plug-in offers a set of classes that lets you easily integrate FTP into your application. FTP happens to be one of the Internet protocols not directly supported by REALbasic (unlike HTTP, SMTP, and POP).




REALbasic Cross-Platform Application Development
REALbasic Cross-Platform Application Development
ISBN: 0672328135
EAN: 2147483647
Year: 2004
Pages: 149

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net