C#调用C++的dll,换台电脑运行失败
使用.net framework 4.6.1 编写一个WPF项目,项目中引用了C++的dll动态链接库。
C++是在VS2017编写,WinSDK 是:10.0.17763.0,平台工具集为:Visual Studio 2017 (v141)
报错日志:
调用的目标发生了异常。
在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
在 GalaSoft.MvvmLight.Helpers.WeakAction1.Execute(T parameter) 在 GalaSoft.MvvmLight.Command.RelayCommand
1.Execute(Object parameter)
在 System.Windows.Interactivity.InvokeCommandAction.Invoke(Object parameter)
在 System.Windows.Interactivity.TriggerBase.InvokeActions(Object parameter)
在 System.Windows.Interactivity.EventTriggerBase.OnEvent(EventArgs eventArgs)
在 System.Windows.Interactivity.EventTriggerBase.OnEventImpl(Object sender, EventArgs eventArgs)
在 System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
在 System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
在 System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
在 System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
在 System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
在 System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
在 MS.Internal.LoadedOrUnloadedOperation.DoWork()
在 System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
在 System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
在 System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
在 System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
在 System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
在 System.Windows.Interop.HwndTarget.OnResize()
在 System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
C# .net framework 4.6.1 编写一个WPF项目
C++是在VS2017编写,WinSDK 是:10.0.17763.0
这个需要在客户电脑上在线安装vs上的“使用C++桌面开发”工作负载,这显然是不可行的。
使用这个方法是可以解决问题的。
直接一键安装C++环境(之前我也装了VC++ 2015-2022 X86),解决掉这个报错,然后可以直接运行程序
自己电脑上打开软件,用Process Explorer查看软件加载的dll,把运行库相关的复制出来放软件目录,一般是同一个路径。
多种方法
1.使用打包工具把安装环境一起打包,好处是不用写代码都是自动工具。当然客户得先安装不“绿色”
2.使用一些虚拟机软件连环境一起虚拟,好处是是既绿色又不用安装。坏处是运行启动慢,而且容易被杀软干掉(当然现在这类虚拟机不太好找了,毕竟这种方式那么流行)
3.自己写代码安装环境,先下载vc runtime (VC2017redist.x86/x64.exe 当然也有All in one包,自己看情况办)包放入项目(编译时复制),然后加入检查代码,如果没有对应vc环境,启动命令行安装(vc runtime有命令行,具体参数我就不写了)
至于检查代码么,下面有个
https://www.nuget.org/packages/MscVersion
https://github.com/enclave-networks/mscversion
你可以自己看官方说明。(嘿嘿,vc各个版本注册信息太散乱了,这个老外算收集的比较齐全,可以一用)
下载个 Dependency Walker,安装启动,把你的DLL拖进去,就能看到所有的依赖库,逐个拷贝到你程序目录下就可以了
用打包工具把安装环境一起打包
客户电脑上需要下载安装Microsoft Visual C++ Redistributable 2017
https://learn.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-170#visual-studio-2015-2017-2019-and-2022
你把你VC安装的库文件dll,只要你用到的你就全部拷贝到你生成功的目标文件的文件夹下,这样就不会报错了
你可以找台干净的电脑去运行你的程序,提示需要那个dll你就从你安装好环境的VC安装目录下把dll拷贝到那个干净的环境中去,这样一直不报错为止
直接一键安装C++环境(之前我也装了VC++ 2015-2022 X86)
你已经安装了VC++ 2015-2022 X86 安装包了吗?