From 7f70476f0edc60216a94e6abc797f01d15795bd3 Mon Sep 17 00:00:00 2001 From: chenjiangqun Date: Tue, 26 May 2026 15:58:45 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85C#=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MesApi/CSharpDemo/CSharpDemo.csproj | 8 ++ MesApi/CSharpDemo/Program.cs | 132 ++++++++++++++++++++++++++++ MesApi/MesApi.sln | 24 +++++ MesApi/Test/Test.cpp | 6 +- 4 files changed, 167 insertions(+), 3 deletions(-) create mode 100644 MesApi/CSharpDemo/CSharpDemo.csproj create mode 100644 MesApi/CSharpDemo/Program.cs diff --git a/MesApi/CSharpDemo/CSharpDemo.csproj b/MesApi/CSharpDemo/CSharpDemo.csproj new file mode 100644 index 0000000..676aa0b --- /dev/null +++ b/MesApi/CSharpDemo/CSharpDemo.csproj @@ -0,0 +1,8 @@ + + + Exe + net48 + 12.0 + enable + + \ No newline at end of file diff --git a/MesApi/CSharpDemo/Program.cs b/MesApi/CSharpDemo/Program.cs new file mode 100644 index 0000000..bf4d900 --- /dev/null +++ b/MesApi/CSharpDemo/Program.cs @@ -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 => "未知异常", + _ => "未定义返回码" + }; + } +} diff --git a/MesApi/MesApi.sln b/MesApi/MesApi.sln index 3e372e3..1adc453 100644 --- a/MesApi/MesApi.sln +++ b/MesApi/MesApi.sln @@ -7,30 +7,54 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MesApi", "MesApi\MesApi.vcx EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Test", "Test\Test.vcxproj", "{5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpDemo", "CSharpDemo\CSharpDemo.csproj", "{FB06BBC5-68D4-94C9-D147-7E4EB7BA9FD9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU Debug|x64 = Debug|x64 Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU Release|x64 = Release|x64 Release|x86 = Release|x86 EndGlobalSection 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.Build.0 = Debug|x64 {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}.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.Build.0 = Release|x64 {04F8460A-E9A1-43E4-B5CF-C1E30D24631D}.Release|x86.ActiveCfg = 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.Build.0 = Debug|x64 {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}.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.Build.0 = Release|x64 {5CA99838-CB5D-4C18-8CD6-BCABAE89A2BD}.Release|x86.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/MesApi/Test/Test.cpp b/MesApi/Test/Test.cpp index 998bbb7..d91166b 100644 --- a/MesApi/Test/Test.cpp +++ b/MesApi/Test/Test.cpp @@ -45,14 +45,14 @@ void TestDynamicLoad() return; } std::wcout << L"[OK] 鍑芥暟鍦板潃鑾峰彇鎴愬姛" << std::endl; - + char buffer[4096] = {}; int ret = pfnUpload( "SN20260402001", "STA_STA_Touch", "FC_TouchScreen", - "OK",//杩欓噷瑕佸啓娴嬭瘯缁撴灉OK鎴栬匩G - "13.5", + "Fail",//杩欓噷瑕佸啓娴嬭瘯缁撴灉OK鎴栬匩G + "13.22", buffer, sizeof(buffer) );