written by Sumbera, S.,  April. 2003

6. Going native with MDL

this chapter  DISCUSS how to easily migrate interpreted MDL code into native code.

 

 

download sample project

 

Intro

No doubt that V8 brought relief for Windows based developers in a way that Bentley made improvements for interoperability between MDL and native code and moreover they partially enabled to write entire code  natively. I wrote partially because it is not still 'perfect', however  good enough to get rid of interpreted MDL, command line debugger, or may be make files or some slow performance issues. Well, this chapter is intended for those who would like to hear my opinion and suggestion when porting some MDL code into native. if you have already some code in traditional MDL and you want smoothly try the native compilation, you are the right person to read this short tutorial.

 

Choose your IDE

Well there are a lot of nice IDE for C/C++  code, however I would recommend you to use Microsoft Visual Studio (because I don't have opportunity to try other one ..and I guess Borland C++ builder could be also gorgeous... ;). So read and understand  the article and FAQ of the research article :Setting Up Visual Studio 6 IDE for MDL programming.

 

 

Strategy of porting

Well, if you have some amount of code written in MDL which works correctly you have may be less reasons to port it into native, however who said you cannot have both at the same time? I mean that you may just do slight modification of your code to enable native compilation while preserving backup way with compilation into interpreted code... this mean that if you will find any problems in native code execution you may check the execution in interpreted code to see if all work correctly in original form...Well you may be surprised but not all still works as would be expected in native code (for instance some events in version of v8.0..)

 

What need to be changed:

Private MdlCommandNumbers commandNumbers [] =

{

{foo_do,   CMD_DOING_DO},

{foo_undo, CMD_DOING_UNDO},

{foo_redo, CMD_DOING_REDO},

 0 /* The table must be NULL-terminated */

};

 

simmilary you need to replace cmdName with :

 

Private MdlCommandNames commandNames [] =

{

{foo_do,   "foo_do"},

{foo_undo, "foo_undo"},

{foo_redo, "foo_redo"},

 0 /* The table must be NULL-terminated */

};

 

 

..in Main function register your command number and command name  maps :

mdlSystem_registerCommandNumbers (commandNumbers);

mdlSystem_registerCommandNames   (commandNames);

 

alternatively you may use functions

mdlSystem_registerCommandNumbersByDesc or

mdlSystem_registerCommandNamesByDesc

 

#if defined winNT

 #define MAIN MdlMain /* native entry point */

 #include <windows.h>

 #define __NORECTANGLE__ 1

 /*default entry point for dll */

 BOOL WINAPI DllMain (HANDLE hMod, DWORD reason, LPVOID res){ return 1;}

#else

 #define MAIN main /* interpreted entry point */

#endif

 

As you may see above, we defined  different entry points for interpreted and native code. This way you will be able to maintain in one source file different builds - either for native or for interpreted release. But you need to reflect the definition above in your main function:

 

DLLEXPORT int MAIN (int argc, char *argv[] )

 

DLLEXPORT is defined in basedefs.h as :

#if defined (winNT) && !defined (mdl)

# define DLLEXPORT __declspec( dllexport )

# define DLLIMPORT __declspec( dllimport )

#else

# define DLLEXPORT

# define DLLIMPORT

#endif

If code is compiled and loaded as native, first is called DllMain with reason DLL_PROCESS_ATTACH  then mdlMain. In unload process DllMain is called as well with reason DLL_PROCESS_DETACH.

 

 

 

Integration the code with Visual Studio

This issue has been already discussed in article Setting up Visual studio for MDL programming. However, there is one interesting point to mention. You may directly debug your native code from IDE ebven you have defined the project for Visual Studio as MAKE FILE !

 

If you add this into Program arguments of Project/Settings you will run your native code as console application

 

 

..and execution lokos like this - IDE debugging:

 

 

 

while adding this will run your application as interpreted code:

 

..and exectution looks as you know  I guess perfectly - command line debugger:

 

 

 

Setting valid breakpoints

Visual Studio needs to load symbols for your dll into memory before the dll is really loaded by MicroStation process. So you need to specify Additional DLLs in Projects/Settings/Debug Tab/Category combo Box/Additional Dlls. There you specify your dll with full path:

 

 

Then Debugger will first load your dll symbols and then others dlls:

 

 

For more information for dll breakpoints follow this link to MSDN

 

..to be ..may be... continued

mail comments to stanislav@sumbera.com