Thanks for the prompt response. That addition worked perfectly.
Original Message:
Sent: 01-08-2020 08:18
From: Peter Horsbøll Møller
Subject: Creating public MapBasic functions for MapInfo Pro 17.0.1
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
Original Message:
Sent: 01-07-2020 19:22
From: Tom Malpass
Subject: Creating public MapBasic functions for MapInfo Pro 17.0.1
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
Original Message:
Sent: 10-12-2018 06:09
From: Peter Horsbøll Møller
Subject: Creating public MapBasic functions for MapInfo Pro 17.0.1
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 a 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 msall 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 the follow four things:
- Add the declaration for the RegisterPublicFunction
- 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.