MapInfo Pro

Expand all | Collapse all

Creating public MapBasic functions for MapInfo Pro 17.0.1

  • 1.  Creating public MapBasic functions for MapInfo Pro 17.0.1

    Posted 10-12-2018 10:10
    Edited by Peter Horsbøll Møller 01-10-2020 03:41

    With the release of MapInfo Pro 17.0.1, we have improved the way you can use custom functions from a running MapBasic application directly from MapInfo Pro.

    MapInfo Pro 17.0 allowed the use of these thru the MapBasic function Exec(). This was a bit cumbersome but did work quite well.

    With MapInfo Pro 17.0.1 we have given the MapBasic developers more control over the process. As a MapBasic developer you can now register a custom MapBasic function for public usage inside MapInfo Pro, say from the MapBasic window, in the SQL Select dialog, in Label Expressions and in a lot of other places.

    Here are some tips on how to do this.

    RIBBONLib

    I have published a new version of the RIBBONLib to the mbLibrary on Github. Find it here: https://github.com/PeterHorsbollMoller/mbLibrary

    The RIBBONLib has three functions that support registering these custom MapBasic functions. You only need to concentrate on one: RBNRegisterFunctionAsPublic

    The other two are used for unregistering the functions for example when the application is unloaded. This is done automatically when the tool is unloaded via the RBNEndHandler.

    The function RBNRegisterFunctionAsPublic takes three parameters:

    • The name of the function
    • The name that MapInfo Pro users will use and see in dropdown lists
    • A short description

    Here is a small example on how to use if taken from the next version of WindowHelper:

    If RBNRegisterFunctionAsPublic("STRINGReplace", "StringReplace$", "Search for a substring and replace it with another substring") Then

    '**Function successfully added

    End If

    You will now see the function in the functions dropdown for example in SQL Select

    See Attachment

    In your own MapBasic module

    You don't need to use the RIBBONLib if you prefer to keep it in your own module.

    To do this in your own module, you will need to follow four things:

    • Add the declaration for the RegisterPublicFunction. If you are using MapBasic 17.0.3 or newer, this declaration is included in IMapInfoPro.def.
    • Make sure you have an EndHandler
    • Get the IMapInfoApplication object/interface
    • Get the IMapBasicApplication object/interface
    • Call the RegisterPublicFunction for each function you want to register

     

    Include "MapBasic.def"
    Include "IMapInfoPro.def"
    Declare Method RegisterPublicMBXFunction Class "MapInfo.Types.IMapBasicApplication" Lib "MapInfo.Types, Version=12.5.0.0, Culture=neutral, PublicKeyToken=1c8d81d2ee78b75d"Alias RegisterPublicFunction
           ( ByVal IMBXInstance As This, ByVal functionName As String
           , ByVal publicName as String, ByVal description as String
           ) As Integer
    Declare Sub Main
    Declare Sub EndHandler
    Declare Function CleverFunction(ByVal sInput As String) As String
    '********************

    Sub Main
    Dim  theMapInfoApplication As This,
         theMapBasicApplication As This
     
       'Get MIPro interface
       theMapInfoApplication = SystemInfo(SYS_INFO_IMAPINFOAPPLICATION)
       'Get MapBasic interface
       theMapBasicApplication = MapBasicApplication(theMapInfoApplication, ApplicationName$())
       If RegisterPublicMBXFunction("CleverFunction", "MakeClever", "Adds clever before the the input string") > 0 Then
          '**Function added!
       End if
    End Sub
    '********************
    Function CleverFunction(ByVal sInput As String) As String

       CleverFunction = "Clever: " & sInput
    End Function
    '********************
    Sub EndHandler

       '**Doesn't have to do anything
    End Sub

     

    Compile the above and run it in MapInfo Pro 17.0.1. In your functions list, you should now see "MakeClever" as a new function

    See Attachment

    I have attached the source code for the CleverFunction below. I'm sure you can come up with an even more clever function that this one.



  • 2.  RE: Creating public MapBasic functions for MapInfo Pro 17.0.1

    Posted 01-07-2020 19:23
    Hi Peter,

    I am trying to use the above code to create a custom function, however I am getting the following error "Incorrect number of arguments to function" and it seems to be realted to the line If RegisterPublicMBXFunction("CleverFunction", "MakeClever", "Adds clever before the the input string") > 0 Then. I am unsure if this is related to some outdated libraries I am using but it looks like it is missing the argument ByVal IMBXInstance As This.

    I am currently using MapInfo 17.0.4 and MapBasic 17.0.3.

    Thanks in advance,
    Tom Malpass

    ------------------------------
    Tom Malpass
    Acting GIS Coordinator
    Cairns Regional Council
    Cairns
    ------------------------------



  • 3.  RE: Creating public MapBasic functions for MapInfo Pro 17.0.1

    Posted 01-08-2020 08:19
    I think you are right, Tom, you are missing the reference to the running MapBasic application in the method.

    Try changing in to this:

    Dim theMapInfoApplication As This
    Dim theMapBasicApplication As This
    'Get MIPro interface
    theMapInfoApplication = SystemInfo(SYS_INFO_IMAPINFOAPPLICATION)

    'Get MapBasic interface
    theMapBasicApplication = MapBasicApplication(theMapInfoApplication, ApplicationName$())

    If RegisterPublicMBXFunction(theMapBasicApplication , "CleverFunction", "MakeClever", "Adds clever before the the input string") > 0 Then

    If you already have instances of the MapInfo Pro interface and the MapBasic Application inteface, you can use these instead of those I have added.

    ------------------------------
    Peter Horsbøll Møller
    Distinguished Engineer
    Pitney Bowes Software & Data
    ------------------------------



  • 4.  RE: Creating public MapBasic functions for MapInfo Pro 17.0.1

    Posted 01-09-2020 16:39
    Hi Peter,

    Thanks for the prompt response. That addition worked perfectly.

    Thanks again

    ------------------------------
    Tom Malpass
    Acting GIS Coordinator
    Cairns Regional Council
    Cairns
    ------------------------------