Bentley Communities
Bentley Communities
  • Site
  • User
  • Site
  • Search
  • User
  • Welcome
  • Products
  • Support
  • About
  • More
  • Cancel
MicroStation Programming
  • Product Communities
  • Developers and Programming
  • MicroStation Programming
  • More
  • Cancel
MicroStation Programming
MicroStation Programming - Wiki Building a simple MDL Application
    • Sign in
    • MicroStation Programming - Wiki
    • -MicroStation CONNECT Edition
      • +MicroStation SDK
      • +MicroStation MDL
      • +MicroStation VBA
      • -Training
        • +Common Topics
        • Customizing and Programming MicroStation CONNECT
        • +Developers Migrating to MicroStation CONNECT
        • Getting Started with Managed APIs
        • +Managed (C#) - Step By Step Guide
        • MicroStation CONNECT Developer Training
        • -Native (C++) - Step By Step Guide
          • Introduction and Prerequisites
          • Building a simple MDL Application
          • Detailed explanation of mke file
          • Creating elements in MDL applications
          • Adding commands to MDL applications
          • Creating a VS project and debugging the application
          • Implementing Interactive tools using DgnTools Class
          • Adding User Interface to a MDL Application
          • Launching applications through the customized interface
      • +Code Examples and Utilities
      • +Code Snippets
    • +MicroStation V8

     
     Questions about this article, topic, or product? Click here. 

    Building a simple MDL Application

    This topic will guide you step by step to build the simplest Hello World native code MDL application to load and run within MicroStation CONNECT Edition (MSCE).

    1. Create a folder for the project source code, e.g. D:\MDLSource\HelloWorld (or a preferred location).
    2. Start a Text Editor (or IDE) and either type, or copy and paste the following text and save it as file: e.g. D:\MDLSource\HelloWorld\HelloWorld.cpp. This file contains the function MdlMain (an entry point) that will be executed upon loading of an application. In this example we simply call an MDL C API function mdlDialog_dmsgsPrint that will open and display a text message "Hello World" in a dialog message box.


    /*----+ | HelloWorld.cpp | +----*/ 
    #include <Mstn\MdlApi\MdlApi.h> 
    extern "C" DLLEXPORT void MdlMain (int argc, WCharCP argv[]) { 
        mdlDialog_dmsgsPrint (L"Hello World");
    }

    Some explanations of the code above are as follow:

    • The header files in the MSCE SDK are stored within a hierarchy of folders and paths to header files are required, similar to Mstn\MdlApi above.
    • This MdlApi.h header file is very important and should be included first (or prior) to ensure other header file dependencies used by other classes and functions can be resolved properly.
    • The entry function of an MDL program must be called MdlMain, and its parameters are int and WCharCP. Note that the second parameter of the MSCE version of MdlMain has changed from V8i's char* to WCharCP (ie WChar const *). This is also an important difference between MSCE SDK and V8i SDK: all parts related to the text must be used instead of Unicode;
    • extern "C" causes the MdlMain function to be generated in the obj file in the form of C language instead of C++, so as to ensure that no function overload occurs;
    • DLLEXPORT is defined as __declspec(dllexport), indicating that the MdlMain function needs to be exported.

    3.Type the following in a text editor and save it as the file D:\MDLSource\HelloWorld\HelloWorld.r. The DllMdlApp resource is defined in this file. The function of this resource is to associate the MA (actually not the MA file name but the task identifier) ​​with the specified DLL file.

    #include <Mstn\MdlApi\rscdefs.r.h>
    #define  DLLAPPID     1
    
    DllMdlApp   DLLAPPID =
    {
        L"HELLOWORLD", L"HelloWorld"    // taskid, dllName
    }

    Some explanations of the code above are as follow:

    • DllMdlApp is a structure defined in the rscdef.r.h file. It consists of two parts: TaskId and DLL name;
    • TaskId can basically be understood as the name of the ma file, but it does not completely correspond. TaskId will be automatically converted to all uppercase characters and has a length limitation. Only when the name of your ma file is very long, will TaskId be different from the file name;
    • The MDL program consists of two files: a ma and a dll. When loading, first find the ma, and then find the corresponding .dll file to load through the DllMdlApp structure. Under normal circumstances, we will keep the names of ma and dll the same, but of course, they can be inconsistent.

    4. Copy and paste the following content into a file called HelloWorld.mke and save it under D:\MDLSource\HelloWorld. This file is the control file for generating the project. During the project generation process, bmake reads the content of the file and then calls the corresponding compiler and linker to generate the final MA and DLL from the source file.

    #--------------------------------------------------------
    #    MstnCE  HelloWorld.mke
    #--------------------------------------------------------
    PolicyFile = MicroStationPolicy.mki
    DEFAULT_TARGET_PROCESSOR_ARCHITECTURE=x64
    
    appName=HelloWorld
    appObjs = $(o)$(appName)$(oext)
    appRscs = $(o)$(appName).rsc
    baseDir = $(_MakeFilePath)
    mdlLibs = $(MSMDE)library/
    
    %include mdl.mki
    
    #--------------------------------------------------------
    # Create needed output directories if they don't exist
    #--------------------------------------------------------
    
    always:
    	 ~mkdir $(o)
    	 ~mkdir $(rscObjects)
    	 ~mkdir $(reqdObjs)
    
    #--------------------------------------------------------
    # Define macros for files included in our link and resource merge
    #--------------------------------------------------------
    DLM_NO_SIGN       = 1
    DLM_OBJECT_DEST   = $(o)
    DLM_NAME    =$(appName)
    DLM_NO_DLS   = 1
    DLM_NO_DEF   = 1
    DLM_NOENTRY   = 1
    DLM_OBJECT_FILES        = $(appObjs)
    DLM_NO_MANIFEST   = 1
    DLM_DEST   = $(mdlapps)
    
    LINKER_LIBRARIES        = $(mdlLibs)bentley.lib \
                              $(mdlLibs)BentleyAllocator.lib \
                              $(mdlLibs)mdlbltin.lib \
                              $(mdlLibs)RmgrTools.lib \
                              $(mdlLibs)BentleyGeom.lib \
                              $(mdlLibs)DgnPlatform.lib \
                              $(mdlLibs)dgnview.lib
    
    #-----------------------------------------------------------------------
    #   Generate resource files
    #-----------------------------------------------------------------------
    $(o)$(appName).rsc: $(baseDir)$(appName).r
    
    #---------------------------------------------
    #	Merge the app resources using rlib
    #---------------------------------------------
    $(o)$(appName).mi    : $(appRscs)
    	$(msg)
    	> $(o)make.opt
    	-o$@
    	$(appRscs)
    	<
    	$(RLibCmd) @$(o)make.opt
    	~time
    
    appRscs =   \
        $(o)$(appName).mi \
        $(o)$(appName).rsc
    
    $(DLM_DEST)$(appName).ma         : $(appRscs)
            $(msg)
            > $(rscObjects)make.opt
            -o$@
            $(appRscs)
            <
            $(RLibCmd) @$(rscObjects)make.opt
            ~time
    
    #-----------------------------------------------------------------------
    #	Builds any necessary C++ CODE modules and link them to DLL
    #-----------------------------------------------------------------------
    $(o)$(appName)$(oext):$(baseDir)$(appName).cpp
    
    %include dlmlink.mki
    
    # Blank line above required for MKE/MKI files, this line for editors that rip.

    This mke file controls the generation process of the entire project, and its content is relatively complicated. Please see the next topic for a detailed introduction.

    5. Right-click "Start > All Programs> Bentley > MicroStation CONNECT Edition SDK > MicroStation CONNECT Edition SDK", and select "Run as administrator" from the pop-up menu to start the MDL program development environment. After the development environment command prompt, type cd /d d:\mdlsource\helloworld and press Enter to enter the directory where our project is located, and then type bmake -a to generate HelloWorld.ma and HelloWorld.dll. These generated files are located in the …\MicroStation\mdlapps directory.

    The above two commands are explained as follows:

    • The cd command is an abbreviation of change directory, which can be used to change directory.
    • The /d parameter indicates that the drive has been changed to D drive.
    • bmake is not a Windows command, but a MicroStation SDK executable located under C:\Program Files\Bentley\MicroStationCONNECTSDK\bin. The role of this tool is to generate the project according to the instructions in the mke file (i.e. to generate executable ma and dll files from the source code), which is equivalent to the Build Solution menu item in VS.
    • The -a flag indicates that all target files are regenerated regardless of whether you have modified the source code or not. It is equivalent to Rebuild solution option in VS.

    Tips: Follow the steps in the Developer Notes section of the SDK Readme to create MicroStation CONNECT Edition SDK as a shortcut to the Windows desktop, then right-click the shortcut and select properties to open the shortcut properties, and then click the Advanced button to open the advanced property settings, check Run as administrator and press the Ok button to confirm. In the future, you can simply click this shortcut on the desktop to start the development environment.

    6. Start MicroStation.exe and open any DGN file. Ensure that the current workflow in the Ribbon interface is Drawing or Modeling, then select MDL Applications under the Utilities page to open the MDL dialog box, find and select HELLOWORLD in the Available Applications list box at the bottom of the dialog box. Then click the Load button to load our MDL application. A message box with the words Hello World pops up. 

    7. You need to unload the application before you rebuild any projects, otherwise you will encounter the error message shown in the figure below. The unloading method is to select HELLOWORLD in Loaded Applications at the top of the above MDL dialog box, and then click the Unload button.

    Tips: You can also load and unload MDL applications by typing commands. As shown in the figure below, open the input command dialog box, enter MDL LOAD HelloWorld to load the application, and enter MDL UNLOAD HelloWorld to unload the application.

    Note: MicroStation SDK development environment relies heavily on the short path of Windows (ie 8.3 format). If the disk where you installed Visual Studio or MicroStation does not support short paths, please refer to the link below to set it with FSUTIL.EXE. If you still cannot set it up, it is best to install VS and MSTN on a disk that supports short paths.

    Link: https://docs.microsoft.com/en-us/archive/blogs/josebda/windows-server-2012-file-server-tip-disable-8-3-naming-and-strip-those-short-names-too 

    Download Project Source Code: HelloWorld.zip. Contributed by: Jon Summers (LA Solutions Ltd).

    Prev:Introduction and Prerequisites Next:Detailed explanation of mke file
    • Share
    • History
    • More
    • Cancel
    • Showgata Chakraborty Created by Bentley Colleague Showgata Chakraborty
    • When: Sun, Nov 8 2020 8:30 AM
    • Ayushi Basaiwal Last revision by Bentley Colleague Ayushi Basaiwal
    • When: Tue, May 10 2022 1:45 PM
    • Revisions: 17
    • Comments: 0
    Recommended
    Related
    Communities
    • Home
    • Getting Started
    • Community Central
    • Products
    • Support
    • Secure File Upload
    • Feedback
    Support and Services
    • Home
    • Product Support
    • Downloads
    • Subscription Services Portal
    Training and Learning
    • Home
    • About Bentley Institute
    • My Learning History
    • Reference Books
    Social Media
    •    LinkedIn
    •    Facebook
    •    Twitter
    •    YouTube
    •    RSS Feed
    •    Email

    © 2022 Bentley Systems, Incorporated  |  Contact Us  |  Privacy |  Terms of Use  |  Cookies