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 about XTaskQueue and different dispatch modes, see Async task queue design.
For example, the following code creates a task queue by using a system thread pool.
Ensure that you release your acquired task queue handle back to the system when it's no longer needed.
C++
XTaskQueueCloseHandle(queue);
queue = nullptr;
If you choose not to create your own task queue, ensure that you pass in 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 of HttpClient.
The following code sets the HttpClient trace level and enables output for debugger information.
#include<xsapi-c/services-c.h>
...
XblInitArgs xblArgs = {};
xblArgs.queue = queue; // TODO: Only include this line if you've chosen to create your own XTaskQueue. Otherwise, by default, this line isn't needed.
xblArgs.scid = "00000000-0000-0000-0000-000000000000"; // TODO: Add your scid here.
HRESULT hr = XblInitialize(&xblArgs);
if (FAILED(hr))
{
// TODO: Handle failure.
}
Sign the user in to the Xbox network
Most of the XSAPIs require the user to first sign in to the Xbox network (also known as Xbox Live).
To sign a user in to the Xbox network, see the code example of the XUserAddAsync API.
Create an XboxLiveContext object
An XboxLiveContext object represents the service context that is associated to a particular user.
Most XSAPIs require you to pass in an XboxLiveContextHandle that represents the context of the calling user.
To create an Xbox Live (network) context, use the XblContextCreateHandle API and pass in the XUserHandle object that was acquired from the previous step.
Make a service call to Xbox services
Now that you have an XboxLiveContext object associated with the XUserHandle object for the user who has signed in, you can make service calls to Xbox services.
For example, to retrieve the user's friends list, you can do the following:
C++
#include<xsapi-c/services-c.h>
...
auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of the XAsyncBlock*.
XblSocialRelationshipResultHandle relationshipResult{ nullptr };
HRESULT hr = XblSocialGetSocialRelationshipsResult(asyncBlock, &state.socialResultHandle);
// Use the result in the game.
XblSocialRelationshipResultCloseHandle(relationshipResult);
};
HRESULT hr = XblSocialGetSocialRelationshipsAsync(
xboxLiveContext,
xboxUserId,
socialRelationshipFilter,
0,
0,
asyncBlock.get()
);
if (SUCCEEDED(hr))
{
// The call succeeded. Release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.// If the call fails, the std::unique_ptr will keep ownership and delete the XAsyncBlock*.
asyncBlock.release();
}
// End of code example.
Cleaning up XSAPI
It is not neccessary to call XblCleanupAsync() in any scenario.
In an app termination scenario, there is currently no synchronous XblCleanup() API that you can call. App termination, by its life-cycle nature, needs to be handled synchronously and immediately. As a result, normal OS-level cleanup—that happens with process termination of the app—is relied upon and is sufficient for this situation.
In a scenario where the app is suspended, XSAPI handles releasing and then restoring the system resources required for operation in order to maintain functionality after resume.
XblCleanupAsync() can still be used in cases where your game chooses to deliberately release the resources allocated to XSAPI.
Learn about foundational accessibility best practice principles that you can use to guide the development of games and platforms. Also learn how to use the Xbox Accessibility Guidelines, a free online resource for developers that provides a comprehensive list of best practices organized by specific game elements and features.
Services for managing games and player information. Use the **Xbox.Services** API (XSAPI)whenever possible. Use the RESTful interface for newer methods that have not been added to **Xbox.Services** yet.
Creating a new app or game at Partner Center as a Managed Partner, enabling Xbox services for the game, and publishing the game to a development sandbox.