CLR编写
C#调用
结果崩了,这个不晓得怎么测试,单从代码层面能看出错误不呢?
调试的时候要选中这两项:
您在c#端声明一个string类型的实例传递给Begin函数试试。
Answer Verified By: aoteman
P/Invoke调用就好了,没必要用clr方式,这种封装好的函数又不需要调试的。
public static class Class1 { [DllImport("ustation.dll", CharSet = CharSet.Unicode)] public extern static IntPtr mdlDialog_completionBarOpen(string messageText); [DllImport("ustation.dll", CharSet = CharSet.Unicode)] public extern static void mdlDialog_completionBarUpdate(IntPtr dialog, string messageText, int percent); [DllImport("ustation.dll", CharSet = CharSet.Unicode)] public extern static void mdlDialog_completionBarDisplayMessage(IntPtr dialog, string messageText); [DllImport("ustation.dll", CharSet = CharSet.Unicode)] public extern static void mdlDialog_completionBarClose(IntPtr dialog); public static void Test(string unparsed) { var ptr = mdlDialog_completionBarOpen("测试"); System.Threading.Thread.Sleep(1000); //下面这一行,测试了下,感觉用不上 //mdlDialog_completionBarDisplayMessage(ptr, "测试2"); for (int i = 0; i <= 10; i++) { mdlDialog_completionBarUpdate(ptr, $"更新{i}", i * 10); System.Threading.Thread.Sleep(200); } System.Threading.Thread.Sleep(1000); mdlDialog_completionBarClose(ptr); return; } }
试了下clr,貌似挺正常的
#pragma once #include <Mstn/MdlApi/msdialog.fdf> #include <vcclr.h> namespace Test { public ref class ProcessBarHelper { public: ProcessBarHelper() { dialog = NULL; } void OpenBar(System::String^ message) { pin_ptr<const wchar_t> msg = PtrToStringChars(message); if (dialog == NULL) dialog = mdlDialog_completionBarOpen(msg); } void CloseBar() { mdlDialog_completionBarClose(dialog); } void UpdateBar(System::String^ message, int percent) { pin_ptr<const wchar_t> msg = PtrToStringChars(message); mdlDialog_completionBarUpdate(dialog, msg, percent); } ~ProcessBarHelper() { dialog = NULL; } private: MSDialogP dialog; }; } //ProcessBarHelper.cpp #include "ProcessBarHelper.h" //C# Test.ProcessBarHelper processBarHelper = new Test.ProcessBarHelper(); processBarHelper.OpenBar("1"); System.Threading.Thread.Sleep(1000); for (int i = 1; i <= 100; i++) { processBarHelper.UpdateBar( $"更新{i}", i ); System.Threading.Thread.Sleep(200); } processBarHelper.CloseBar();