Successfully reported this slideshow.

DLL Design with Building Blocks

0

Share

Loading in …3
×
1 of 12
1 of 12

DLL Design with Building Blocks

0

Share

Download to read offline

Mistakes to avoid when designing DLLs and thoughts about other platforms to deliever with your DLL.
Some compilers provide mechanisms to automatically export all functions and variables in a library that have external linkage. Avoid using any such mechanisms. Export exactly the interface that you need to export, and no more.

Mistakes to avoid when designing DLLs and thoughts about other platforms to deliever with your DLL.
Some compilers provide mechanisms to automatically export all functions and variables in a library that have external linkage. Avoid using any such mechanisms. Export exactly the interface that you need to export, and no more.

More Related Content

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

DLL Design with Building Blocks

  1. 1. DLL Building Blocks Yes, we scan… http://en.wikipedia.org/wiki/Web_of_Things Max Kleiner Script: 362_maxon3D_EKON18.TXT
  2. 2. Agenda and Tutorial (DLL 32/64 bit) 2 http://www.softwareschule.ch/download/maxbox_starter28.pdf • Call a DLL • Call a DLL+ (Interface, Callback, Events) • Build a DLL • Recompile a DLL • Android NDK and Lazarus http://scholz2000.com/ 136_sysinformation_dll_EKON1.txt A Short History of Time 1991 Application Program 1995 Application 1998 Applet 2010 App 2015 A (Scholz2000, Android, Arduino, ARM)
  3. 3. 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.txt 070_pas_functionplotter_digital2.txt
  4. 4. CCaallllbbaacckk hhaacckk ssttaacckk • 4 Callback example in client unit 136_Callback_dll_EKON6.txt ----------------------------------------------- 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;
  5. 5. 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;
  6. 6. 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.txt 036_pas_dynlib_EKON4.txt 036_pas_DLLTesting_EKON5.tx
  7. 7. Solution FPC ELF32 shared object 7 32-bit EXE loads 32-bit DLL, 64-bit EXE loads 64-bit DLL. ************* Simple Sequence Diagram************** Client DLL ¦ TestCallBack(clientFunc) ¦ ¦---------------------------------------------->¦ ¦ clientFunc.callMe(sig ) ¦ ¦<----------------------------------------------¦ ¦ ¦ ¦ true (or something to return) ¦ ¦---------------------------------------------->¦ ¦ ¦ http://wiki.freepascal.org/Android_Programming http://www.softwareschule.ch/examples/440_DLL_Tutor2.txt Tutor: http://www.softwareschule.ch/download/maxbox_starter18_3.pdf
  8. 8. Solution NDK 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. 8 http://www.ntu.edu.sg/home/ehchua/programming/android/android_ndk.html
  9. 9. NDK Need Things If 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:Androidandroid-ndk-r9c // customize this var for your own location set LD_LIBRARY_PATH=%NDK_HOME%platformsandroid-18arch-armusrlib cd REM -- TEMPORARILY COPY SOME LIBS COMPILER MAY NEED copy %NDK_HOME%platformsandroid-18arch-armusrlibcrtbegin*.o . copy %NDK_HOME%platformsandroid-18arch-armusrlibcrtend*.o . REM -- GENERATE YOUR OBJ FILE %NDK_HOME%toolchainsarm-linux-androideabi-4.8prebuiltwindows-x86_64binarm-linux-androideabi- gcc.exe -g -I%NDK_HOME%platforms android-18arch-armusrinclude -c -fPIC YourLib.c -o YourLib.o REM -- GENERATE SHARED OBJ FROM OBJ FILE %NDK_HOME%toolchainsarm-linux-androideabi-4.8prebuiltwindows-x86_64binarm-linux-androideabi- gcc.exe -g -L%NDK_HOME%platforms android-18arch-armusrlib -shared -o YourLib_so.so YourLib_so.o REM -- finally, remove the libraries previously copied to src directory del .crtbegin*.o del .crtend*.o 9
  10. 10. 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. 10 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 http://developer.android.com/sdk/ndk/index.html
  11. 11. Thanks! Links to Blocks the source is the code http://www.softwareschule.ch/maxbox.htm http://sourceforge.net/projects/maxbox http://en.wikipedia.org/wiki/Arduino http://www.softwareschule.ch/download/webofthings2013.pdf Book Patterns konkret http://www.amazon.de/Patterns-konkret-Max-Kleiner/dp/3935042469 maXbox https://github.com/maxkleiner/maXbox3/releases 11 http://max.kleiner.com/dllplus.htm http://www.softwareschule.ch/download/dlldesign_ekon9.pdf http://developer.android.com/sdk/ndk/index.html http://www.ntu.edu.sg/home/ehchua/programming/android/android_ndk.html
  12. 12. Questions? Code a World hack the earth 12 Yes, we hack… https://github.com/maxkleiner/maXbox3/releases

×