Updated on 2023-03-23 GMT+08:00

Scenario 1: Initialization

Description

The initialization API can be called for SDK initialization after a third-party application starts. You only need to call this API once. You must call this API before calling other functional APIs.

Service Process

During SDK initialization, change the name of the HwmSdk.exe file in the HwmSdk folder, call the Init API, and then process the callback function OnInitResult.

  1. Change the name of the .exe file.

    Change the name of the HwmSdk.exe file in the HwmSdk folder to MySdk.exe.

  2. Call the API.

    1. Construct the HwmInitInfo data structure.
      1. Define the derived class demoNotifyProc that it inherits from the base class HwmAgentNotify, and create a derived class object.
        1
        static demoNotifyProc *notifyObj = new demoNotifyProc();
        
      1. Define the derived class demoCallbackProc that it inherits from the base class HwmAgentCallback, and create a derived class object.
        1
        static demoCallbackProc *callbackObj = new demoCallbackProc();
        
      1. Construct other variables.
    2. Call the Init API to initialize the configuration. The data in the preceding step is used as input parameters.

  3. Implement the callback function.

    Implement the OnInitResult function.

Sample Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Call the initialization API and start the application.
*/
int CdemoBeforeLoginDlg::Init()
{
    // Assemble the input parameter structure.
    hwmsdkagent::HwmInitInfo initParam;
    memset(&initParam, 0, sizeof(HwmInitInfo));
 
    // (Not required for the 32-bit Windows platform) Configure the SDK path for the x64 platform. Assume that the HwmSdk directory is E:\\Hello_World\\debug\\win32\\ and the .exe file is MySdk.exe.
    std::string path = "E:\\Hello_World\\debug\\win32\\HwmSdk\\MySdk.exe";
    //Convert the SDK path into the UTF-8 format and copy it to initParam. The conversion code is omitted here.
    strcpy_s(initParam.exePath, HWM_MAX_FILE_PATH_LEN, path.c_str());
    //Specify the log path, convert the path into the UTF-8 format, and copy the converted path to initParam. The conversion code is omitted here.
    std::string logPath = "E:\\Hello_World\\debug\\win32\\MySdk\\log\\";
    strcpy_s(initParam.logPath, HWM_MAX_FILE_PATH_LEN, logPath.c_str());
    //Specify the data path, convert the path into the UTF-8 format, and copy the converted path to initParam. The conversion code is omitted here.
    std::string userDataPath = "E:\\Hello_World\\debug\\win32\\MySdk\\UserData\\";
    strcpy_s(initParam.userDataPath, HWM_MAX_FILE_PATH_LEN, userDataPath.c_str());
 
    string appId("602b111272ad4b6989e683c27e6e87d4");
    strncpy_s(initParam.appId, appId.c_str(), HWM_MAX_APPID_LEN);
    
    initParam.notify = notifyObj;
    initParam.callback = callbackObj;
    initParam.authType = hwmsdkagent::HWM_AUTH_TYPE_APPID;
    int ret = hwmsdkagent::Init(&initParam);
 
    return ret;
}

The value of exePath is the parent directory of the HwmSdk directory. For example, if the HwmSdk directory is E:\Hello_World\debug\win32\HwmSdk, the value of exePath is E:\Hello_World\debug\win32\.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
/**
* Initialization callback
*/
void demoCallbackProc::OnInitResult(hwmsdk::HwmErrCode ret, const char* msg, HwmSdkInfo *sdkInfo)
{
    CString codeStr;
    codeStr.Format(_T("%d"), ret);
    string msgStr = CTools::UTF82MultiByte(msg);
    string verStr = "";
    if (sdkInfo != NULL) {
        verStr = CTools::UTF82MultiByte(sdkInfo->version);
    }
    CString tips = _T("OnInitResult code:") + codeStr + _T(", msg:") + CString(msgStr.c_str()) + _T(" version:") + CString(verStr.c_str());
    AfxMessageBox(tips);
 
}

The sample code in the typical scenario and API reference is pseudo code and cannot be directly used.