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 IntegerDeclare Sub MainDeclare Sub EndHandlerDeclare Function CleverFunction(ByVal sInput As String) As String
'********************Sub MainDim 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: " & sInputEnd 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.