Skip to main content

OH_Ability_CreateNativeChildProcess

Function OH_Ability_CreateNativeChildProcess 

Source
pub unsafe extern "C" fn OH_Ability_CreateNativeChildProcess(
    libName: *const c_char,
    onProcessStarted: OH_Ability_OnNativeChildProcessStarted,
) -> c_int
Available on crate feature api-12 only.
Expand description

Creates a child process, loads the specified dynamic library file, and returns the startup result asynchronously through a callback parameter. The callback notification is an independent thread. When implementing the callback function, pay attention to thread synchronization and do not perform time-consuming operations to avoid long-time blocking.

The dynamic library specified must implement and export the following functions:

  1. OHIPCRemoteStub* NativeChildProcess_OnConnect()

  2. void NativeChildProcess_MainProc()

The processing logic sequence is shown in the following pseudocode:

Main process:

  1. OH_Ability_CreateNativeChildProcess(libName, onProcessStartedCallback)

Child process:

  1. dlopen(libName)

  2. dlsym(“NativeChildProcess_OnConnect”)

  3. dlsym(“NativeChildProcess_MainProc”)

  4. ipcRemote = NativeChildProcess_OnConnect()

  5. NativeChildProcess_MainProc()

Main process:

  1. onProcessStartedCallback(ipcRemote, errCode)

Child process:

  1. The child process exits after the NativeChildProcess_MainProc() function is returned.

§Arguments

  • libName - Name of the dynamic library file loaded in the child process. The value cannot be nullptr.

  • onProcessStarted - Pointer to the callback function for notifying the child process startup result. The value cannot be nullptr. For details, see OH_Ability_OnNativeChildProcessStarted.

§Returns

  • Returns [NCP_NO_ERROR] if the call is successful, but the actual startup result is notified by the callback function.

Returns [NCP_ERR_INVALID_PARAM] if the dynamic library name or callback function pointer is invalid.

Returns [NCP_ERR_NOT_SUPPORTED] if the device does not support the creation of native child processes.

Returns [NCP_ERR_MULTI_PROCESS_DISABLED] if the multi-process mode is disabled on the device.

Returns [NCP_ERR_ALREADY_IN_CHILD] if it is not allowed to create another child process in the child process.

Returns [NCP_ERR_MAX_CHILD_PROCESSES_REACHED] if the maximum number of native child processes is reached.

For details, see [Ability_NativeChildProcess_ErrCode]. OH_Ability_OnNativeChildProcessStarted Available since API-level: 12