MapInfo Pro Developers User Group

Expand all | Collapse all

Sharing global variables between mbx's

  • 1.  Sharing global variables between mbx's

    Posted 8 days ago
    Hi,
    I'm trying to access global variables held in one mbx from another mbx.
    I can do this via DDE but saw the GetMBXGlobalVariable in the IMapInfoPro.def and thought this would be better.
    I've got the code shown below, but what ever variable I try, I just get the value 3 back.
    We're still using MB16 with MI v16.0.3
    Any ideas?
    Cheers
    David


    Declare Method GetMBXGlobalVariable Class "MapInfo.Types.IMapBasicApplication" Lib "MapInfo.Types, Version=16.0.0.0, Culture=neutral, PublicKeyToken=1c8d81d2ee78b75d" alias GetGlobalVariable (ByVal IMBXInstance As This, ByVal name As String) As This
    Declare Method MapBasicApplication Class "MapInfo.Types.IMapInfoPro" Lib "MapInfo.Types, Version=16.0.0.0, Culture=neutral, PublicKeyToken=1c8d81d2ee78b75d" alias GetMapBasicApplication (ByVal IMIProInstance As This, ByVal applicationName As String) As This
    Declare Function GetGlobalVar(byval sMBX as string,byval sVariable as string)as string
    '---------------------------------------------------------------------------------
    Function GetGlobalVar(byval sMBX as string, byval sVariable as string)as string
    dim mapinfoApplication, otherApp as This
    dim sRetVal as string
    'Get MIPro interface
    mapinfoApplication = SystemInfo(SYS_INFO_IMAPINFOAPPLICATION)

    'Get handle to current application
    otherApp = MapBasicApplication(mapinfoApplication, sMBX)
    sRetVal = GetMBXGlobalVariable(otherApp,sVariable)

    GetGlobalVar = sRetVal

    otherApp = NULL_PTR

    exit sub
    error_handler:
    note "Error: " + Error$() + " " + err()
    End function

    ------------------------------
    David Wilson
    Buchanan Computing
    ------------------------------


  • 2.  RE: Sharing global variables between mbx's

    Posted 8 days ago
      |   view attached
    Hi David

    The problem is that the GetMBXGlobalVariable returns the actual variable as an object (type This) and not the value assigned to it.

    I have modified your function slightly to make it work:

    '---------------------------------------------------------------------------------
    Function GetGlobalVar(byval sMBX as string, byval sVariable as string) as string


    dim theGlobalVar, otherApp as This

      'Get handle to current application
       otherApp = MapBasicApplication(mapinfoApplication, sMBX)
       If otherApp = 0 Then
          Note "Application '" & sMBX & "' might not be running!"
          Exit Sub
       End If

       theGlobalVar = GetMBXGlobalVariable(otherApp, sVariable)
       'GetGlobalVarName(theGlobalVar) 'Returns the name of the global variable
       'GetGlobalVarValue(theGlobalVar) 'Returns the value assigned to of the global variable
        GetGlobalVar = GetGlobalVarValue(theGlobalVar)


       theGlobalVar = NULL_PTR
       otherApp = NULL_PTR


    exit sub
       error_handler: note "Error: " + Error$() + " " + err()


    End function

    Please also notice that I decided to move the mapInfoApplication definition and assignment out of the function. I have included my full sample application so that you can see how I have structured it with a module level variable for the mapInfoApplication. It could also have been a global variable.

    I'll make sure to add this function to the RIBBONLib :-)

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

    Attachment(s)

    zip
    GetGlobalVar.zip   636B 1 version


  • 3.  RE: Sharing global variables between mbx's

    Posted 8 days ago
    Hi Peter,
    Many thanks for the quick reply. I should have realised that, as the declaration returns 'as This'.
    Cheers
    David

    ------------------------------
    David Wilson
    Knowledge Community Shared Account
    Shelton CT
    ------------------------------