我正在使用https://technet.microsoft.com/en-us/library/ms345134(v=sql.90).aspx处找到的代码,但在跟踪关闭时会继续崩溃。
我在Windows 10 x64上运行Visual Studio 2017。它在Windows 10 x64和Windows 2012 R2 x64上失败,但适用于Windows 7 x86和Windows 2008 R2 x64。
我没有改变任何事情: 通过从Profiler导出默认模板获取文件c:\ temp \ standard.tdf。我尝试了几个模板文件,仍然没有运气。我尝试了几个版本的SQL Server(2014年,2016年,2017年),没有任何乐趣。
这是我正在运行的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
//reference: c:\Program Files (x86)\Microsoft SQL Server\140\SDK\Assemblies\Microsoft.SqlServer.ConnectionInfo.dll
using Microsoft.SqlServer.Management.Common;
// reference c:\Program Files (x86)\Microsoft SQL Server\140\SDK\Assemblies\Microsoft.SqlServer.ConnectionInfoExtended.dll
using Microsoft.SqlServer.Management.Trace;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
TraceServer reader = new TraceServer();
ConnectionInfoBase ci = new SqlConnectionInfo(".\\SQL2014");
((SqlConnectionInfo)ci).UseIntegratedSecurity = true;
reader.InitializeAsReader(ci, @"c:\temp\standard.tdf");
int eventNumber = 0;
while (reader.Read())
{
Console.Write(reader.GetValue(0).ToString());
eventNumber++;
if (eventNumber == 10) break;
}
reader.Close();
// debug reaches the end of the main method, then crashes
}
}
}
在EventViewer中,我找到了以下信息:
Faulting application name: ConsoleApp1.exe, version: 1.0.0.0, time stamp: 0x5a399d47
Faulting module name: KERNELBASE.dll, version: 10.0.16299.15, time stamp: 0x2cd1ce3d
Exception code: 0xc0020001
Fault offset: 0x001008b2
Faulting process id: 0x59c
Faulting application start time: 0x01d3791f189914cd
Faulting application path: c:\users\spagh\documents\visual studio 2017\Projects\ConsoleApp1\ConsoleApp1\bin\Debug\ConsoleApp1.exe
Faulting module path: C:\WINDOWS\System32\KERNELBASE.dll
Report Id: efe37d30-1589-41fe-82b1-f3248afd7110
Faulting package full name:
Faulting package-relative application ID:
我尝试了几个版本的SMO,但无济于事。 在调试和发布中崩溃。在Windows 10和Windows 2012 R2中崩溃。
我没有想法。有什么指针吗?
答案 0 :(得分:4)
我能够在自己的实验室机器上重现这个问题。
我在google上搜索了很多报告的异常代码:0x0020001。异常通常是托管代码加载非托管代码,其中非托管代码"挂起"并试图"报告"回到托管代码 - 但托管代码确实退出,因此它失败了。
我确实花了一些时间用ILSpy对dll文件进行逆向工程,以查看最终的结果。
查看Microsoft.SqlServer.Management.Trace.TraceServer.InitializeAsReader() 我们看到了这个:
// Microsoft.SqlServer.Management.Trace.TraceServer
public void InitializeAsReader(ConnectionInfoBase serverConnInfo, string profileFileName)
{
try
{
this.rowsetCtrl = (TraceUtils.CreateInstance("\\Binn\\pfclnt.dll", "Microsoft.SqlServer.Management.Trace.CTraceObjectsRowsetController") as ITraceObjectsRowsetController);
this.rowsetCtrl.Initialize(serverConnInfo, profileFileName);
this.rowsetCtrl.InitSource(false);
this.traceController = this.rowsetCtrl;
}
catch (Exception ex)
{
TraceUtils.FilterException(ex);
throw new SqlTraceException(typeof(StringConnectionInfo), "CannotInitializeAsReader", ex);
}
}
我们首先要关注的是:
this.rowsetCtrl = (TraceUtils.CreateInstance("\\Binn\\pfclnt.dll", "Microsoft.SqlServer.Management.Trace.CTraceObjectsRowsetController") as ITraceObjectsRowsetController);
它只是一种加载方式: C:\ Program Files(x86)\ Microsoft SQL Server \ 140 \ Tools \ Binn \ PFCLNT.DLL 归档到内存
如果我们查看PFCLNT.DLL文件,并查找Initialize和InitSource方法,它们不是标准的C#方法:
// Microsoft.SqlServer.Management.Trace.CTraceObjectsRowsetController
public unsafe void Initialize(ConnectionInfoBase pConnInfo, string pTemplateFileName)
{
ITraceConnection* ptr = <Module>.CreateOleDbTraceConnection();
if (null == ptr)
{
<Module>.?A0xa0507c21.ProcessError(-2147024882, null);
}
CTraceControllerBase.ConvertConnectionInfoToITraceConnection(pConnInfo, ptr);
ITraceConnection* expr_1D = ptr;
int num = calli(System.Int32 modopt(System.Runtime.CompilerServices.IsLong) modopt(System.Runtime.CompilerServices.CallConvThiscall)(System.IntPtr), expr_1D, *(*(int*)expr_1D + 12));
if (num < 0)
{
$ArrayType$$$BY0CAA@G $ArrayType$$$BY0CAA@G = 0;
initblk(ref $ArrayType$$$BY0CAA@G + 2, 0, 1022);
object arg_50_0 = calli(System.Int32 modopt(System.Runtime.CompilerServices.IsLong) modopt(System.Runtime.CompilerServices.CallConvThiscall)(System.IntPtr,System.UInt16*), ptr, ref $ArrayType$$$BY0CAA@G, *(*(int*)ptr + 180));
<Module>.?A0xa0507c21.ProcessError(num, (ushort*)(&$ArrayType$$$BY0CAA@G));
}
byte b = 0;
byte b2 = 0;
int num2 = calli(System.Int32 modopt(System.Runtime.CompilerServices.IsLong) modopt(System.Runtime.CompilerServices.CallConvThiscall)(System.IntPtr,System.Byte*,System.Byte*,System.UInt16*), ptr, ref b, ref b2, 0, *(*(int*)ptr + 148));
if (num2 < 0)
{
$ArrayType$$$BY0CAA@G $ArrayType$$$BY0CAA@G2 = 0;
initblk(ref $ArrayType$$$BY0CAA@G2 + 2, 0, 1022);
object arg_9A_0 = calli(System.Int32 modopt(System.Runtime.CompilerServices.IsLong) modopt(System.Runtime.CompilerServices.CallConvThiscall)(System.IntPtr,System.UInt16*), ptr, ref $ArrayType$$$BY0CAA@G2, *(*(int*)ptr + 180));
<Module>.?A0xa0507c21.ProcessError(num2, (ushort*)(&$ArrayType$$$BY0CAA@G2));
}
if (b < 9)
{
ITraceConnection* ptr2 = null;
int num3 = calli(System.Int32 modopt(System.Runtime.CompilerServices.IsLong) modopt(System.Runtime.CompilerServices.CallConvThiscall)(System.IntPtr,ITraceConnection/eConnectionType,ITraceConnection**), ptr, 0, ref ptr2, *(*(int*)ptr + 28));
if (num3 < 0)
{
$ArrayType$$$BY0CAA@G $ArrayType$$$BY0CAA@G3 = 0;
initblk(ref $ArrayType$$$BY0CAA@G3 + 2, 0, 1022);
object arg_E6_0 = calli(System.Int32 modopt(System.Runtime.CompilerServices.IsLong) modopt(System.Runtime.CompilerServices.CallConvThiscall)(System.IntPtr,System.UInt16*), ptr, ref $ArrayType$$$BY0CAA@G3, *(*(int*)ptr + 180));
<Module>.?A0xa0507c21.ProcessError(num3, (ushort*)(&$ArrayType$$$BY0CAA@G3));
}
ITraceConnection* expr_F1 = ptr;
object arg_FB_0 = calli(System.UInt32 modopt(System.Runtime.CompilerServices.IsLong) modopt(System.Runtime.CompilerServices.CallConvStdcall)(System.IntPtr), expr_F1, *(*(int*)expr_F1 + 8));
ptr = ptr2;
ITraceConnection* expr_101 = ptr2;
num2 = calli(System.Int32 modopt(System.Runtime.CompilerServices.IsLong) modopt(System.Runtime.CompilerServices.CallConvThiscall)(System.IntPtr), expr_101, *(*(int*)expr_101 + 12));
if (num2 < 0)
{
$ArrayType$$$BY0CAA@G $ArrayType$$$BY0CAA@G4 = 0;
initblk(ref $ArrayType$$$BY0CAA@G4 + 2, 0, 1022);
object arg_132_0 = calli(System.Int32 modopt(System.Runtime.CompilerServices.IsLong) modopt(System.Runtime.CompilerServices.CallConvThiscall)(System.IntPtr,System.UInt16*), ptr, ref $ArrayType$$$BY0CAA@G4, *(*(int*)ptr + 180));
<Module>.?A0xa0507c21.ProcessError(num2, (ushort*)(&$ArrayType$$$BY0CAA@G4));
}
}
this.templateFileName = pTemplateFileName;
byte* ptr3 = pTemplateFileName;
if (ptr3 != null)
{
ptr3 = RuntimeHelpers.OffsetToStringData + ptr3;
}
Char modopt(System.Runtime.CompilerServices.IsConst)& char modopt(System.Runtime.CompilerServices.IsConst)& = ptr3;
CTraceRowsetCtrl* ptr4 = <Module>.@new(16460u);
CTraceRowsetCtrl* ptr5;
try
{
if (ptr4 != null)
{
ptr5 = <Module>.CTraceRowsetCtrl.{ctor}(ptr4, ptr, char modopt(System.Runtime.CompilerServices.IsConst)&);
}
else
{
ptr5 = 0;
}
}
catch
{
<Module>.delete((void*)ptr4);
throw;
}
this.m_pTraceCtrl = ptr5;
if (0 == ptr5)
{
<Module>.?A0xa0507c21.ProcessError(-2147024882, null);
}
ITraceConnection* expr_192 = ptr;
object arg_19C_0 = calli(System.UInt32 modopt(System.Runtime.CompilerServices.IsLong) modopt(System.Runtime.CompilerServices.CallConvStdcall)(System.IntPtr), expr_192, *(*(int*)expr_192 + 8));
}
因此,由于我对此的知识非常有限,我认为你面对的是一些不在你手中的东西。我相信我们需要在microsoft上找一个人,利用你的网络和SQL社区,让他们看看它失败的原因。