SlideShare a Scribd company logo
Leveraging the Notes
C API in LotusScript:
“A How-to Guide”


Bill Buchan
HADSL
          © 2007 Wellesley Information Services. All rights reserved.
What We’ll Cover …
•   Introduction
•   Architectures
•   Platform differences
•   Calling simple Notes C API
•   Notes C API handles
•   Custom classes help hide platform differences
•   Complex Notes C API
•   Pros and cons
•   Wrap-up



                                                    2
Target and Purpose
•   Who is the target audience?
       Advanced Lotus Notes Developers
       Developers who have a little experience in C API
•   What is this about?
       This talk demonstrates advanced LotusScript, showing the
        Notes C API interface
•   Why Notes C API?
       900+ supported methods
       Many complex tasks can be called via API




                                                            3
Who Am I?
•   Bill Buchan
•   Dual Principal Certified Lotus Professional (PCLP) in v3,
    v4, v5, v6, v7
•   10+ years senior development consultancy for Enterprise
    customers
       Learn from my pain!
•   5+ years code auditing
•   CEO of HADSL
       Developing best-practice tools




                                                     4
Notes C API Programming Toolkit
•   Where can you find information on C API?
       The Lotus Notes C Programming toolkit
•   The toolkit:
       Contains two documentation databases
       Documents each function and structure
       Includes a large number of sample C programs
       Provides compile-time libraries for all platforms
•   Generally speaking, backward compatibility is good
       For instance, if you have v6 and v7 servers, creating the
        program using v6 of the API toolkit means that it will run on v6
        and v7 servers



                                                                5
What We’ll Cover …
•   Introduction
•   Architectures
•   Platform differences
•   Calling simple Notes C API
•   Notes C API handles
•   Custom classes help hide platform differences
•   Complex Notes C API
•   Pros and cons
•   Wrap-up



                                                    6
Notes/Domino Architectures
•   Domino is a multi-platform server product
•   Notes now supports four clients:
       Windows, Mac (Power PC), Mac (Intel), Linux
•   LotusScript supported from v4.x of Notes, on all server
    and client platforms
       Rich set of APIs for general business logic
       Robust, supported environment for general business
        applications
       Amazing level of multi-platform support
•   What if you want to go further?
       Exploit a piece of Notes C API interface?
       Call a platform-specific DLL?

                                                             7
Why Worry About Platform Independence?
•   Currently, something like:
       90% of all Domino servers run on Windows
       95% of all Notes clients run on Windows
•   Why not write platform code for that and that alone?
       A short-sighted decision
          Your environment may start supporting more platforms
       Windows 64-bit coming over the horizon
          Treat it as a separate platform
              As was 16-bit Windows

•   Corporate push to centralize and consolidate
       Especially on medium-high level platforms
          Linux, Solaris, AIX, HP/UX


                                                           8
Be Pragmatic — The Skills Triangle

          Few Code Examples
                                     Harder

                 C API




           LotusScript, Java

                                              Easier
          Many Code Examples
                                                 9
Be Pragmatic
•   Call the Notes C API from LotusScript if:
       You cannot find a supported architectural solution
       You have the skills to support Notes C API coding
       You might possibly require multi-platform code
       You accept that this code will be harder to write, harder to
        maintain, and harder to support




                                                                10
Architectures — Conclusion
•   If you have to step outside the comfort zone, you have
    two choices:
       LotusScript eXtension Toolkit (LSX)
          This builds a library — on Windows, a DLL — which allows
           you to extend LotusScript with custom C code
          Fairly straightforward
          Not part of this presentation
       LotusScript calling the Notes C API interface directly
          Not straightforward
          One code base can cover all platforms




                                                            11
What We’ll Cover …
•   Introduction
•   Architectures
•   Platform differences
•   Calling simple Notes C API
•   Notes C API handles
•   Custom classes help hide platform differences
•   Complex Notes C API
•   Pros and cons
•   Wrap-up



                                                    12
Platform Differences: Definitions
•   In LotusScript, platform differences are taken care of
       Not so in direct LotusScript to C API calls
•   Each platform represents data in a subtly different
    manner
       For instance, the HANDLE datatype has different lengths on
        different platforms:
          32 bits long in Windows and OS/400
          16 bits long on AIX, Macintosh, etc.
       Memory alignment widths on iSeries are different from other
        platforms




                                                             13
Platform-Specific Implementations
•   In order to call the Notes library, you will have to link to
    different library files on each platform:
       Windows/32:       nnotes.dll
       Solaris, Linux:   libnotes.so
       AIX:              lnotes_r.a
       HPUX:             libnotes.sl
       MacOs, OS/X:      NotesLib
       OS/400:           libnotes.srvpgm
       OS/390            libnotes
       OS/2:             lnotes.dll
       Windows Alpha:    anotes.dll




                                                          14
Platform Differences: Endians
•   Little endians
       Some platforms represent multi-byte numbers with the lowest
        value bytes in the lower segments of memory
•   Big endians
       Macintosh (on PowerPC!) represents the larger value bytes in
        the lower segments of memory




                                                             15
Coding for Platform Differences
•   Our challenge is to construct a code sequence that:
       Knows which platform it’s running on
       Makes calls to the platform-specific library file
       Understands platform differences in terms of alignment and
        data item size
       And most importantly — fails “safe” when presented with a new
        platform that it cannot deal with
•   This is not a trivial exercise
       My estimate is that this exercise will take at least 5-10 times
        more effort — development and testing — than a normal
        LotusScript business function




                                                                 16
Decoding Signatures




Bold indicates a different memory structure for the same type across platforms. In other words, “Watch out!”
                                                                                                    17
What We’ll Cover …
•   Introduction
•   Architectures
•   Platform differences
•   Calling simple Notes C API
•   Notes C API handles
•   Custom classes help hide platform differences
•   Complex Notes C API
•   Pros and cons
•   Wrap-up



                                                    18
Calling a Simple C API Function
•   Let’s get our feet wet with some simple API
       No complex sequence of calls handling a shared memory
        resource
           A single call with simple datatypes
       A call that will not result in client memory corruption should we
        get it wrong
           Hopefully

•   Let’s call it from Windows and ignore other platforms
       A function that tells you the network latency, in milliseconds,
        between the current Notes instance and a target server
          NSFGetServerLatency




                                                                19
NSFGetServerLatency() — API Reference
•   We shall call NSFGetServerLatency()
       This returns the network latency time described in milliseconds
        for a call to a remote server
•   Useful to decide which server is closest in terms of
    network response
•   Its signature from the C API reference is:

         STATUS LNPUBLIC NSFGetServerLatency(
            char far *ServerName,
            DWORD Timeout,
            DWORD far *retClientToServerMS,
            DWORD far *retServerToClientMS,
            WORD far *ServerVersion);



                                                              20
NSFGetServerLatency() — API Reference (cont.)
•   From Lotus C API Notes/Domino 7.0 Reference:
    Input Parameters
    ServerName — Null-terminated string containing the name of the
    server to query.
    Timeout — Number of milliseconds to wait for a reply from the server.
    A timeout of 0 indicates that the default timeout value is to be used.
    Output Parameters
    (routine) — Return status from this call:
    NOERROR — Success
    retClientToServerMS — Optional — If not NULL, the number of
    milliseconds required to send the request to the server is stored at this
    address.
    retServerToClientMS — Optional — If not NULL, the number of
    milliseconds required for the reply to return from the server is stored at
    this address.
    ServerVersion — Optional — If not NULL, the server version (the
    Domino build number for the server) is stored at this address.
                                                                   21
Simple C API Calling
' This is a constant for our windows-based
' Library file:
Const LIB_W32 = "nnotes.dll"

' Declare our function for windows
Declare Function W32_NSFGetServerLatency _
   Lib LIB_W32 Alias {NSFGetServerLatency} (_
   Byval ServerName As Lmbcs String, _
   Byval Timeout As Long, _
   retClientToServerMS As Long, _
   retServerToClientMS As Long, _
   ServerVersion As Integer) As Integer




                                                22
Simple C API Calling: Execution
' A function to get network latency time...
Public Function getServerLatency _
(strServer As String) As Long
   Dim nnServer As New NotesName(strServer)
   Dim ToServer As Long, fromServer As Long
   Dim ver As Integer
   Dim timeout As Long

   timeout = 1000        ' 1000ms == 1 second

   Call W32_NSFGetServerLatency(nnServer.Canonical,_
     timeout, toServer, fromServer, ver)

   ' Return both directional latencies added together
   getServerLatency = fromServer + ToServer
End Function

                                                23
Simple C API Calling: Execution (cont.)
Sub initialise
   Print “Calling function”
   Print “Latency is: “ + _
      cstr(getServerLatency(“domino-90.hadsl.com/
        HADSL/US”))
   Print “Finished calling”
end sub




                                               24
Database Handle Class — Introduction




                Demo of
            GetServerLatency




                                       25
Simple C API Calling: The Results
•   Running the agent “Example1” in the database produces
    the following runtime output:


•   Now, this is not production code! It requires:
       Error handling
       Multi-platform support




                                                     26
What Can’t You Call?
•   Callback routines
       Some Notes C API functions require you to specify mandatory
        callback routines — other pieces of C API that will get called by
        the target C API routine
           LotusScript does not (and, IMHO, never will) support this
           This rules out:
               Extension Manager routines
               Menu Addin routines
       In terms of callback routines that allow you to specify optional
        callbacks (progress status indicators, etc.), you can pass in
        NULL (or ZERO), and the callback function will be ignored




                                                                27
What We’ll Cover …
•   Introduction
•   Architectures
•   Platform differences
•   Calling simple Notes C API
•   Notes C API handles
•   Custom classes help hide platform differences
•   Complex Notes C API
•   Pros and cons
•   Wrap-up



                                                    28
API Handles
•   A handle is a numeric reference to a structure in memory
    that the C API interface has created
       For example, a handle to a Notes database
•   You do not deal with the memory structure itself; you deal
    with the handle to the memory structure
       Example: NSFDbOpen() creates a new memory structure and
        gives you back the handle to that
       You perform some work using NSFDInfoGet()
       You close the database using NSFDbClose()




                                                         29
Some Rules on Handles
•   There are different types of handles:
       Document handle, database handle, etc.
       Do not mix these handles up!
          Doing so will crash the session/client/server

•   Always properly close your handles:
       You have to properly trap all code branches
       You have to keep track of the handle until you specifically
        de-allocate it
          Not doing so will crash the session/client/server
          It may not crash immediately
               It may crash when the session, client, or server closes

•   You cannot change the value of the handle:
          Doing   so will crash the session/client/server

                                                                30
Coding Around Handles
•   Track handles with classes
       Perform the “open” of the handle on the class constructor
       Perform the “close” of the handle on the class destructor
           This means that no matter what happens, the handle
            operation will be properly closed — even if this is not
            specifically coded in your LotusScript
       All the code specific to this handle operation is embedded in
        the class
           And can include multi-platform code
       All the consumer sees is an operation with this specific class




                                                               31
Handle Summary
•   If you get a handle, you have to de-allocate it
•   Only use handle data you know is valid
       If a handle == NULLHANDLE (or ZERO), then its not a valid
        handle
•   Bonus
       NotesDocument.handle returns the current handle
       You can easily use LotusScript to:
          Find a document
          Use direct calls to update information that the object model
           does not support (at the moment)
          e.g., writing Notes replica items




                                                               32
What We’ll Cover …
•   Introduction
•   Architectures
•   Platform differences
•   Calling simple Notes C API
•   Notes C API handles
•   Custom classes help hide platform differences
•   Complex Notes C API
•   Pros and cons
•   Wrap-up



                                                    33
Custom Classes Can Help Us Considerably
•   We can construct classes which “hide” the platform
    differences
•   Each platform will have its own set of classes
•   Each code sequence will be small, easily maintainable
•   We can reuse common code
•   We can use the custom class constructor and destructor
    to ensure that resources are properly returned




                                                   34
Custom Classes: Architecture

Hidden Code      Base Class

              Object
              Handler

    Platform
      Platform
    Specific
        Platform
      Specific
     Object
        Specific
       Object
    Handlers
         Object
     Handlers
        Handlers
                    Factory
                                Dim F As New APIFactoryClass()
                                Dim dbH As Variant
                   User Code    Set dbH = F.getDbHandler()
                                …

                                                      35
Custom Classes
•   Our base class carries infrastructure code
       Logging, string handling, etc.
•   Our object base class:
       Defines the functions that we will use
       Defines the business logic
•   Our platform-specific classes:
       Override platform-specific functions with that platform’s
        implementation
•   Our factory class:
       Helps the user of the code find the correct platform-specific
        class to use



                                                               36
Is This Over-Engineered?
•   At first sight, this looks complex
•   However, it does allow us to:
       Keep platform-specific code in well-defined classes
       Leverage class constructor/destructor calls to ensure handles
        are properly managed
       Perform a simple “Copy+Paste” of the object class to port to a
        new platform
       Keep code sequences short and clear
       Hide the implementation of the code from the consumer of
        the code




                                                              37
Database Handle Class — Introduction




                Demo of
               IDRefresh




                                       38
What We’ll Cover …
•   Introduction
•   Architectures
•   Platform differences
•   Calling simple Notes C API
•   Notes C API handles
•   Custom classes help hide platform differences
•   Complex Notes C API
•   Pros and cons
•   Wrap-up



                                                    39
Defining Complex C API
•   Complex C API is where:
       More than one function call is required to perform a task
          And/or
       A HANDLE of some description is needed to reference a
        data object




                                                               40
Complex C API
•   Where more than one function call is required to perform
    a function it is recommended that:
       You use defensive coding techniques to understand and make
        “safe” every single coding branch possible
       Any handle information used during this sequence is kept safe
        and its de-allocation specifically catered to
       You use classes to ensure proper construction and destruction
        of handle information




                                                            41
Our Complex Example
•   Is passed through a server and database path
•   Opens and gets the DBHANDLE for that database
•   Extracts the replication flags into a memory structure
•   Checks to see if the “Disable Replication” flag is on
       If it is, switches that flag off
       Saves the replication flags
•   Finally, closes the DBHANDLE




                                                      42
We Use Custom Classes To …
•   Ensure that the DBHANDLE is always properly returned
•   Insulate the consumer of the code from the platform
    differences
•   Contain all the platform-specific code in separate classes
•   Keep it simple!




                                                      43
Database Handle Class — Introduction




                Demo of
            DbHandleManager




                                       44
What We’ll Cover …
•   Introduction
•   Architectures
•   Platform differences
•   Calling simple Notes C API
•   Notes C API handles
•   Custom classes help hide platform differences
•   Complex Notes C API
•   Pros and cons
•   Wrap-up



                                                    45
Know Your Battleground
•   LotusScript coding is:
       Fairly straightforward
       Supported
       Platform-independent
•   Stepping outside of this comfort zone is going to be hard
       Take far more time in development and testing
       Push development resources to the limit
       Only do this if you have an absolute business need




                                                             46
LSX vs. Notes C API
•   You need to establish, based on your requirements,
    which is the best fit:


    Feature             LSX                        LotusScript calling Notes C API
    Deployment          Requires code deployment No deployment required
    Multi-platform      Recompile LSX for each     Same code in library
                        platform

    C API integration   Very good integration —    Basic integration
                        supports callbacks, etc.
    Code difficulty     Medium                     Hard

    Stability           Extremely stable           Stable


                                                                            47
What We’ll Cover …
•   Introduction
•   Architectures
•   Platform differences
•   Calling simple Notes C API
•   Notes C API handles
•   Custom classes help hide platform differences
•   Complex Notes C API
•   Pros and cons
•   Wrap-up



                                                    48
Resources
•   Normunds Kalnberzin, LotusScript to Lotus C API
    Programming Guide (November, 2003).
       The ultimate reference
       www.ls2capi.com
•   Tools for Lotus Notes and Domino
       Lotus Notes API tips
       www.nsftools.com/tips/APITips.htm
•   Working application with several Notes C API calls
       Included on the conference CD




                                                      49
7 Key Points to Take Home
•   Calling Notes C API is difficult
       Only use it if absolutely required
•   It has to be tested on all platforms
•   It can easily take out a client or server process
•   Use defensive coding!
•   Use classes to hide platform differences
•   It’s the next stage beyond the well-regulated
    LotusScript sandbox
•   It gives you all possible Notes capability




                                                        50
Your Turn!




             How to contact me:
                Bill Buchan
              Bill@hadsl.com
                                  51

More Related Content

What's hot (18)

PDF
Future of .NET - .NET on Non Windows Platforms
Aniruddha Chakrabarti
 
PPTX
Programming With WinRT And Windows8
Rainer Stropek
 
PDF
Intel the-latest-on-ofi
Tracy Johnson
 
PDF
Python Introduction
Learnbay Datascience
 
PPTX
Overview new programming languages
umoren
 
PDF
01 overview
Suresh Kumar
 
PPT
.Net framework
Viv EK
 
PDF
Turbo C Compiler Reports
Sunil Kumar R
 
PDF
Fundamental of Information Technology - UNIT 7
Shipra Swati
 
PDF
Os Rego
oscon2007
 
PDF
.net Core Blimey - Smart Devs UG
citizenmatt
 
PPT
Compilers and interpreters
RAJU KATHI
 
PDF
How to Port Your .NET Applications to Linux Using Mono Tools for Visual Studio
Novell
 
PDF
ASP.NET on zLinux: A New Workload
Novell
 
DOCX
Resume -9 Yrs -Looking for New Opportunity !!
Raju Tiwari
 
PDF
Os Lattner
oscon2007
 
DOCX
Raju Tiwari-Resume-8+
Raju Tiwari
 
PPTX
Cf&oa
Mohd Nazim
 
Future of .NET - .NET on Non Windows Platforms
Aniruddha Chakrabarti
 
Programming With WinRT And Windows8
Rainer Stropek
 
Intel the-latest-on-ofi
Tracy Johnson
 
Python Introduction
Learnbay Datascience
 
Overview new programming languages
umoren
 
01 overview
Suresh Kumar
 
.Net framework
Viv EK
 
Turbo C Compiler Reports
Sunil Kumar R
 
Fundamental of Information Technology - UNIT 7
Shipra Swati
 
Os Rego
oscon2007
 
.net Core Blimey - Smart Devs UG
citizenmatt
 
Compilers and interpreters
RAJU KATHI
 
How to Port Your .NET Applications to Linux Using Mono Tools for Visual Studio
Novell
 
ASP.NET on zLinux: A New Workload
Novell
 
Resume -9 Yrs -Looking for New Opportunity !!
Raju Tiwari
 
Os Lattner
oscon2007
 
Raju Tiwari-Resume-8+
Raju Tiwari
 
Cf&oa
Mohd Nazim
 

Viewers also liked (7)

PDF
The View - Deploying domino applications to BlackBerry MDS Studio
Bill Buchan
 
PDF
Softsphere - Development for administrators
Bill Buchan
 
PDF
Connections Lotusphere Worst Practices 2013
Bill Buchan
 
PDF
The View - Leveraging Lotuscript for Database Connectivity
Bill Buchan
 
PPT
Michele Payn-Knoper - Authentically Agriculture
John Blue
 
PDF
Dev buchan leveraging the notes c api
Bill Buchan
 
PPS
Analyze and export Lotus Notes data to Excel and Word
John de Giorgio
 
The View - Deploying domino applications to BlackBerry MDS Studio
Bill Buchan
 
Softsphere - Development for administrators
Bill Buchan
 
Connections Lotusphere Worst Practices 2013
Bill Buchan
 
The View - Leveraging Lotuscript for Database Connectivity
Bill Buchan
 
Michele Payn-Knoper - Authentically Agriculture
John Blue
 
Dev buchan leveraging the notes c api
Bill Buchan
 
Analyze and export Lotus Notes data to Excel and Word
John de Giorgio
 
Ad

Similar to Dev buchan leveraging the notes c api (20)

PDF
Vienna buchan calling the notes c-api from lotus_script
Bill Buchan
 
PDF
Entwicker camp2007 calling-the-c-api-from-lotusscript
Bill Buchan
 
PDF
Advanced Programming With Notes/DominoCOM Classes
dominion
 
ODP
The xsp starter kit
Stephan H. Wissel
 
PDF
MWLUG 2011: The Never Ending Integration Story
John Head
 
PPTX
BP207 - Meet the Java Application Server You Already Own – IBM Domino
Serdar Basegmez
 
KEY
Using Aspects for Language Portability (SCAM 2010)
lennartkats
 
PDF
Using Eclipse and Lua for the Internet of Things - EclipseDay Googleplex 2012
Benjamin Cabé
 
PDF
Cocoa for Scientists
Francis Rowland
 
PDF
Inside notes
dominion
 
PDF
POCO C++ Libraries Intro and Overview
Günter Obiltschnig
 
PDF
Open source building blocks for the Internet of Things - Jfokus 2013
Benjamin Cabé
 
PPT
Building a Real-World Application with Adobe Flex 2
dcoletta
 
PPTX
haXe - One codebase to rule'em all
Tom Crombez
 
PPT
Management information system software
Online
 
PPS
Chapter3
suks_87
 
PDF
Pythonic APIs - Anthony Baxter
knappt
 
PDF
Delphi Prism for iPhone/iPad and Linux with Mono and Monotouch
Andreano Lanusse
 
PPT
Casual Engines 2009
David Fox
 
PDF
Creating Large Scale Software Platforms with OSGi and an Extension Point Mode...
Nuxeo
 
Vienna buchan calling the notes c-api from lotus_script
Bill Buchan
 
Entwicker camp2007 calling-the-c-api-from-lotusscript
Bill Buchan
 
Advanced Programming With Notes/DominoCOM Classes
dominion
 
The xsp starter kit
Stephan H. Wissel
 
MWLUG 2011: The Never Ending Integration Story
John Head
 
BP207 - Meet the Java Application Server You Already Own – IBM Domino
Serdar Basegmez
 
Using Aspects for Language Portability (SCAM 2010)
lennartkats
 
Using Eclipse and Lua for the Internet of Things - EclipseDay Googleplex 2012
Benjamin Cabé
 
Cocoa for Scientists
Francis Rowland
 
Inside notes
dominion
 
POCO C++ Libraries Intro and Overview
Günter Obiltschnig
 
Open source building blocks for the Internet of Things - Jfokus 2013
Benjamin Cabé
 
Building a Real-World Application with Adobe Flex 2
dcoletta
 
haXe - One codebase to rule'em all
Tom Crombez
 
Management information system software
Online
 
Chapter3
suks_87
 
Pythonic APIs - Anthony Baxter
knappt
 
Delphi Prism for iPhone/iPad and Linux with Mono and Monotouch
Andreano Lanusse
 
Casual Engines 2009
David Fox
 
Creating Large Scale Software Platforms with OSGi and an Extension Point Mode...
Nuxeo
 
Ad

More from Bill Buchan (20)

PDF
Dummies guide to WISPS
Bill Buchan
 
PPTX
WISP for Dummies
Bill Buchan
 
PDF
WISP Worst Practices
Bill Buchan
 
PDF
Marykirk raft race presentation night 2014
Bill Buchan
 
PDF
Dev buchan best practices
Bill Buchan
 
PDF
Dev buchan leveraging
Bill Buchan
 
PDF
Dev buchan everything you need to know about agent design
Bill Buchan
 
PDF
Dev buchan 30 proven tips
Bill Buchan
 
PDF
Entwicker camp2007 blackberry-workshop
Bill Buchan
 
PDF
Bp301
Bill Buchan
 
PDF
Ad507
Bill Buchan
 
PDF
Ad505 dev blast
Bill Buchan
 
PDF
Admin2012 buchan web_services-v101
Bill Buchan
 
PDF
Reporting on your domino environment v1
Bill Buchan
 
PDF
12 Step Guide to Lotuscript
Bill Buchan
 
PDF
Everything you ever wanted to know about lotus script
Bill Buchan
 
PDF
Admin camp 2011-domino-sso-with-ad
Bill Buchan
 
PDF
Softsphere 08 web services bootcamp
Bill Buchan
 
PDF
Lotusphere 2009 The 11 Commandments
Bill Buchan
 
PDF
Lotusphere 2008 Worst practices
Bill Buchan
 
Dummies guide to WISPS
Bill Buchan
 
WISP for Dummies
Bill Buchan
 
WISP Worst Practices
Bill Buchan
 
Marykirk raft race presentation night 2014
Bill Buchan
 
Dev buchan best practices
Bill Buchan
 
Dev buchan leveraging
Bill Buchan
 
Dev buchan everything you need to know about agent design
Bill Buchan
 
Dev buchan 30 proven tips
Bill Buchan
 
Entwicker camp2007 blackberry-workshop
Bill Buchan
 
Ad505 dev blast
Bill Buchan
 
Admin2012 buchan web_services-v101
Bill Buchan
 
Reporting on your domino environment v1
Bill Buchan
 
12 Step Guide to Lotuscript
Bill Buchan
 
Everything you ever wanted to know about lotus script
Bill Buchan
 
Admin camp 2011-domino-sso-with-ad
Bill Buchan
 
Softsphere 08 web services bootcamp
Bill Buchan
 
Lotusphere 2009 The 11 Commandments
Bill Buchan
 
Lotusphere 2008 Worst practices
Bill Buchan
 

Dev buchan leveraging the notes c api

  • 1. Leveraging the Notes C API in LotusScript: “A How-to Guide” Bill Buchan HADSL © 2007 Wellesley Information Services. All rights reserved.
  • 2. What We’ll Cover … • Introduction • Architectures • Platform differences • Calling simple Notes C API • Notes C API handles • Custom classes help hide platform differences • Complex Notes C API • Pros and cons • Wrap-up 2
  • 3. Target and Purpose • Who is the target audience?  Advanced Lotus Notes Developers  Developers who have a little experience in C API • What is this about?  This talk demonstrates advanced LotusScript, showing the Notes C API interface • Why Notes C API?  900+ supported methods  Many complex tasks can be called via API 3
  • 4. Who Am I? • Bill Buchan • Dual Principal Certified Lotus Professional (PCLP) in v3, v4, v5, v6, v7 • 10+ years senior development consultancy for Enterprise customers  Learn from my pain! • 5+ years code auditing • CEO of HADSL  Developing best-practice tools 4
  • 5. Notes C API Programming Toolkit • Where can you find information on C API?  The Lotus Notes C Programming toolkit • The toolkit:  Contains two documentation databases  Documents each function and structure  Includes a large number of sample C programs  Provides compile-time libraries for all platforms • Generally speaking, backward compatibility is good  For instance, if you have v6 and v7 servers, creating the program using v6 of the API toolkit means that it will run on v6 and v7 servers 5
  • 6. What We’ll Cover … • Introduction • Architectures • Platform differences • Calling simple Notes C API • Notes C API handles • Custom classes help hide platform differences • Complex Notes C API • Pros and cons • Wrap-up 6
  • 7. Notes/Domino Architectures • Domino is a multi-platform server product • Notes now supports four clients:  Windows, Mac (Power PC), Mac (Intel), Linux • LotusScript supported from v4.x of Notes, on all server and client platforms  Rich set of APIs for general business logic  Robust, supported environment for general business applications  Amazing level of multi-platform support • What if you want to go further?  Exploit a piece of Notes C API interface?  Call a platform-specific DLL? 7
  • 8. Why Worry About Platform Independence? • Currently, something like:  90% of all Domino servers run on Windows  95% of all Notes clients run on Windows • Why not write platform code for that and that alone?  A short-sighted decision  Your environment may start supporting more platforms  Windows 64-bit coming over the horizon  Treat it as a separate platform  As was 16-bit Windows • Corporate push to centralize and consolidate  Especially on medium-high level platforms  Linux, Solaris, AIX, HP/UX 8
  • 9. Be Pragmatic — The Skills Triangle Few Code Examples Harder C API LotusScript, Java Easier Many Code Examples 9
  • 10. Be Pragmatic • Call the Notes C API from LotusScript if:  You cannot find a supported architectural solution  You have the skills to support Notes C API coding  You might possibly require multi-platform code  You accept that this code will be harder to write, harder to maintain, and harder to support 10
  • 11. Architectures — Conclusion • If you have to step outside the comfort zone, you have two choices:  LotusScript eXtension Toolkit (LSX)  This builds a library — on Windows, a DLL — which allows you to extend LotusScript with custom C code  Fairly straightforward  Not part of this presentation  LotusScript calling the Notes C API interface directly  Not straightforward  One code base can cover all platforms 11
  • 12. What We’ll Cover … • Introduction • Architectures • Platform differences • Calling simple Notes C API • Notes C API handles • Custom classes help hide platform differences • Complex Notes C API • Pros and cons • Wrap-up 12
  • 13. Platform Differences: Definitions • In LotusScript, platform differences are taken care of  Not so in direct LotusScript to C API calls • Each platform represents data in a subtly different manner  For instance, the HANDLE datatype has different lengths on different platforms:  32 bits long in Windows and OS/400  16 bits long on AIX, Macintosh, etc.  Memory alignment widths on iSeries are different from other platforms 13
  • 14. Platform-Specific Implementations • In order to call the Notes library, you will have to link to different library files on each platform:  Windows/32: nnotes.dll  Solaris, Linux: libnotes.so  AIX: lnotes_r.a  HPUX: libnotes.sl  MacOs, OS/X: NotesLib  OS/400: libnotes.srvpgm  OS/390 libnotes  OS/2: lnotes.dll  Windows Alpha: anotes.dll 14
  • 15. Platform Differences: Endians • Little endians  Some platforms represent multi-byte numbers with the lowest value bytes in the lower segments of memory • Big endians  Macintosh (on PowerPC!) represents the larger value bytes in the lower segments of memory 15
  • 16. Coding for Platform Differences • Our challenge is to construct a code sequence that:  Knows which platform it’s running on  Makes calls to the platform-specific library file  Understands platform differences in terms of alignment and data item size  And most importantly — fails “safe” when presented with a new platform that it cannot deal with • This is not a trivial exercise  My estimate is that this exercise will take at least 5-10 times more effort — development and testing — than a normal LotusScript business function 16
  • 17. Decoding Signatures Bold indicates a different memory structure for the same type across platforms. In other words, “Watch out!” 17
  • 18. What We’ll Cover … • Introduction • Architectures • Platform differences • Calling simple Notes C API • Notes C API handles • Custom classes help hide platform differences • Complex Notes C API • Pros and cons • Wrap-up 18
  • 19. Calling a Simple C API Function • Let’s get our feet wet with some simple API  No complex sequence of calls handling a shared memory resource  A single call with simple datatypes  A call that will not result in client memory corruption should we get it wrong  Hopefully • Let’s call it from Windows and ignore other platforms  A function that tells you the network latency, in milliseconds, between the current Notes instance and a target server  NSFGetServerLatency 19
  • 20. NSFGetServerLatency() — API Reference • We shall call NSFGetServerLatency()  This returns the network latency time described in milliseconds for a call to a remote server • Useful to decide which server is closest in terms of network response • Its signature from the C API reference is: STATUS LNPUBLIC NSFGetServerLatency( char far *ServerName, DWORD Timeout, DWORD far *retClientToServerMS, DWORD far *retServerToClientMS, WORD far *ServerVersion); 20
  • 21. NSFGetServerLatency() — API Reference (cont.) • From Lotus C API Notes/Domino 7.0 Reference: Input Parameters ServerName — Null-terminated string containing the name of the server to query. Timeout — Number of milliseconds to wait for a reply from the server. A timeout of 0 indicates that the default timeout value is to be used. Output Parameters (routine) — Return status from this call: NOERROR — Success retClientToServerMS — Optional — If not NULL, the number of milliseconds required to send the request to the server is stored at this address. retServerToClientMS — Optional — If not NULL, the number of milliseconds required for the reply to return from the server is stored at this address. ServerVersion — Optional — If not NULL, the server version (the Domino build number for the server) is stored at this address. 21
  • 22. Simple C API Calling ' This is a constant for our windows-based ' Library file: Const LIB_W32 = "nnotes.dll" ' Declare our function for windows Declare Function W32_NSFGetServerLatency _ Lib LIB_W32 Alias {NSFGetServerLatency} (_ Byval ServerName As Lmbcs String, _ Byval Timeout As Long, _ retClientToServerMS As Long, _ retServerToClientMS As Long, _ ServerVersion As Integer) As Integer 22
  • 23. Simple C API Calling: Execution ' A function to get network latency time... Public Function getServerLatency _ (strServer As String) As Long Dim nnServer As New NotesName(strServer) Dim ToServer As Long, fromServer As Long Dim ver As Integer Dim timeout As Long timeout = 1000 ' 1000ms == 1 second Call W32_NSFGetServerLatency(nnServer.Canonical,_ timeout, toServer, fromServer, ver) ' Return both directional latencies added together getServerLatency = fromServer + ToServer End Function 23
  • 24. Simple C API Calling: Execution (cont.) Sub initialise Print “Calling function” Print “Latency is: “ + _ cstr(getServerLatency(“domino-90.hadsl.com/ HADSL/US”)) Print “Finished calling” end sub 24
  • 25. Database Handle Class — Introduction Demo of GetServerLatency 25
  • 26. Simple C API Calling: The Results • Running the agent “Example1” in the database produces the following runtime output: • Now, this is not production code! It requires:  Error handling  Multi-platform support 26
  • 27. What Can’t You Call? • Callback routines  Some Notes C API functions require you to specify mandatory callback routines — other pieces of C API that will get called by the target C API routine  LotusScript does not (and, IMHO, never will) support this  This rules out:  Extension Manager routines  Menu Addin routines  In terms of callback routines that allow you to specify optional callbacks (progress status indicators, etc.), you can pass in NULL (or ZERO), and the callback function will be ignored 27
  • 28. What We’ll Cover … • Introduction • Architectures • Platform differences • Calling simple Notes C API • Notes C API handles • Custom classes help hide platform differences • Complex Notes C API • Pros and cons • Wrap-up 28
  • 29. API Handles • A handle is a numeric reference to a structure in memory that the C API interface has created  For example, a handle to a Notes database • You do not deal with the memory structure itself; you deal with the handle to the memory structure  Example: NSFDbOpen() creates a new memory structure and gives you back the handle to that  You perform some work using NSFDInfoGet()  You close the database using NSFDbClose() 29
  • 30. Some Rules on Handles • There are different types of handles:  Document handle, database handle, etc.  Do not mix these handles up!  Doing so will crash the session/client/server • Always properly close your handles:  You have to properly trap all code branches  You have to keep track of the handle until you specifically de-allocate it  Not doing so will crash the session/client/server  It may not crash immediately  It may crash when the session, client, or server closes • You cannot change the value of the handle:  Doing so will crash the session/client/server 30
  • 31. Coding Around Handles • Track handles with classes  Perform the “open” of the handle on the class constructor  Perform the “close” of the handle on the class destructor  This means that no matter what happens, the handle operation will be properly closed — even if this is not specifically coded in your LotusScript  All the code specific to this handle operation is embedded in the class  And can include multi-platform code  All the consumer sees is an operation with this specific class 31
  • 32. Handle Summary • If you get a handle, you have to de-allocate it • Only use handle data you know is valid  If a handle == NULLHANDLE (or ZERO), then its not a valid handle • Bonus  NotesDocument.handle returns the current handle  You can easily use LotusScript to:  Find a document  Use direct calls to update information that the object model does not support (at the moment)  e.g., writing Notes replica items 32
  • 33. What We’ll Cover … • Introduction • Architectures • Platform differences • Calling simple Notes C API • Notes C API handles • Custom classes help hide platform differences • Complex Notes C API • Pros and cons • Wrap-up 33
  • 34. Custom Classes Can Help Us Considerably • We can construct classes which “hide” the platform differences • Each platform will have its own set of classes • Each code sequence will be small, easily maintainable • We can reuse common code • We can use the custom class constructor and destructor to ensure that resources are properly returned 34
  • 35. Custom Classes: Architecture Hidden Code Base Class Object Handler Platform Platform Specific Platform Specific Object Specific Object Handlers Object Handlers Handlers Factory Dim F As New APIFactoryClass() Dim dbH As Variant User Code Set dbH = F.getDbHandler() … 35
  • 36. Custom Classes • Our base class carries infrastructure code  Logging, string handling, etc. • Our object base class:  Defines the functions that we will use  Defines the business logic • Our platform-specific classes:  Override platform-specific functions with that platform’s implementation • Our factory class:  Helps the user of the code find the correct platform-specific class to use 36
  • 37. Is This Over-Engineered? • At first sight, this looks complex • However, it does allow us to:  Keep platform-specific code in well-defined classes  Leverage class constructor/destructor calls to ensure handles are properly managed  Perform a simple “Copy+Paste” of the object class to port to a new platform  Keep code sequences short and clear  Hide the implementation of the code from the consumer of the code 37
  • 38. Database Handle Class — Introduction Demo of IDRefresh 38
  • 39. What We’ll Cover … • Introduction • Architectures • Platform differences • Calling simple Notes C API • Notes C API handles • Custom classes help hide platform differences • Complex Notes C API • Pros and cons • Wrap-up 39
  • 40. Defining Complex C API • Complex C API is where:  More than one function call is required to perform a task  And/or  A HANDLE of some description is needed to reference a data object 40
  • 41. Complex C API • Where more than one function call is required to perform a function it is recommended that:  You use defensive coding techniques to understand and make “safe” every single coding branch possible  Any handle information used during this sequence is kept safe and its de-allocation specifically catered to  You use classes to ensure proper construction and destruction of handle information 41
  • 42. Our Complex Example • Is passed through a server and database path • Opens and gets the DBHANDLE for that database • Extracts the replication flags into a memory structure • Checks to see if the “Disable Replication” flag is on  If it is, switches that flag off  Saves the replication flags • Finally, closes the DBHANDLE 42
  • 43. We Use Custom Classes To … • Ensure that the DBHANDLE is always properly returned • Insulate the consumer of the code from the platform differences • Contain all the platform-specific code in separate classes • Keep it simple! 43
  • 44. Database Handle Class — Introduction Demo of DbHandleManager 44
  • 45. What We’ll Cover … • Introduction • Architectures • Platform differences • Calling simple Notes C API • Notes C API handles • Custom classes help hide platform differences • Complex Notes C API • Pros and cons • Wrap-up 45
  • 46. Know Your Battleground • LotusScript coding is:  Fairly straightforward  Supported  Platform-independent • Stepping outside of this comfort zone is going to be hard  Take far more time in development and testing  Push development resources to the limit  Only do this if you have an absolute business need 46
  • 47. LSX vs. Notes C API • You need to establish, based on your requirements, which is the best fit: Feature LSX LotusScript calling Notes C API Deployment Requires code deployment No deployment required Multi-platform Recompile LSX for each Same code in library platform C API integration Very good integration — Basic integration supports callbacks, etc. Code difficulty Medium Hard Stability Extremely stable Stable 47
  • 48. What We’ll Cover … • Introduction • Architectures • Platform differences • Calling simple Notes C API • Notes C API handles • Custom classes help hide platform differences • Complex Notes C API • Pros and cons • Wrap-up 48
  • 49. Resources • Normunds Kalnberzin, LotusScript to Lotus C API Programming Guide (November, 2003).  The ultimate reference  www.ls2capi.com • Tools for Lotus Notes and Domino  Lotus Notes API tips  www.nsftools.com/tips/APITips.htm • Working application with several Notes C API calls  Included on the conference CD 49
  • 50. 7 Key Points to Take Home • Calling Notes C API is difficult  Only use it if absolutely required • It has to be tested on all platforms • It can easily take out a client or server process • Use defensive coding! • Use classes to hide platform differences • It’s the next stage beyond the well-regulated LotusScript sandbox • It gives you all possible Notes capability 50
  • 51. Your Turn! How to contact me: Bill Buchan [email protected] 51