SDK Initialization
Introduction
Before integrating the MG AdS SDK, the SDK must be initialized first. Only after successful initialization can all the functions of this SDK be used in conjunction with the backend system. SDK initialization should be performed upon entering the game.
SDK Initialization
void InitMgAdSdk(HWND hWnd) {
if (hDLL) return;
hDLL = LoadLibrary(L"MgAdSDKCSharpDLL.dll");
if (hDLL) {
// Register the CMP callback event
if (auto func = (CmpClosedEvent)GetProcAddress(hDLL, "CmpClosedEvent")) //CMP closed
func(onCmpClosedEvent);
// Register the initialization completion callback event
if (auto func = (InitCompleteEvent)GetProcAddress(hDLL, "InitCompleteEvent")) // Callback function after initialization is complete
func(onInitCompleteEvent);
if (auto func = (AdCloseEvent)GetProcAddress(hDLL, "AdCloseEvent")) // Callback function for ad close event
func(onAdCloseEvent);
//1. Set parameters
setAppId(hDLL, YourAppId, YourSecretKey);
//2. First, determine whether CMP needs to be displayed
if (isOpenCmp(hDLL))
{
RECT clientRect;
if (GetClientRect(hWnd, &clientRect)) {
int panelWidth = clientRect.right - clientRect.left;
int panelHeight = clientRect.bottom - clientRect.top;
//2.1. Create CMP container
HINSTANCE minstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
g_hPnlCmp = CreateWindowW(L"STATIC", L"This is a panel for CMP", WS_CHILD | WS_VISIBLE, 0, 0, panelWidth, 50, hWnd, (HMENU)3011, minstance, NULL);
BringWindowToTop(g_hPnlCmp);
//2.2. Display CMP
nlohmann::json json_obj = {
{"handle", reinterpret_cast<int>(g_hPnlCmp)},
{"parentWidth", panelWidth},
{"parentHeight", panelHeight}
};
std::string jsonStr = json_obj.dump();
openCmp(hDLL, jsonStr.c_str());
}
}
//3. SDK initialization
initialize(hDLL);
}
}
void initialize(HINSTANCE hdll) {
try
{
Initialize func = (Initialize)GetProcAddress(hdll, "Initialize");
func(YourAppId, YourSecretKey);
}
catch (const std::exception&)
{
}
}
//Callback function after initialization is complete
void onInitCompleteEvent(char* s) {
try
{
nlohmann::json json_obj = nlohmann::json::parse(s); //{"success":true,"data":""}
bool success = json_obj["success"];
if (success) {
AppendLog(L"Initialization successful");
//Exit screen ad; Step1. After successful initialization, load exit screen ad resources
setupExitAd(hDLL);
//Send a message to the UI thread to call the splash screen ad
PostMessage(g_hwndMain, WM_SHOW_OPENSCREEN_ADVERT, 0, NULL);
}
}
catch (const std::exception&)
{
}
}
void onCmpClosedEvent(char* s) {
try
{
g_cmpChangedWidth = 0;
g_cmpChangedHeight = 0;
//Remove CMP control
PostMessage(g_hwndMain, WM_DESTROY_CMP, 0, NULL);
AppendLog(L"CMP has been removed.");
}
catch (const std::exception&)
{
}
}
Possible errors if initialization fails
● Network failure, no proper network support
● UWP applications do not support VPN, and VPN software is enabled on the local machine
● Incorrect AppId, please check the application settings in the developer backend
● Server issues, please check the error message in the result and contact technical support in a timely manner