Before you begin
Complete these steps before you write code:- Create your product in Partner Center, enable XBOX services, and publish the service configuration to your development sandbox. For more information, see Setting up an app or game at Partner Center, for Managed Partners.
- Set the
TitleIdandMSAAppIdelements in yourMicrosoftGame.configfile. The values must exactly match the Title ID (hexadecimal) and MSA App ID values in Partner Center. - Copy the Service Configuration ID (SCID) from the Identity details section of the Game setup page in Partner Center. You pass the SCID to
XblInitialize. The SCID is case-sensitive. - Set your dev kit or Windows PC to your development sandbox, and sign in with a test account that has access to that sandbox. For more information, see Setting up sandboxes and Troubleshooting Sign-in and Sandbox Errors.
- Include
xsapi-c/services_c.h. To link XSAPI statically, linkMicrosoft.Xbox.Services.143.C.lib(orMicrosoft.Xbox.Services.142.C.libwith the v142 toolset; ARM64 provides only the 143 library), together withlibHttpClient.lib, the import library forlibHttpClient.dll. To use the XSAPI DLL, linkMicrosoft.Xbox.Services.C.Thunks.lib. If more than one DLL in your game calls XSAPI, use the DLL so that all of them share one XSAPI instance. CopylibHttpClient.dll, andMicrosoft.Xbox.Services.C.Thunks.dllif you use the DLL, from the GDKbinfolder into your game’s layout. For the full list of libraries, see GDK directory structure.
Initialize Gaming Runtime Services
XSAPIs depend on Gaming Runtime Services (GRTS). Before calling any XSAPIs, initialize GRTS, shown as follows.Create XTaskQueue (optional)
Most of the XSAPIs are asynchronous APIs and require the use of a task queue. It’s an API for queuing work and for completion task callbacks. To learn more aboutXTaskQueue and different dispatch modes, see Async task queue design.
For example, the following code creates a task queue by using a system thread pool.
nullptr when a queue handle is needed. When nullptr is used, the task system uses ThreadPool by default. It can be overridden by calling XTaskQueueSetCurrentProcessTaskQueue.
Set up HttpClient trace (optional)
To view additional runtime debug information, ensure that you set up the trace functionality ofHttpClient.
The following code sets the HttpClient trace level and enables output for debugger information.
Initialize XSAPI
Initialize XSAPI before calling any XSAPIs.Sign the user in to the XBOX network
Most of the XSAPIs require the user to first sign in to the XBOX network. To sign a user in to the XBOX network, see the code example of the XUserAddAsync API.Create an XBOX services context
An XBOX services context represents the service context that’s associated with a particular user. Most XSAPIs require you to pass in anXblContextHandle that represents the context of the calling user.
To create an XBOX services context, call XblContextCreateHandle and pass in the XUserHandle that you got from the previous step.
Make a service call to XBOX services
Now that you have anXblContextHandle for the user who signed in, you can make service calls to XBOX services.
For example, to retrieve the user’s friends list, you can do the following:
Set the user’s Rich Presence
To show what the player is doing in the game, set the user’s Rich Presence when the player’s activity changes, such as moving from the main menu into a match. For more information, see Updating a user’s Presence via XSAPI C.Clean up XSAPI
You don’t need to callXblCleanupAsync when your game exits or is suspended. The operating system reclaims resources when the process exits, and XSAPI handles releasing and restoring the system resources that it needs across suspend and resume.
Call XblCleanupAsync in these cases:
- You installed custom allocators with
XblMemSetFunctions. CallXblCleanupAsync, and wait for it to finish, before you shut down your memory manager. - You want to release XSAPI resources while the game keeps running, for example before you call
XblInitializeagain.
XblCleanupAsync, wait for your outstanding XSAPI calls to finish, and close your XblContextHandle handles with XblContextCloseHandle.