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

Getting Started

This section uses SDK initialization as an example to describe how to use the Windows SDK for secondary development.

Preparations

During the development, ensure that the following environment requirements are met:

Table 1 Environment requirements

Environment and Tool

Version

Description

OS

Windows 10/11 Professional

Hardware requirements

  • CPU: i5-2400 quad-core, 3.1 GHz or above
  • Memory: 4 GB or above

Microsoft Visual Studio

Visual Studio 2017

-

Requesting test resources and app IDs

-

For details, see Preparations.

SDK Integration

The high DPI awareness function of each monitor must be enabled for the exe program that integrates the SDK on a computer with a high DPI (zoom ratio greater than 100%). Otherwise, the image will be displayed abnormally. For example, the image is blurry, or the image is incomplete or tilted on the receiver during sharing.

For details, see Figure 12.

  1. Decompress the SDK package to the local host.

    Figure 1 Decompressing the SDK package to the local host

    include: stores the header files required for compilation.

    lib: contains two subfolders, win32 and x64. The two subfolders store the .lib files that the compilation of the corresponding platform depends on.

    dll: contains two subfolders, win32 and x64. The two subfolders store the .dll files and the executable SDK .exe files that the running of the corresponding platform depends on.

    HwmSdk: This folder stores the common dependency libraries used by Windows 32-bit and Windows 64-bit.

    demo: stores demo code files and resource files.

  2. Create a project.

    1. Open Visual Studio 2017 and choose File > New > Project from the main menu.
      Figure 2 Opening a new project
    2. On the Create a new project page, choose Installed > Visual C++ from the navigation tree. Select MFC Application in the template list, set the project name to Hello_World, select E:\ from the Location drop-down list, and click OK.
      Figure 3 Creating a project
    3. On the Application Type page, set Application type to Dialog based, retain the default values for other parameters, and click Finish.
      Figure 4 Application type options

  3. Copy the static library files and header files to the project.

    1. Create a directory named SDK at the same level as the Hello_World.sln file in the Hello_World project directory.
      Figure 5 Creating the SDK directory

    2. Copy the lib and include directories in the SDK directory of the demo installation package to the Hello_World\SDK directory.
      Figure 6 Copying files

  4. Modify configuration properties of the project.

    The following operations are performed based on the Debug configuration and Win32 platform. When modifying the configuration and platform, add additional include directories and dependent libraries again.

    1. Right-click the Hello_World project and choose Properties from the shortcut menu.
      Figure 7 Project properties
    2. Choose Configuration Properties > C/C++ > General, change the value of Additional Include Directories to ..\SDK\include, and click Apply.
      Figure 8 Configuring Additional Include Directories
    3. Choose Configuration Properties > Linker > General, change the value of Additional Library Directories to ..\SDK\lib\win32, and click Apply.
      Figure 9 Configuring Additional Library Directories
    4. Choose Configuration Properties > Linker > Input, change the value of Additional Dependencies to hwm_sdk_agent.lib, and click Apply.
      Figure 10 Configuring Additional Dependencies
    5. Choose Configuration Properties > General, change the value of Output Directory to ..\debug\win32, and click OK.
      Figure 11 Configuring Output Directory
    6. In the left pane of the property page, choose Configuration Properties > Manifest Tools > Input and Output, change the value of DPI Awareness to Per Monitor High DPI Aware, and click OK.
      Figure 12 Configuring the DPI awareness function

  5. Add a GUI resource control.

    1. In the resource view, open the Dialog page corresponding to the project.
      Figure 13 Dialog page of the project
    2. Select all controls on the page and press Delete.
      Figure 14 Deleting existing controls
    3. Add the initialization button.
      1. Add the initialization button control. Open the toolbox page, select the button control, drag it to the dialog box, and adjust the width and height of the button.
      Figure 15 Adding the initialization button
      1. To modify the text on the button, right-click the button and choose Properties from the shortcut menu.
        Figure 16 Button properties
      2. Change the value of Caption to Init.
        Figure 17 Modifying button properties

  6. Add a button click event.

    1. Double-click the Init control. The system automatically adds the OnBnClickedButton1() method to Hello_WorldDlg.cpp for the Init button.
      Figure 18 Creating an event method for the Init button

  7. Add a code file.

    1. Add a callback processing class.
      1. Right-click the project and choose Add > Class from the shortcut menu.
        Figure 19 Adding a class
      2. In the Generic C++ class wizard dialog box, set the class name to callbackProc, retain the default values for other parameters, and click Finish.
        Figure 20 Adding the callbackProc class
    2. Add a notification processing class and set the class name to notifyProc.
      Figure 21 Adding the notifyProc class

  8. Add the logic code.

    1. Add the SDK callback class header file HwmAgentCallback.h to the callbackProc.h file, so the callbackProc class inherits the base class HwmAgentCallback. You can copy only API calling callback functions that need to be focused on in the HwmAgentCallback.h file.
       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
      #pragma once
      #include "HwmAgentCallback.h"
       
      class callbackProc : public hwmsdkagent::HwmAgentCallback
      {
      public:
          callbackProc();
          ~callbackProc();
          /**
               * @brief [en] Callback of Init interface.
               *
               * @param [in] hwmsdk::HwmErrCode ret    [en] Return code
               * @param [in] const char* reason        [en] Fail reason
               * @attention [en] NA
               **/
          void OnInitResult(hwmsdk::HwmErrCode ret, const char* reason);
       
          /**
           * @brief [en] Callback of Exit interface.
           *
           * @param [in] hwmsdk::HwmErrCode ret    [en] Return code
           * @param [in] const char* reason        [en] Fail reason
           * @attention [en] NA
           **/
          void OnExitResult(hwmsdk::HwmErrCode ret, const char* reason);
          
         // To save space, other callback APIs are omitted here. Copy them completely during actual operations.
      }
      

      To save space, some of the preceding code is omitted. You need to implement the complete code based on the SDK header file.

    2. Define APIs in the callbackProc.cpp file.
       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
      31
      32
      33
      34
      #include "stdafx.h"
      #include "callbackProc.h"
      
      callbackProc::callbackProc()
      {
       
      }
      callbackProc::~callbackProc()
      {
       
      }
       
      /**
      * Initialization callback
      */
      void callbackProc::OnInitResult(hwmsdk::HwmErrCode ret, const char* reason)
      {
          CString retStr;
          retStr.Format(_T("%d"), ret);
          CString tips = _T("OnInitResult code:") + retStr;
          AfxMessageBox(tips);
      }
      
      /**
      * Exit callback */
      void callbackProc::OnExitResult(hwmsdk::HwmErrCode ret, const char* reason)
      {
          CString retStr;
          retStr.Format(_T("%d"), ret);
          CString tips = _T("OnExitResult code:") + retStr;
          AfxMessageBox(tips);
      }
      
      // To save space, some code is omitted here. In actual operations, define callback function APIs that you need to pay attention to.
      

      To save space, some of the preceding code is omitted. You need to implement the complete code based on the SDK header file. The header files (stdafx.h or pch.h) used for precompilation in different versions may be different. If a compilation error occurs, use the local default header file.

    3. Add the SDK callback class header file HwmAgentNotify.h to the notifyProc.h file, so the callbackProc class inherits the base class HwmAgentNotify. You can copy only API calling functions that need to be focused on in the HwmAgentNotify.h file.
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      #pragma once
      #include "HwmAgentNotify.h"
       
       
      class notifyProc : public hwmsdkagent::HwmAgentNotify
      {
      public:
          notifyProc();
          ~notifyProc();
       
          /**
           * @brief [en] Notify of sdk disconnected.
           *
           * @attention [en] NA
           **/
          void OnSdkDisconnected();
      
         // To save space, other callback APIs are omitted here. Copy them completely during actual operations.
      }
      

      To save space, some of the preceding code is omitted. You need to implement the complete code based on the SDK header file.

    4. Define APIs in the notifyProc.cpp file.
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      #include "stdafx.h"
      #include "notifyProc.h"
       
      notifyProc::notifyProc()
      {
       
      }
      notifyProc::~notifyProc()
      {
       
      }
       
      // SDK disconnection notification
      void notifyProc::OnSdkDisconnected()
      {
          CString tips = _T("OnSdkDisconnected");
          AfxMessageBox(tips);
      }
      
      // To save space, other callback APIs are omitted here. Copy them completely during actual operations.
      

      To save space, some of the preceding code is omitted. You need to implement the complete code based on the SDK header file. The header files (stdafx.h or pch.h) used for precompilation in different versions may be different. If a compilation error occurs, use the local default header file.

    5. Include the required header files in the Hello_WorldDlg.h file.
      1
      #include "HwmErrorCodeDef.h"
      
    6. Declare functions to be used in the Hello_WorldDlg.h file.
      1
      2
      3
      4
      5
      public:
          // Initialization API
          hwmsdk::HwmErrCode Init();
          // Exit API
          static hwmsdk::HwmErrCode Exit();
      
    7. Include the required header files in the Hello_WorldDlg.cpp file.
      1
      2
      3
      4
      #include "notifyProc.h"
      #include "callBackProc.h"
      #include "HwmSdkAgent.h"
      #include <string>
      
    8. Define the callback and notification objects in the Hello_WorldDlg.cpp file.
      1
      2
      static notifyProc *notifyObj = new notifyProc();
      static callbackProc *callbackObj = new callbackProc();
      
    9. Define the initialization API in the Hello_WorldDlg.cpp file.
       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
      /**
      * Initialization API
      */
      hwmsdk::HwmErrCode CHello_WorldDlg::Init()
      {
          // Assemble the input parameter structure.
          hwmsdkagent::HwmInitInfo initParam;
          memset(&initParam, 0, sizeof(hwmsdkagent::HwmInitInfo));
       
          // Set the SDK path. If the actual path of the HwmSdk folder is different from the following path, use the actual path.
          std::string path = "E:\\Hello_World\\debug\\win32\\HwmSdk\\HwmSdk.exe";
          // Convert the 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());
       
          std::string appId = "Hello_World";
          strncpy_s(initParam.appId, appId.c_str(), HWM_MAX_APPID_LEN);
       
          initParam.notify = notifyObj;
          initParam.callback = callbackObj;
          hwmsdk::HwmErrCode ret = hwmsdkagent::Init(&initParam);
       
          return ret;
      }
      
    10. In the Hello_WorldDlg.cpp file, implement the processing of the event of clicking the Init button.
      1
      2
      3
      4
      5
      6
      7
      8
      9
      void CHello_WorldDlg::OnBnClickedButton1()
      {
          // TODO: Add a control to notify code processing.
          hwmsdk::HwmErrCode ret = Init();
          if (hwmsdk::HWM_COMMON_SUCCESS != ret)
          {
              AfxMessageBox(_T("Init error"));
          }
      }
      

      The preceding code is the same as the project code. Do not add the code repeatedly.

    11. Define the logout API in Hello_WorldDlg.cpp file.
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      /**
      * Exit API
      */
      hwmsdk::HwmErrCode CHello_WorldDlg::Exit()
      {
          hwmsdk::HwmErrCode ret = hwmsdkagent::Exit();
          if (hwmsdk::HWM_COMMON_SUCCESS != ret)
          {
              AfxMessageBox(_T("Exit error"));
          }
          return ret;
      }
      
    12. Find the InitInstance API in the Hello_World.cpp file and call the Exit API when the window exits.
       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
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      // Initialize CHello_WorldApp.
       
      BOOL CHello_WorldApp::InitInstance()
      {
         // If an application running on Windows XP specifies to
         // enable the visualization mode using ComCtl32.dll version 6 or later,
         // InitCommonControlsEx() is required. Otherwise, the window cannot be created.
          INITCOMMONCONTROLSEX InitCtrls;
          InitCtrls.dwSize = sizeof(InitCtrls);
         // Set it to include all the common control classes
         // to be used in the application.
          InitCtrls.dwICC = ICC_WIN95_CLASSES;
          InitCommonControlsEx(&InitCtrls);
       
          CWinApp::InitInstance();
       
       
          AfxEnableControlContainer();
       
         // Create a shell manager to prevent the dialog box from containing
         // any shell tree view control or shell list view control.
          CShellManager *pShellManager = new CShellManager;
       
         // Activate the Windows Native visual manager to enable the theme in the MFC control.
          CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
       
         // Standard initialization
         // If these functions are not used, 
         // and you want to reduce the size of the final executable file, 
         // remove the following specific initialization routines that are not required, 
         // and change the registry key for storing settings.
         // TODO: Modify the character string as required.
          // For example, change the value to the company or organization name.
          SetRegistryKey(_T("Local applications generated by the application wizard"));
       
          CHello_WorldDlg dlg;
          m_pMainWnd = &dlg;
          INT_PTR nResponse = dlg.DoModal();
          if (nResponse == IDOK)
          {
              // TODO: Place code here 
              // to handle when to use OK to close the dialog box.
              CHello_WorldDlg::Exit();
          }
          else if (nResponse == IDCANCEL)
          {
              // TODO: Place code here 
              // to handle when to use Cancel to close the dialog box.
              CHello_WorldDlg::Exit();
       
          }
          else if (nResponse == -1)
          {
              TRACE(traceAppMsg, 0, "Warning: Creating the dialog box failed. The application will terminate unexpectedly.\n");
              TRACE(traceAppMsg, 0, "Warning: If you use the MFC control in the dialog box, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\n");
          }
       
          // Delete the created shell manager.
          if (pShellManager != nullptr)
          {
              delete pShellManager;
          }
       
      #if !defined(_AFXDLL) && !defined(_AFX_NO_MFC_CONTROLS_IN_DIALOGS)
          ControlBarCleanUp();
      #endif
       
          // Because the dialog box is closed, FALSE is returned to exit the application, 
          // instead of starting the message pump of the application.
          return FALSE;
      }
      

      The preceding code is the same as the project code. Do not add the code repeatedly.

  9. Compile and debug the project.

    1. Right-click the project and choose Rebuild from the shortcut menu to compile the project.
      Figure 22 Rebuilding files
    2. Observe the Output window. If the message "Rebuild All: 1 succeeded, 0 failed, 0 skipped" is displayed, the compilation is successful.
      Figure 23 Result
    3. After the compilation is successful, the generated executable program Hello_World.exe is stored in the output directory. The current output directory is ${path}\Hello_World\debug\win32.
      Figure 24 .exe file
    4. Copy the corresponding files in SDK\dll and SDK\HwmSdk to the output directory.

      If the 32-bit Windows platform is used, copy DLL files in SDK\dll\win32 to the output directory, and copy all the files in SDK\HwmSdk to the output directory.

      Figure 25 Copying DLL files in SDK\dll\win32 to the output directory

      Figure 26 Copying all files in SDK\HwmSdk to the output directory

      If the x64 platform is used, copy DLL files in SDK\dll\x64 to the output directory and the entire SDK\HwmSdk folder to the output directory, copy HwmSdk.exe in SDK\dll\x64 to the HwmSdk folder in the output directory.

      Figure 27 Copying DLL files in SDK\dll\x64 to the output directory

      Figure 28 Copying the entire SDK\HwmSdk folder to the output directory

      Figure 29 Copying HwmSdk.exe in SDK\dll\x64 to the HwmSdk folder in the output directory

      When the Win32 platform is used, the directory is SDK\dll\win32. When the x64 platform is used, the directory is SDK\dll\x64. HwmSdk contains common dependency library files.

    5. Double-click Hello_World.exe.
      Figure 30 Startup screen
    6. Click Init. A dialog box is displayed indicating the initialization result. The value 0 indicates that the initialization is successful, and other values indicate that the initialization fails. After the initialization is successful, a notification dialog box is displayed.
      Figure 31 Result prompt
      Figure 32 Startup notification