MapInfo Pro Developers User Group

  • 1.  Using a .NET Method from MapBasic

    Employee
    Posted 04-05-2019 05:38
    Edited by Peter Møller 04-11-2019 10:24
    You have for a long time been able to use .NET method from MapBasic. This gives you access to a very powerful world of new capabilities when developing solutions for MapInfo Pro.

    There are a number of things that are easier to do in .NET than in MapBasic.

    In order to use a .NET method developed with one of the .NET development languages such as C# and VB.NET, you need to make sure that
    • your method is declared as a static method,
    • the number and type of parameters to match
    • the name space and class name match
    In MapBasic you can use the Declare Method to describe the method to MapBasic. Here is a description of the Declare Method statement from the MapBasic Help File (see the Help File for full description).

    Syntax

    Declare Method fname
       Class "class_name" 
       Lib "assembly_name"  [ Alias function_alias ] 
       ( [ [ ByVal ] parameter [ , parameter... ] As var_type ]
         [ , [ ByVal ] parameter [ , parameter... ] As var_type... ]
       )  [ As return_type ]


    • fname is the name by which a function will be called; if the optional Alias clause is omitted, fname must be the same as the actual .NET method/function name. This option can not be longer than 31 characters.
    • class_name is the name of the .NET class that provides the function to be called, including the class's namespace (such as System.Windows.Forms.MessageBox)
    • assembly_name is the name of a .NET assembly file, such as filename.dll. If the assembly is to be loaded from the GAC, assembly_name must be a fully qualified assembly name.
    • function_alias is the original name of the .NET method/function (the name as defined in the .NET assembly). Note: Include the Alias clause only when you want to call the method by a name other than its original name.
    • parameter is the name of a parameter to the function.
    • var_type is a MapBasic data type, such as Integer
    • return_type is a standard MapBasic scalar variable type, such as Integer. If the As clause is omitted, the MapBasic program can call the method as a Sub (using the Call statement).

    Description

    The Declare Method statement allows a MapBasic program to call a method (function or procedure) from a .NET assembly. The .NET assembly can be created using various languages, such as C# or VB.NET. For details on calling .NET from MapBasic, see the MapBasic User Guide.
    MapBasic programs can only call .NET methods or functions that are declared as static. (VB.NET refers to such functions as "shared functions," while C# refers to them as "static methods."). This limitation has however been removed with the introduction of the 64 bit version of MapInfo Pro.

    Examples

    Here is a simple example of a C# class that provides a static method:
    namespace MyProduct
    {
       class MyWrapper
       {
          public static int ShowMessage(string s)
          {
             System.Windows.Forms.MessageBox.Show(s);
             return 0; 
          }
       }
    }

    In VB.NET, the class definition might look like this.
    Namespace MyProduct 
       Public Class MyWrapper
          Public Shared Function ShowMessage(ByVal s As String) As Integer
             System.Windows.Forms.MessageBox.Show(s)    
             Return 0
          End Function 
       End Class
    End Namespace


    A MapBasic program could call the method with this syntax:
    Declare Method ShowMessage  Class "MyProduct.MyWrapper" Lib "MyAssembly.DLL" (ByVal str As String) As Integer
    . . . 
    Dim retval As Integer 
    retval = ShowMessage("Here I am")


    ------------------------------
    Peter Horsbøll Møller
    Pitney Bowes
    ------------------------------


  • 2.  RE: Using a .NET Method from MapBasic

    Employee
    Posted 04-07-2019 19:50
    Edited by Bob Fortin 04-07-2019 19:50
    Hi Peter,

    Nice post. One thing is that the line in bold about only being able to call static methods is no longer true since v12.5 64 bit.

    You can call a constructor for a .NET class and then invoke methods on it.

    See the example from the help topic for the "This" variable type in MapBasic reference.

    -Bob

    ------------------------------
    Bob Fortin
    Software Architect and Distinguished Engineer
    ------------------------------



  • 3.  RE: Using a .NET Method from MapBasic

    Posted 04-08-2019 21:03
    Edited by Steven Graham 04-08-2019 21:40
    Hi Peter,

    I Think I have it all worked out now, seems to be working, now just to finish the interface and handling with MapBasic!

    Thanks a lot for all your help Peter!

    Once I have it tested and working, perhaps I should release this as a plugin for other local governments in NSW.
    ------------------------------
    Steven Graham
    Software Architect and Distinguished Computer Engineer
    Tenterfield Shire Council
    ------------------------------



  • 4.  RE: Using a .NET Method from MapBasic

    Employee
    Posted 04-11-2019 10:29
    If you want to share it that would be great.

    I can help you get it up on our Community Download site if you want

    ------------------------------
    Peter Horsbøll Møller
    Pitney Bowes
    ------------------------------



  • 5.  RE: Using a .NET Method from MapBasic

    Posted 04-22-2019 20:48
      |   view attached
    Hi Peter, 

    After my function completes an address lookup, it returns into 2 variables with the coodrinates
    Private Shared Lat As Decimal
    Private Shared Lng As Decimal

    There only way to access these variables is using properties:

    Public Shared ReadOnly Property Latitude
    Get
    Return Lat
    End Get
    End Property

    Public Shared ReadOnly Property Longitude
    Get
    Return Lng
    End Get
    End Property

    Is there a way I can access a DLL Property within MapBasic?

    If not, what do you suggest the best way to return coordinates would be (or alternatively, control mapinfo to pan and zoom to said coordinates).

    Please find my source code attached. Any help would be greatly appreciated.

    ------------------------------
    Steven Graham
    Software Architect and Distinguished Computer Engineer
    Tenterfield Shire Council
    ------------------------------

    Attachment(s)

    zip
    repos.zip   7.80 MB 1 version


  • 6.  RE: Using a .NET Method from MapBasic

    Employee
    Posted 04-23-2019 04:24
    Hi Steven,

    I brief look in the IMapInfoPro.def file that comes with MapBasic illustrates a solution, I think.

    Declare Method GetMICtrlVisible Class "MapInfo.Types.IMapInfoControl"
       Lib "MapInfo.Types, Version=12.5.0.0, Culture=neutral, PublicKeyToken=1c8d81d2ee78b75d"
       alias get_Visible (ByVal IMICtrlInstance As This) As Logical

    Declare Method SetMICtrlVisible Class "MapInfo.Types.IMapInfoControl"
       Lib "MapInfo.Types, Version=12.5.0.0, Culture=neutral, PublicKeyToken=1c8d81d2ee78b75d"
       alias set_Visible (ByVal IMICtrlInstance As This, ByVal visible As Logical)


    These two declare statements also refer to a property.
    This is however a bit different that your approach as you here have a This variable in your MapBasic application referencing the .NET object that you pass as a parameter to the methods.

    I think this is how you can access a .NET property from MapBasic. I'm not quite sure if you need to do something specific on the .NET side for this to work. Maybe @John Teague og @Bob Fortin would know.

    Another approach is to create a new method in .NET that allows you to pass two parameters byref. In the ​​method you can now assign the value from your properties to these two parameters and back in your MapBasic tool, you can access the values assigned.

    ------------------------------
    Peter Horsbøll Møller
    Pitney Bowes
    ------------------------------



  • 7.  RE: Using a .NET Method from MapBasic

    Posted 04-23-2019 20:46
    Cheers Pete,

    I ended up getting around this by using 2 new methods getLatitude and getLongitude and calling them after the Location was successful.

    I had a side thought to this, is there a way I can integrate the LPI addressing into MapInfo's Geocoding services? This might be a better way to implement this for other local governments in NSW to use it.

    Please see https://maps.six.nsw.gov.au/sws/AddressLocation.html for more information.

    The Web Service URL is formatted like this:
    http://maps.six.nsw.gov.au/services/public/Address_Location?houseNumber=346&roadName=Panorama&roadType=Ave&suburb=Bathurst&postCode=2795&projection=EPSG%3A4326

    ------------------------------
    Steven Graham
    Software Architect and Distinguished Computer Engineer
    Tenterfield Shire Council
    ------------------------------



  • 8.  RE: Using a .NET Method from MapBasic

    Employee
    Posted 04-24-2019 02:48
    Hi Steven

    That's certainly also a good way of doing it, and easier to use afterwards. Personally, I also prefer to create individual functions that return a single value instead of passing multiple variables byref to a function.

    To your question on integrating the LPI addressing into the new Geocoding tool in Pro, I'd say no. The geocoding tool has been coded against our LI API, Geocode. I'd expect the structure of the two services to be different.

    One way of making that work is to build a sort of a proxy that emulates the services of our Geocode API. But that's a completely different story. I'm certain it can be done as we did this ourselves when we switched from Envinsa geocoding to Spectrum geocoding a few years back.

    ------------------------------
    Peter Horsbøll Møller
    Pitney Bowes
    ------------------------------



  • 9.  RE: Using a .NET Method from MapBasic

    Posted 04-24-2019 08:34
    Edited by Eric Blasenheim 04-24-2019 09:07

    It is important for me to note that both Geocoding and Drivetime interfaces inside MapInfo Pro work with both LI API interaces and an On Premise Spectrum installation. 
    The choice is clearly based on your organization. We have a number of larger companies who already process many geocodes and some drivetimes (isochrones), make this available for their internal MapInfo Pro users.



    ------------------------------
    Eric Blasenheim
    Spectrum Spatial Technical Product Manager
    Troy, NY
    ------------------------------



  • 10.  RE: Using a .NET Method from MapBasic

    Employee
    Posted 04-11-2019 10:26
    Thanks Bob, I have added a clarification on this to the post above.

    ------------------------------
    Peter Horsbøll Møller
    Pitney Bowes
    ------------------------------