dll design with building blocks

12
DLL Building Blocks Yes, we scan… http://en.wikipedia.org/wiki/Web_of_Things Max Kleiner Script: 362_maxon3D_EKON18.TXT

Upload: max-kleiner

Post on 15-Jul-2015

242 views

Category:

Engineering


6 download

TRANSCRIPT

Page 1: DLL Design with Building Blocks

DLL Building Blocks

Yes, we scan…http://en.wikipedia.org/wiki/Web_of_Things

Max Kleiner

Script: 362_maxon3D_EKON18.TXT

Page 2: DLL Design with Building Blocks

Agenda and Tutorial (DLL 32/64 bit)

2

• Call a DLL• Call a DLL+ (Interface, Callback, Events)• Build a DLL• Recompile a DLL• Android NDK and Lazarus

http://scholz2000.com/

A Short History of Time 1991 Application Program1995 Application1998 Applet2010 App2015 A (Scholz2000, Android, Arduino, ARM)

136_sysinformation_dll_EKON1.txt

http://www.softwareschule.ch/download/maxbox_starter28.pdf

Page 3: DLL Design with Building Blocks

DLL Primer•• Call: If you want your library to be called from programs compiled with

other compilers, it is important to specify the correct calling convention.•• Create: A library can be created just as a program, only it uses the

library keyword, and it has an exports section.•• Let's get back to the topic and create a DLL callback function: •• 1. declare a function type • 2. the function itself • 3. define the DLL reference • 4. then implement the function in the client • 5. and call the DLL:

3

136_sysinformation_dll_EKON1.txt 039_pas_dllcall_EKON2.txt070_pas_functionplotter_digital2.txt

Page 4: DLL Design with Building Blocks

Callback hack stackCallback hack stack

4

136_Callback_dll_EKON6.txtCallback example in client unit ----------------------------------------------- interface... 1. TCallBackFunction = function(sig: integer):boolean;

2. function callME(sig: integer):boolean;

implement... 3. procedure TestCallBack(myCBFunction: TCallBackFunction); register; external('watchcom.dll'); 4. function callMe(sig: integer): boolean; begin {whatever you need to do, case of...} showmessage('I was called with'+ inttostr(sig)); end;

5. procedure TForm1.Button1Click(sender: TObject); begin testCallBack(callMe); //subscribe function in DLL end;

Page 5: DLL Design with Building Blocks

Create a DLL

5

Callback in the DLL

In the DLL you would also declare a function type and a procedure (or function) itself, so use it like this:

type TCallBackFunction = function(sig: integer):boolean;

procedure TestCallBack(clientFunc: TCallBackFunction); stdcall; var sigAlive: boolean; begin {timer stuff... set the signal...} if(clientFunc(55)) then sigalive := true; end;

exports TestCallBack;

Page 6: DLL Design with Building Blocks

http://max.kleiner.com/dllplus.htm

6

Export an object-reference from a DLL is one approach to get real OO-access to a DLL. The DLL must create and return the object, so the client gets the methods without encapsulating.

unit income1; interface type IIncome = class public function GetIncome(const aNetto: Currency): Currency;

virtual; abstract; procedure SetRate(const aPercent, aYear: integer); virtual; abstract; function queryDLLInterface(var queryList: TStringList):

TStringList; virtual; abstract; end;

036_pas_includetest_EKON3.txt036_pas_dynlib_EKON4.txt036_pas_DLLTesting_EKON5.tx

Page 7: DLL Design with Building Blocks

Solution FPC ELF32 shared object

7Tutor: http://www.softwareschule.ch/download/maxbox_starter18_3.pdf

************* Simple Sequence Diagram**************

Client DLL ¦ TestCallBack(clientFunc) ¦ ¦---------------------------------------------->¦ ¦ clientFunc.callMe(sig ) ¦ ¦<----------------------------------------------¦ ¦ ¦ ¦ true (or something to return) ¦ ¦---------------------------------------------->¦ ¦ ¦

32-bit EXE loads 32-bit DLL, 64-bit EXE loads 64-bit DLL.

http://www.softwareschule.ch/examples/440_DLL_Tutor2.txthttp://wiki.freepascal.org/Android_Programming

Page 8: DLL Design with Building Blocks

Solution NDK

8

http://www.ntu.edu.sg/home/ehchua/programming/android/android_ndk.html

The NDK is a toolset that allows you to implement parts of your app using native-code languages such as C, Pascal, Object Pascal and C++. For certain types of apps, this can be helpful so you can reuse existing code libraries written in these languages, but most apps do not need the Android NDK.

Typical good candidates for the NDK are CPU-intensive workloads such as game engines, signal processing, physics simulation, and so on. When examining whether or not you should develop in native code, think about your requirements and see if the Android framework APIs provide the functionality that you need.

You have to use the Android NDK to recompile the library. The ARM architecture is completely different from the x86 architecture. The system calls are different on Linux and Windows.

Page 9: DLL Design with Building Blocks

NDK Need ThingsIf you have the src files for the DLL, try recompiling as an ELF32 shared object, then link

that instead into your Android code (-

below is a Windows solution):set NDK_HOME=C:\Android\android-ndk-r9c // customize this var for your own locationset LD_LIBRARY_PATH=%NDK_HOME%\platforms\android-18\arch-arm\usr\lib cd

REM -- TEMPORARILY COPY SOME LIBS COMPILER MAY NEEDcopy %NDK_HOME%\platforms\android-18\arch-arm\usr\lib\crtbegin*.o .copy %NDK_HOME%\platforms\android-18\arch-arm\usr\lib\crtend*.o .

REM -- GENERATE YOUR OBJ FILE%NDK_HOME%\toolchains\arm-linux-androideabi-4.8\prebuilt\windows-x86_64\bin\arm-linux-

androideabi-gcc.exe -g -I%NDK_HOME%\platforms\android-18\arch-arm\usr\include -c -fPIC YourLib.c -o YourLib.o

REM -- GENERATE SHARED OBJ FROM OBJ FILE%NDK_HOME%\toolchains\arm-linux-androideabi-4.8\prebuilt\windows-x86_64\bin\arm-linux-

androideabi-gcc.exe -g -L%NDK_HOME%\platforms

\android-18\arch-arm\usr\lib -shared -o YourLib_so.so YourLib_so.oREM -- finally, remove the libraries previously copied to src directory

del .\crtbegin*.odel .\crtend*.o

9

Page 10: DLL Design with Building Blocks

10http://developer.android.com/sdk/ndk/index.html

Here's the general outline of how you work with the NDK tools:

Place your native sources under <project>/jni/... Create <project>/jni/Android.mk to describe your native sources to the NDK

build system Optional: Create <project>/jni/Application.mk. Build your native code by running the 'ndk-build' script from your project's directory. It is located in the top-level NDK directory:

cd <project> <ndk>/ndk-build

I discovered that to use the build-ndk script, I don't need a real project. I created a folder project, with nothing in it except another folder jni, and put all my sources in that folder. I then created the Android.mk file and ran the script as described in the ndk docs.

Page 11: DLL Design with Building Blocks

Thanks! Links to Blocksthe source is the code

11

http://www.softwareschule.ch/maxbox.htmhttp://sourceforge.net/projects/maxboxhttp://en.wikipedia.org/wiki/Arduinohttp://www.softwareschule.ch/download/webofthings2013.pdf

Book Patterns konkrethttp://www.amazon.de/Patterns-konkret-Max-Kleiner/dp/3935042469

maXboxhttps://github.com/maxkleiner/maXbox3/releases

http://max.kleiner.com/dllplus.htmhttp://www.softwareschule.ch/download/dlldesign_ekon9.pdfhttp://developer.android.com/sdk/ndk/index.htmlhttp://www.ntu.edu.sg/home/ehchua/programming/android/android_ndk.html

Page 12: DLL Design with Building Blocks

Questions? Code a Worldhack the earth

12

Yes, we hack…https://github.com/maxkleiner/maXbox3/releases