C/C++_windows
<p>以下代码示范了初始化,搜索设备,登录设备,设置音量和获取音量API的使用,设备返回信息见:<a href="https://www.showdoc.cc/ViplexCore?page_id=4745574396569976">运行结果</a>
<font color='red'> 注意: </font><strong>如果JSON参数中涉及到中文,请确保调用处是<em>UTF-8</em>编码,如:</strong>
<code>#pragma execution_character_set("utf-8")</code></p>
<pre><code>#include <iostream>
#include <Windows.h>
#include <functional>
#include <atomic>
#include <thread>
#include <chrono>
#include <string>
#pragma execution_character_set("utf-8")
using namespace std;
typedef void(*ExportViplexCallback)(const uint16_t, const char *);
//#define LOADING_FUNCTION_WITH_DYNAMIC
#ifndef LOADING_FUNCTION_WITH_DYNAMIC
#include "exportviplexcoreasync.h"
#pragma comment(lib, "viplexcore.lib")
#else
typedef int(*nvInitFunc)(const char*, const int, const int);
typedef void(*nvSerchTerminalAsyncFunc)(ExportViplexCallback);
typedef void(*nvLoginAsyncFunc)(const char*, ExportViplexCallback);
typedef void(*nvSetVolumeAsyncFunc)(const char*, ExportViplexCallback);
typedef void(*nvGetVolumeAsyncFunc)(const char*, ExportViplexCallback);
nvInitFunc nvInit;
nvSerchTerminalAsyncFunc nvSerchTerminalAsync;
nvLoginAsyncFunc nvLoginAsync;
nvSetVolumeAsyncFunc nvSetVolumeAsync;
nvGetVolumeAsyncFunc nvGetVolumeAsync;
void loadingFunctionWithDynamic()
{
DWORD dwError = 0;
HMODULE hDll = LoadLibraryExA("viplexcore.dll", NULL, LOAD_WITH_ALTERED_SEARCH_PATH);//加载dll
if (NULL == hDll)
{
dwError = GetLastError();
std::string msg = std::string("ViplexCore Demo 加载dll文件失败:") + to_string(dwError);
OutputDebugStringA(msg.c_str());
}
nvInit = (nvInitFunc)(GetProcAddress(hDll, "nvInit"));//加载函数
if (NULL == nvInit)
{
std::string msg = std::string("ViplexCore Demo 函数nvInit加载失败");
OutputDebugStringA(msg.c_str());
}
nvSerchTerminalAsync = (nvSerchTerminalAsyncFunc)(GetProcAddress(hDll, "nvSerchTerminalAsync"));
if (NULL == nvSerchTerminalAsync)
{
std::string msg = std::string("ViplexCore Demo 函数nvInit加载失败");
OutputDebugStringA(msg.c_str());
}
nvLoginAsync = (nvLoginAsyncFunc)(GetProcAddress(hDll, "nvLoginAsync"));
if (NULL == nvLoginAsync)
{
std::string msg = std::string("ViplexCore Demo 函数nvInit加载失败");
OutputDebugStringA(msg.c_str());
}
nvSetVolumeAsync = (nvSetVolumeAsyncFunc)(GetProcAddress(hDll, "nvSetVolumeAsync"));
if (NULL == nvSetVolumeAsync)
{
std::string msg = std::string("ViplexCore Demo 函数nvInit加载失败");
OutputDebugStringA(msg.c_str());
}
nvGetVolumeAsync = (nvGetVolumeAsyncFunc)(GetProcAddress(hDll, "nvGetVolumeAsync"));
if (NULL == nvSetVolumeAsync)
{
cout << "ViplexCore Demo 函数nvGetVolumeAsync加载失败" << endl;
}
}
#endif
atomic_bool g_bAPIReturn = false;
ExportViplexCallback callback = [](const uint16_t code, const char *data)
{
std::string msgCode = std::string("ViplexCore Demo code:") + to_string(code);
std::string msgData = std::string("ViplexCore Demo data:") + data;
OutputDebugStringA(msgCode.c_str());
OutputDebugStringA(msgData.c_str());
g_bAPIReturn = true;
};
void waitAPIReturn()
{
while (!g_bAPIReturn)
{
this_thread::sleep_for(chrono::seconds(1));
}
g_bAPIReturn = false;
}
void APITester()
{
cout << "nvInit:" << nvInit("D:\\test", 1, 1) << endl;
OutputDebugStringA("ViplexCore Demo nvSerchTerminalAsync begin... ");
nvSerchTerminalAsync(callback);
this_thread::sleep_for(chrono::seconds(5));
g_bAPIReturn = false;
OutputDebugStringA("ViplexCore Demo nvLoginAsync begin... ");
nvLoginAsync("{\"sn\":\"BZSA07194A0049999716\",\"username\":\"admin\",\"password\":\"123456\",\"loginType\":0,\"rememberPwd\":0}", callback);
waitAPIReturn();
OutputDebugStringA("ViplexCore Demo nvSetVolumeAsync begin... ");
nvSetVolumeAsync("{\"sn\":\"BZSA07194A0049999716\",\"volumeInfo\":{\"ratio\":60.0}}", callback);
waitAPIReturn();
OutputDebugStringA("ViplexCore Demo nvGetVolumeAsync begin... ");
nvGetVolumeAsync("{\"sn\":\"BZSA07194A0049999716\"}", callback);
waitAPIReturn();
}
int main()
{
#ifdef LOADING_FUNCTION_WITH_DYNAMIC
loadingFunctionWithDynamic();
#endif
APITester();
return 0;
}</code></pre>