Initialization

内容表

关于

The macro MAXON_INITIALIZATION() is used to execute code when the application starts and when it shuts down. This is typically useful to allocate and free global data.

警告
If the "init" functions of MAXON_INITIALIZATION() returns an error, all components defined in the current source file will not be loaded.

用法

One can simply call global functions in the MAXON_INITIALIZATION() macro.

警告
MAXON_INITIALIZATION() is called before the classic core is loaded. This means that classic API functions and classes (e.g. GePrint() ) cannot be used in this context.
// This example declares a global string containing the machine name. // MAXON_INITIALIZATION is used to set the value on start up and free the resource on shut down.

#include " maxon/module.h "

#include " maxon/string.h " #include " maxon/application.h " #include " maxon/machineinfo.h "
maxon::String * g_machineName = nullptr ; // global string containing the machine name

// called on system start static maxon::Result<void> InitGlobalString() { iferr_scope ;

// allocate global string g_machineName = NewObj ( maxon::String ) iferr_return ;

// get machine data const maxon::DataDictionary data = maxon::Application::GetMachineInfo (); // get machine name *g_machineName = data.Get(maxon::MACHINEINFO::COMPUTERNAME) iferr_return ;

return maxon::OK ; }

// called on system end static void ClearGlobalString() { // delete global string if (g_machineName != nullptr ) DeleteObj (g_machineName); } MAXON_INITIALIZATION (InitGlobalString, ClearGlobalString);

Or simply use a lambda within the macro itself:

// This example declares a global string containing the user name. // MAXON_INITIALIZATION is used to set the value on start up and free the resource on shut down. maxon::String * g_userName = nullptr ; // global string containing the user name MAXON_INITIALIZATION (

// system startup []() -> maxon::Result<void> { iferr_scope ;

// allocate global string g_userName = NewObj ( maxon::String ) iferr_return ;

// get machine data const maxon::DataDictionary data = maxon::Application::GetMachineInfo (); // get machine name *g_userName = data.Get(maxon::MACHINEINFO::USERNAME) iferr_return ; return maxon::OK ; }, // system shutdown []() { // delete global string DeleteObj (g_userName); });

If no function should be called nullptr can be set.

延伸阅读

maxon::Application::GetMachineInfo
static MAXON_METHOD DataDictionary GetMachineInfo()
DeleteObj
#define DeleteObj(obj)
定义: newobj.h:159
string.h
module.h
maxon::String
定义: string.h:1197
maxon::OK
return OK
定义: apibase.h:2532
iferr_return
#define iferr_return
定义: resultbase.h:1434
MAXON_INITIALIZATION
#define MAXON_INITIALIZATION(...)
定义: module.h:735
maxon::Result< void >
iferr_scope
#define iferr_scope
定义: resultbase.h:1343
machineinfo.h
NewObj
#define NewObj(T,...)
定义: newobj.h:108
application.h

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1