WiX安装调用自定义DLL崩溃问题排查(DLL未打包进MSI)
WiX安装包自定义DLL未嵌入MSI导致安装崩溃的解决办法
问题场景
使用WiX制作安装包时,通过自定义DLL实现写入TXT文件的功能,已在product.wxs中配置自定义动作与DLL引用,DLL实现了WriteToFile方法,且所有项目平台均设置为32位,但安装程序运行时崩溃,排查发现输出的MSI中未包含该自定义DLL。
解决方案
修正DLL路径与文件名匹配
当前product.wxs中<Binary>元素引用的DLL名为GetPathFromWizard.dll,但自定义DLL项目的命名是CustomActionLibrary,存在名称不匹配问题。需将SourceFile修改为实际的DLL文件名及正确路径,建议使用解决方案内的相对路径:<Binary Id="CustomActionsDLL" SourceFile="..\CustomActionLibrary\bin\x86\Release\CustomActionLibrary.dll" />替换路径为你的DLL项目实际输出目录。
确认DLL编译配置
右键自定义DLL项目→属性→生成→平台目标设置为x86,确保编译后在输出目录生成32位DLL,且WiX项目引用的路径指向该输出文件。将DLL添加到WiX项目引用
在WiX项目中右键选择「添加→现有项」,将自定义DLL添加到项目中,此时SourceFile可直接使用项目内相对路径,避免编译时WiX无法定位文件。检查WiX编译日志
执行WiX编译命令时查看输出日志,排查是否存在「找不到指定DLL」的警告或错误:candle.exe product.wxs -ext WixUIExtension -ext WixUtilExtension light.exe product.wixobj -ext WixUIExtension -ext WixUtilExtension -out LocalService.msi若日志中有路径相关错误,需调整
SourceFile路径至正确位置。
相关代码参考
product.wxs(修正后关键片段)
<?xml version="1.0" encoding="UTF-8"?> <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"> <Product Id="*" Name="Local Service" Language="1033" Version="0.0.0.3" Manufacturer="innomotion media GmbH" UpgradeCode="81cf2ed4-78b3-42c3-8e6a-cfda143f2133"> <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" Platform="x86" /> <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." /> <MediaTemplate EmbedCab="yes" /> <Feature Id="ProductFeature" Title="Local Service" Level="1"> <ComponentGroupRef Id="ProductComponents" /> <ComponentRef Id="AutoStartComponent" /> </Feature> <InstallExecuteSequence> <Custom Action="SetCustomActionData" Before="RunWriteToFile">NOT Installed</Custom> <Custom Action="RunWriteToFile" After="InstallFiles">NOT Installed</Custom> <Custom Action="ShowSuccessMessage" After="InstallFinalize">NOT Installed</Custom> </InstallExecuteSequence> </Product> <Fragment> <Property Id="USER_URL" Value="http://default.url" /> <Directory Id="TARGETDIR" Name="SourceDir"> <Directory Id="ProgramFilesFolder"> <Directory Id="INSTALLFOLDER" Name="LocalService" /> </Directory> </Directory> <!-- 修正后的DLL引用路径 --> <Binary Id="CustomActionsDLL" SourceFile="..\CustomActionLibrary\bin\x86\Release\CustomActionLibrary.dll" /> <CustomAction Id="SetCustomActionData" Property="RunWriteToFile" Value="USER_URL=[USER_URL]" Execute="immediate" /> <CustomAction Id="RunWriteToFile" BinaryKey="CustomActionsDLL" DllEntry="WriteToFile" Execute="deferred" Impersonate="no" /> <CustomAction Id="ShowSuccessMessage" Execute="immediate" Return="ignore" Script="vbscript"> <![CDATA[ MsgBox "Installation completed successfully!" & vbCrLf & vbCrLf & _ "The program should be available under:" & vbCrLf & vbCrLf & _ "'C:\Program Files (x86)\LocalService\Navcon\net8.0\win-x64'." & vbCrLf & vbCrLf & _ "There, RUN AS ADMINISTRATOR (important): 'NAV.LocalService.Function.exe' to run the function app." & vbCrLf & vbCrLf & _ "Make sure to configure the 'local.settings.json' file as well from that folder.", _ vbInformation, "Installation Success" ]]> </CustomAction> </Fragment> <Fragment> <Component Id="AutoStartComponent" Guid="9f5f7e62-4e3d-4c0f-a5ab-3d2b17b6b1f8" Directory="INSTALLFOLDER"> <RegistryValue Root="HKCU" Key="Software\Microsoft\Windows\CurrentVersion\Run" Name="LocalService" Value="[INSTALLFOLDER]Navcon\net8.0\win-x64\NAV.LocalService.Function.exe" Type="string" /> </Component> </Fragment> </Wix>
自定义DLL代码
namespace CustomActionLibrary { public class CustomActions { [CustomAction] public static ActionResult WriteToFile(Session session) { try { string userUrl = session.CustomActionData["USER_URL"]; string filePath = @"C:\UserUrl.txt"; if (string.IsNullOrEmpty(userUrl)) { userUrl = "nix"; } File.WriteAllText(filePath, userUrl); session.Log($"Successfully wrote USER_URL ({userUrl}) to {filePath}"); return ActionResult.Success; } catch (Exception ex) { session.Log($"Error writing to file: {ex.Message}"); return ActionResult.Failure; } } } }
内容的提问来源于stack exchange,提问作者innom
相关产品推荐
相关产品推荐

