补充C#测试用例
This commit is contained in:
8
MesApi/CSharpDemo/CSharpDemo.csproj
Normal file
8
MesApi/CSharpDemo/CSharpDemo.csproj
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net48</TargetFramework>
|
||||||
|
<LangVersion>12.0</LangVersion>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
</Project>
|
||||||
132
MesApi/CSharpDemo/Program.cs
Normal file
132
MesApi/CSharpDemo/Program.cs
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
using System;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
internal static class NativeMethods
|
||||||
|
{
|
||||||
|
[DllImport("MesApi.dll", EntryPoint = "UploadResult", CallingConvention = CallingConvention.Winapi, ExactSpelling = true)]
|
||||||
|
private static extern int UploadResultNative(
|
||||||
|
IntPtr sn,
|
||||||
|
IntPtr station,
|
||||||
|
IntPtr function,
|
||||||
|
IntPtr result,
|
||||||
|
IntPtr costtime,
|
||||||
|
IntPtr outBuffer,
|
||||||
|
int bufferSize);
|
||||||
|
|
||||||
|
internal static int UploadResult(
|
||||||
|
string sn,
|
||||||
|
string station,
|
||||||
|
string function,
|
||||||
|
string result,
|
||||||
|
string costtime,
|
||||||
|
byte[] outBuffer,
|
||||||
|
int bufferSize)
|
||||||
|
{
|
||||||
|
IntPtr snPtr = IntPtr.Zero;
|
||||||
|
IntPtr stationPtr = IntPtr.Zero;
|
||||||
|
IntPtr functionPtr = IntPtr.Zero;
|
||||||
|
IntPtr resultPtr = IntPtr.Zero;
|
||||||
|
IntPtr costtimePtr = IntPtr.Zero;
|
||||||
|
IntPtr outBufferPtr = IntPtr.Zero;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
snPtr = StringToUtf8HGlobal(sn);
|
||||||
|
stationPtr = StringToUtf8HGlobal(station);
|
||||||
|
functionPtr = StringToUtf8HGlobal(function);
|
||||||
|
resultPtr = StringToUtf8HGlobal(result);
|
||||||
|
costtimePtr = StringToUtf8HGlobal(costtime);
|
||||||
|
outBufferPtr = Marshal.AllocHGlobal(bufferSize);
|
||||||
|
Marshal.Copy(new byte[bufferSize], 0, outBufferPtr, bufferSize);
|
||||||
|
|
||||||
|
int ret = UploadResultNative(
|
||||||
|
snPtr,
|
||||||
|
stationPtr,
|
||||||
|
functionPtr,
|
||||||
|
resultPtr,
|
||||||
|
costtimePtr,
|
||||||
|
outBufferPtr,
|
||||||
|
bufferSize);
|
||||||
|
|
||||||
|
Marshal.Copy(outBufferPtr, outBuffer, 0, bufferSize);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
FreeHGlobal(snPtr);
|
||||||
|
FreeHGlobal(stationPtr);
|
||||||
|
FreeHGlobal(functionPtr);
|
||||||
|
FreeHGlobal(resultPtr);
|
||||||
|
FreeHGlobal(costtimePtr);
|
||||||
|
FreeHGlobal(outBufferPtr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IntPtr StringToUtf8HGlobal(string value)
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
{
|
||||||
|
return IntPtr.Zero;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] bytes = Encoding.UTF8.GetBytes(value + "\0");
|
||||||
|
IntPtr ptr = Marshal.AllocHGlobal(bytes.Length);
|
||||||
|
Marshal.Copy(bytes, 0, ptr, bytes.Length);
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void FreeHGlobal(IntPtr ptr)
|
||||||
|
{
|
||||||
|
if (ptr != IntPtr.Zero)
|
||||||
|
{
|
||||||
|
Marshal.FreeHGlobal(ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
private static void Main()
|
||||||
|
{
|
||||||
|
Console.OutputEncoding = Encoding.UTF8;
|
||||||
|
Console.WriteLine("========================================");
|
||||||
|
Console.WriteLine(" MesApi DLL C# 调用示例");
|
||||||
|
Console.WriteLine("========================================");
|
||||||
|
|
||||||
|
var buffer = new byte[4096];
|
||||||
|
int ret = NativeMethods.UploadResult(
|
||||||
|
"SN20260402001",
|
||||||
|
"STA_Touch",
|
||||||
|
"FC_TouchScreen",
|
||||||
|
"NG",
|
||||||
|
"13.22",
|
||||||
|
buffer,
|
||||||
|
buffer.Length);
|
||||||
|
|
||||||
|
var messageLength = Array.IndexOf(buffer, (byte)0);
|
||||||
|
if (messageLength < 0)
|
||||||
|
{
|
||||||
|
messageLength = buffer.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
var message = Encoding.UTF8.GetString(buffer, 0, messageLength);
|
||||||
|
|
||||||
|
Console.WriteLine($"返回码: {ret}");
|
||||||
|
Console.WriteLine($"返回值: {message}");
|
||||||
|
Console.WriteLine($"结果说明: {GetResultText(ret)}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetResultText(int ret)
|
||||||
|
{
|
||||||
|
return ret switch
|
||||||
|
{
|
||||||
|
0 => "上传成功",
|
||||||
|
-1 => "网络错误",
|
||||||
|
-2 => "MES 返回失败",
|
||||||
|
-3 => "缓冲区不足",
|
||||||
|
-4 => "未知异常",
|
||||||
|
_ => "未定义返回码"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,30 +7,54 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MesApi", "MesApi\MesApi.vcx
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Test", "Test\Test.vcxproj", "{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}"
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Test", "Test\Test.vcxproj", "{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpDemo", "CSharpDemo\CSharpDemo.csproj", "{FB06BBC5-68D4-94C9-D147-7E4EB7BA9FD9}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
Debug|x64 = Debug|x64
|
Debug|x64 = Debug|x64
|
||||||
Debug|x86 = Debug|x86
|
Debug|x86 = Debug|x86
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
Release|x64 = Release|x64
|
Release|x64 = Release|x64
|
||||||
Release|x86 = Release|x86
|
Release|x86 = Release|x86
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Debug|Any CPU.ActiveCfg = Debug|x64
|
||||||
|
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Debug|Any CPU.Build.0 = Debug|x64
|
||||||
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Debug|x64.ActiveCfg = Debug|x64
|
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Debug|x64.Build.0 = Debug|x64
|
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Debug|x64.Build.0 = Debug|x64
|
||||||
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Debug|x86.ActiveCfg = Debug|Win32
|
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Debug|x86.ActiveCfg = Debug|Win32
|
||||||
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Debug|x86.Build.0 = Debug|Win32
|
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Debug|x86.Build.0 = Debug|Win32
|
||||||
|
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Release|Any CPU.ActiveCfg = Release|x64
|
||||||
|
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Release|Any CPU.Build.0 = Release|x64
|
||||||
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Release|x64.ActiveCfg = Release|x64
|
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Release|x64.ActiveCfg = Release|x64
|
||||||
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Release|x64.Build.0 = Release|x64
|
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Release|x64.Build.0 = Release|x64
|
||||||
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Release|x86.ActiveCfg = Release|Win32
|
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Release|x86.ActiveCfg = Release|Win32
|
||||||
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Release|x86.Build.0 = Release|Win32
|
{04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Release|x86.Build.0 = Release|Win32
|
||||||
|
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Debug|Any CPU.ActiveCfg = Debug|x64
|
||||||
|
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Debug|Any CPU.Build.0 = Debug|x64
|
||||||
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Debug|x64.ActiveCfg = Debug|x64
|
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Debug|x64.Build.0 = Debug|x64
|
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Debug|x64.Build.0 = Debug|x64
|
||||||
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Debug|x86.ActiveCfg = Debug|Win32
|
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Debug|x86.ActiveCfg = Debug|Win32
|
||||||
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Debug|x86.Build.0 = Debug|Win32
|
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Debug|x86.Build.0 = Debug|Win32
|
||||||
|
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Release|Any CPU.ActiveCfg = Release|x64
|
||||||
|
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Release|Any CPU.Build.0 = Release|x64
|
||||||
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Release|x64.ActiveCfg = Release|x64
|
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Release|x64.ActiveCfg = Release|x64
|
||||||
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Release|x64.Build.0 = Release|x64
|
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Release|x64.Build.0 = Release|x64
|
||||||
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Release|x86.ActiveCfg = Release|Win32
|
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Release|x86.ActiveCfg = Release|Win32
|
||||||
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Release|x86.Build.0 = Release|Win32
|
{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Release|x86.Build.0 = Release|Win32
|
||||||
|
{FB06BBC5-68D4-94C9-D147-7E4EB7BA9FD9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{FB06BBC5-68D4-94C9-D147-7E4EB7BA9FD9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{FB06BBC5-68D4-94C9-D147-7E4EB7BA9FD9}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{FB06BBC5-68D4-94C9-D147-7E4EB7BA9FD9}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{FB06BBC5-68D4-94C9-D147-7E4EB7BA9FD9}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{FB06BBC5-68D4-94C9-D147-7E4EB7BA9FD9}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{FB06BBC5-68D4-94C9-D147-7E4EB7BA9FD9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{FB06BBC5-68D4-94C9-D147-7E4EB7BA9FD9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{FB06BBC5-68D4-94C9-D147-7E4EB7BA9FD9}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{FB06BBC5-68D4-94C9-D147-7E4EB7BA9FD9}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{FB06BBC5-68D4-94C9-D147-7E4EB7BA9FD9}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{FB06BBC5-68D4-94C9-D147-7E4EB7BA9FD9}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -45,14 +45,14 @@ void TestDynamicLoad()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::wcout << L"[OK] 函数地址获取成功" << std::endl;
|
std::wcout << L"[OK] 函数地址获取成功" << std::endl;
|
||||||
|
|
||||||
char buffer[4096] = {};
|
char buffer[4096] = {};
|
||||||
int ret = pfnUpload(
|
int ret = pfnUpload(
|
||||||
"SN20260402001",
|
"SN20260402001",
|
||||||
"STA_STA_Touch",
|
"STA_STA_Touch",
|
||||||
"FC_TouchScreen",
|
"FC_TouchScreen",
|
||||||
"OK",//这里要写测试结果OK或者NG
|
"Fail",//这里要写测试结果OK或者NG
|
||||||
"13.5",
|
"13.22",
|
||||||
buffer,
|
buffer,
|
||||||
sizeof(buffer)
|
sizeof(buffer)
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user