我在SQL SMO库中编写了一个简单的接口,因此我可以在Inno Setup脚本安装过程中停止并启动SQL Server。我的开发机器是一台64位处理器。托管代码使用.NET,Targinaling .Net 4.0并为X86平台编译的DLLEXPORT以Visual Basic编写。我正在使用Inno设置5.5.9 Unicode版本。
设置可执行文件可在我的开发机和我尝试过的其他64位处理器上工作。当我在32位处理器系统上运行可执行文件时,我会发现一个错误:
外部异常E0434352
这是无助和通用的。这两个系统都是Windows 10,但我也在其他Windows版本上尝试过它,而64位作品,而32位也没有尝试过。那么,我对障碍在32位跑步做了什么?
Inno设置代码:
[Files]
Source: "fiSqlSmoLib2binx86DebugfiSqlSmoLib2.dll"; DestDir: "{app}";
Flags: nocompression 32bit
[Code]
function StartSql(machineName, sqlServerName: AnsiString): Integer;
external 'StartSql@files:fiSqlSmoLib2.dll stdcall setuponly delayload';
function StopSql(machineName, sqlServerName: AnsiString): Integer;
external 'StopSql@files:fiSqlSmoLib2.dll stdcall setuponly delayload';
function InitializeSetup(): Boolean;
var
SQLresultCode : Integer;
Computer : String;
SQLServer : String;
begin
Computer := GetComputerNameString;
SQLServer := 'FACTORYINSITE';
try
SQLresultCode := StopSql(Computer, SQLServer);
except
ShowExceptionMessage;
exit;
end;
try
SQLresultCode := StartSql(Computer, SQLServer);
except
ShowExceptionMessage;
exit;
end;
end;
视觉基本代码:
Imports System.Runtime.InteropServices
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Imports Microsoft.SqlServer.Management.Smo.Wmi
Imports RGiesecke.DllExport
Public Class fiSqlSmo
<DllExport("StartSql", CallingConvention.StdCall)> _
Public Shared Function StartSql(<MarshalAs(UnmanagedType.LPStr)> machineName As String, <MarshalAs(UnmanagedType.LPStr)> sqlServerName As String) As Integer
Dim mc As ManagedComputer
Dim Svc As Service
Dim svcState As ServiceState
Try
mc = New ManagedComputer(machineName)
Svc = mc.Services("MSSQL$" + sqlServerName)
Catch ex As Exception
Return ServiceState.Unknown
End Try
Try
svcState = Svc.ServiceState
Catch ex As Exception
Return ServiceState.Unknown
End Try
If (Svc.ServiceState <> ServiceState.Running) Then
Svc.Start()
End If
Return svcState
End Function
<DllExport("StopSql", CallingConvention.StdCall)> _
Public Shared Function StopSql(<MarshalAs(UnmanagedType.LPStr)> machineName As String, <MarshalAs(UnmanagedType.LPStr)> sqlServerName As String) As Integer
Dim mc As ManagedComputer
Dim Svc As Service
Dim svcState As ServiceState
Try
mc = New ManagedComputer(machineName)
Svc = mc.Services("MSSQL$" + sqlServerName)
Catch ex As Exception
Return ServiceState.Unknown
End Try
Try
svcState = Svc.ServiceState
Catch ex As Exception
Return ServiceState.Unknown
End Try
If (Svc.ServiceState = ServiceState.Running) Then
Svc.Stop()
End If
Return svcState
End Function
End Class
问题似乎是使用的SMO组件的版本。可行的设置程序与SLQ 2016版本的SMO链接,而失败的机器仅安装了SQL 2008 R2。陈旧版本的机器似乎是32位。
似乎是偶然的。重建.NET代码以引用最古老的SMO版本使其在所有计算机上工作。