C#
<p>以下代码示范了初始化,搜索设备,登录设备,设置音量和获取音量API的使用,设备返回信息见:<a href="https://www.showdoc.cc/ViplexCore?page_id=4745574396569976">运行结果</a>
<font color='red'> 注意: </font>如果JSON参数中涉及到中文,请确保调用处是<em>UTF-8</em>编码。</p>
<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Api
{
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public delegate void exportViplexCallback(Int16 code, string data);
class ApiTester
{
[DllImport("viplexcore.dll", EntryPoint = "nvInit", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern int NvInit(string sdkRootDir, int type, int platform);
[DllImport("viplexcore.dll", EntryPoint = "nvSerchTerminalAsync", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern void NvSerchTerminalAsync(exportViplexCallback resultCallback);
[DllImport("viplexcore.dll", EntryPoint = "nvLoginAsync", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern void NvLoginAsync(string data, exportViplexCallback resultCallback);
[DllImport("viplexcore.dll", EntryPoint = "nvSetVolumeAsync", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern void NvSetVolumeAsync(string data, exportViplexCallback resultCallback);
[DllImport("viplexcore.dll", EntryPoint = "nvGetVolumeAsync", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern void NvGetVolumeAsync(string data, exportViplexCallback resultCallback);
static bool g_bAPIReturn = false;
static void testDemoDelegate(Int16 code, string data)
{
string strCode = "ViplexCore Demo code:" + code;
string strData = "ViplexCore Demo data:" + data;
Console.WriteLine(strCode);
Console.WriteLine(strData);
g_bAPIReturn = true;
}
static void waitAPIReturn()
{
while (!g_bAPIReturn)
{
Thread.Sleep(1000);
}
g_bAPIReturn = false;
}
static void testApi()
{
Console.Write("nvInit:");
Console.WriteLine(NvInit("D:/ApiTester/bin", 1, 1));
Console.WriteLine("ViplexCore Demo nvSerchTerminalAsync begin... ");
NvSerchTerminalAsync(testDemoDelegate);
Thread.Sleep(5000);
g_bAPIReturn = false;
Console.WriteLine("ViplexCore Demo nvLoginAsync begin... ");
NvLoginAsync("{\"sn\":\"BZSA07194A0049999716\",\"username\":\"admin\",\"password\":\"123456\",\"loginType\":0,\"rememberPwd\":0}", testDemoDelegate);
waitAPIReturn();
Console.WriteLine("ViplexCore Demo nvSetVolumeAsync begin... ");
NvSetVolumeAsync("{\"sn\":\"BZSA07194A0049999716\",\"volumeInfo\":{\"ratio\":60.0}}", testDemoDelegate);
waitAPIReturn();
Console.WriteLine("ViplexCore Demo nvGetVolumeAsync begin... ");
NvGetVolumeAsync("{\"sn\":\"BZSA07194A0049999716\"}", testDemoDelegate);
waitAPIReturn();
Console.ReadKey();
}
static void Main(string[] args)
{
testApi();
}
}
}
</code></pre>