> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# クイックスタート (Windows) - Core SDK セットアップ

> PlayFab 統合 SDK を Windows プロジェクトに追加し、Core を初期化し、XBOX または Steam でプレイヤーを認証して PFEntityHandle を取得します。

このガイドでは、PlayFab 統合 SDK を Windows プロジェクトに統合し、SDK を初期化して、プレイヤーを認証する方法を説明します。これは PlayFab API 呼び出しの基盤となります。

## 前提条件

開始する前に、[クイックスタート セットアップ](/services/playfab/sdks/unified-sdk/quickstart-setup) のセットアップ手順を完了して、開発環境とプロジェクトを構成してください。

## 達成できること

このクイックスタートの終わりまでに、次のことを完了できます:

* Windows プロジェクトでの PlayFab 統合 SDK のセットアップ
* タイトル ID を使用した SDK の初期化
* XBOX または Steam を使用したプレイヤーの認証
* API 呼び出しを行うためのエンティティ ハンドルの取得
* リソースの適切なクリーンアップ

## このクイックスタートで扱う内容

1. [SDK を初期化する](#step-1-initialize-the-sdk)
2. [プレイヤーを認証する](#step-2-authenticate-a-player)
3. [リソースをクリーンアップする](#step-3-clean-up-resources)

## ステップ 1: SDK を初期化する

### 必要なヘッダーをインクルードする

まず、プロジェクトに必要な PlayFab および XBOX Game Runtime ヘッダーをインクルードします。

```cpp theme={null}
#include <playfab/core/PFCore.h>
#include <XGameRuntimeInit.h>
```

### XBOX Gaming Runtime Services を初期化する

PlayFab 統合 SDK には XBOX Game Runtime が必要です。最初にこれを初期化します。

```cpp theme={null}
HRESULT hr = XGameRuntimeInitialize();
if (FAILED(hr))
{
    std::wcerr << L"Failed to initialize Xbox Game Runtime: 0x" << std::hex << hr << std::endl;
    return hr;
}
std::wcout << L"Xbox Game Runtime initialized successfully." << std::endl;
```

### PlayFab Core を初期化する

PlayFab SDK を初期化し、サービス構成ハンドルを作成します。`ABCDEF` を実際の PlayFab タイトル ID に置き換えてください。

```cpp theme={null}
// Initialize PlayFab
HRESULT hr = PFInitialize(nullptr);
if (FAILED(hr))
{
    std::wcerr << L"Failed to initialize PlayFab: 0x" << std::hex << hr << std::endl;
    return hr;
}

// Create service configuration handle
PFServiceConfigHandle serviceConfigHandle{ nullptr };
hr = PFServiceConfigCreateHandle(
    "https://ABCDEF.playfabapi.com", // Replace ABCDEF with your Title ID
    "ABCDEF",                        // Replace ABCDEF with your Title ID
    &serviceConfigHandle);
if (FAILED(hr))
{
    std::wcerr << L"Failed to create service config handle: 0x" << std::hex << hr << std::endl;
    return hr;
}
std::wcout << L"PlayFab initialized successfully." << std::endl;
```

<Tip>
  タイトル ID は、ゲームの設定の下の [Game Manager ダッシュボード](https://developer.playfab.com) で確認できます。
</Tip>

## ステップ 2: プレイヤーを認証する

PlayFab では API 呼び出しを行う前にプレイヤーの認証が必要です。SDK は、プラットフォームと要件に応じたいくつかの認証方法を提供します。

### 認証方法を選択する

#### オプション A: XBOX でログイン (Windows/XBOX に推奨)

`PFAuthenticationLoginWithXUserAsync` を使用して XBOX ユーザー アカウントでログインします。これは Windows および XBOX アプリケーションに推奨される方法です。

```cpp theme={null}
// Assume you have obtained an XUserHandle (userHandle) through XUser APIs
// For details on obtaining XUserHandle, see the Xbox Game Development Kit documentation

PFAuthenticationLoginWithXUserRequest request{};
request.createAccount = true;  // Create account if it doesn't exist
request.user = userHandle;     // XUserHandle obtained from Xbox user APIs

XAsyncBlock async{};
HRESULT hr = PFAuthenticationLoginWithXUserAsync(serviceConfigHandle, &request, &async);
if (FAILED(hr))
{
    std::wcerr << L"Failed to start Xbox login: 0x" << std::hex << hr << std::endl;
    return hr;
}

// Wait for login to complete
hr = XAsyncGetStatus(&async, true); // Blocking wait
if (FAILED(hr))
{
    std::wcerr << L"Xbox login failed: 0x" << std::hex << hr << std::endl;
    return hr;
}

// Get the result
std::vector<char> loginResultBuffer;
PFAuthenticationLoginResult const* loginResult;
size_t bufferSize;
hr = PFAuthenticationLoginWithXUserGetResultSize(&async, &bufferSize);
if (SUCCEEDED(hr))
{
    loginResultBuffer.resize(bufferSize);
    PFEntityHandle entityHandle{ nullptr };
    hr = PFAuthenticationLoginWithXUserGetResult(&async, &entityHandle, 
        loginResultBuffer.size(), loginResultBuffer.data(), &loginResult, nullptr);
    
    if (SUCCEEDED(hr))
    {
        std::wcout << L"Successfully logged in with Xbox. Player ID: " 
                   << loginResult->playFabId << std::endl;
        // entityHandle is now ready for making API calls
    }
}
```

#### オプション B: Steam でログイン

Steam 統合ゲームの場合、Steam 認証チケットを使用して `PFAuthenticationLoginWithSteamAsync` を使用します。

```cpp theme={null}
// Assume you have obtained a Steam ticket through Steam APIs
PFAuthenticationLoginWithSteamRequest request{};
request.createAccount = true;         // Create account if it doesn't exist
request.steamTicket = steamTicket;    // Steam authentication ticket

XAsyncBlock async{};
HRESULT hr = PFAuthenticationLoginWithSteamAsync(serviceConfigHandle, &request, &async);
if (FAILED(hr))
{
    std::wcerr << L"Failed to start Steam login: 0x" << std::hex << hr << std::endl;
    return hr;
}

// Wait for login to complete
hr = XAsyncGetStatus(&async, true);
if (FAILED(hr))
{
    std::wcerr << L"Steam login failed: 0x" << std::hex << hr << std::endl;
    return hr;
}

// Get the result
std::vector<char> loginResultBuffer;
PFAuthenticationLoginResult const* loginResult;
size_t bufferSize;
hr = PFAuthenticationLoginWithSteamGetResultSize(&async, &bufferSize);
if (SUCCEEDED(hr))
{
    loginResultBuffer.resize(bufferSize);
    PFEntityHandle entityHandle{ nullptr };
    hr = PFAuthenticationLoginWithSteamGetResult(&async, &entityHandle, 
        loginResultBuffer.size(), loginResultBuffer.data(), &loginResult, nullptr);
    
    if (SUCCEEDED(hr))
    {
        std::wcout << L"Successfully logged in with Steam. Player ID: " 
                   << loginResult->playFabId << std::endl;
        // entityHandle is now ready for making API calls
    }
}
```

<Note>
  ログインから返される `PFEntityHandle` は、以降のすべての PlayFab API 呼び出しに必要です。このハンドルはアプリケーションの存続期間中利用可能に保ってください。
</Note>

## ステップ 3: リソースをクリーンアップする

アプリケーションのシャットダウン時には、メモリ リークを防ぎ、正常な終了を保証するために、すべてのリソースを適切にクリーンアップしてください。

```cpp theme={null}
// Clean up in reverse order of initialization

// 1. Close the entity handle
if (entityHandle)
{
    PFEntityCloseHandle(entityHandle);
    entityHandle = nullptr;
    std::wcout << L"Entity handle closed." << std::endl;
}

// 2. Close the service config handle
if (serviceConfigHandle)
{
    PFServiceConfigCloseHandle(serviceConfigHandle);
    serviceConfigHandle = nullptr;
    std::wcout << L"Service config handle closed." << std::endl;
}

// 3. Uninitialize PlayFab
XAsyncBlock async{};
HRESULT hr = PFUninitializeAsync(&async);
if (SUCCEEDED(hr))
{
    hr = XAsyncGetStatus(&async, true); // Wait for completion
    if (SUCCEEDED(hr))
    {
        std::wcout << L"PlayFab uninitialized successfully." << std::endl;
    }
    else
    {
        std::wcerr << L"PlayFab uninitialization failed: 0x" << std::hex << hr << std::endl;
    }
}

std::wcout << L"Cleanup complete." << std::endl;
```

<Info>
  適切なシャットダウンを保証するため、常に初期化とは逆の順序でリソースをクリーンアップしてください。
</Info>

## 次のステップ

SDK を初期化してプレイヤーを認証したので、PlayFab サービスの呼び出しを開始する準備が整いました。

### 最初の API 呼び出しを行う

* **[PlayFab サービスの呼び出し](/services/playfab/sdks/unified-sdk/quickstart-services)** - プレイヤー データを管理し、ゲーム サービスを使用するために PlayFab API を呼び出す方法を学習します

### コア概念

* [非同期操作](/services/playfab/sdks/unified-sdk/async-model) - PlayFab の非同期プログラミング モデルの理解
* [メモリ管理](/services/playfab/sdks/unified-sdk/memory-management) - SDK メモリを管理するためのベスト プラクティス
* [トレースと診断](/services/playfab/sdks/unified-sdk/debug-trace) - 統合のデバッグと監視

### 高度な認証

* **マルチプラットフォーム認証** - 複数の認証プロバイダーをサポート
* **カスタム ID 認証** - 独自の認証システムを使用
* **アカウント リンク** - 複数の認証方法を 1 つのアカウントにリンク

### トラブルシューティング

**一般的な問題と解決策:**

| 問題            | 解決策                                                        |
| ------------- | ---------------------------------------------------------- |
| リンク エラー       | 必要なすべての `.lib` ファイルが Additional Dependencies に追加されていることを確認 |
| ランタイム DLL エラー | 必要な `.dll` ファイルを出力ディレクトリにコピー                               |
| 認証の失敗         | タイトル ID を確認し、ネットワーク接続を確認                                   |
| API 呼び出しの失敗   | トレースを有効にして詳細なエラー情報を確認                                      |

## リファレンス ドキュメント

* [PlayFab 統合 SDK API リファレンス](/services/playfab/api-references/c/pfauthentication/pfauthentication_members)
* [PlayFab Services API リファレンス](/services/playfab/api-references)
* [XBOX Game Development Kit ドキュメント](https://docs.microsoft.com/gaming/gdk/)


## Related topics

- [PlayFab Unified SDK クイックスタートセットアップ](/ja-jp/services/playfab/sdks/unified-sdk/quickstart-setup.md)
- [Windows 向け C++ クイックスタート](/ja-jp/services/playfab/sdks/playfab-cpp/quickstart-windows.md)
- [Lobby SDK クイックスタート](/ja-jp/services/playfab/multiplayer/lobby/lobby-getting-started.md)
- [クイックスタート (Windows) - Party とマルチプレイヤー](/ja-jp/services/playfab/sdks/unified-sdk/quickstart-windows-party.md)
- [クイックスタート (Windows) - PlayFab サービスの呼び出し](/ja-jp/services/playfab/sdks/unified-sdk/quickstart-services.md)
