使用跟踪对象时应用程序崩溃

spa*_*dba 4 c#

我正在使用在https://technet.microsoft.com/zh-cn/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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在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: 
Run Code Online (Sandbox Code Playgroud)

我尝试了多个版本的SMO,但均无济于事。在调试和发行中崩溃。在Windows 10和Windows 2012 R2中崩溃。

我没主意了。有指针吗?

Möt*_*ötz 5

我能够在自己的实验室机器上重现该问题。

我在Google上做了很多搜索,查找报告的异常代码:0x0020001。通常情况是例外,托管代码会加载非托管代码,其中非托管代码“挂起”并尝试“报告”回托管代码-但是托管代码确实退出了,因此失败。

链接1

连结2

我确实花了一些时间使用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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我们首先需要关注的行是:

this.rowsetCtrl = (TraceUtils.CreateInstance("\\Binn\\pfclnt.dll", "Microsoft.SqlServer.Management.Trace.CTraceObjectsRowsetController") as ITraceObjectsRowsetController);
Run Code Online (Sandbox Code Playgroud)

这只是将以下文件加载到内存中的一种方法: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));
}
Run Code Online (Sandbox Code Playgroud)

因此,以我对此知识的了解非常有限,我认为您正在面临无法控制的事情。我认为,我们需要利用您的网络和SQL社区来吸引Microsoft的某个人,让他们调查失败的原因。