LoadUserProfile->未处理的崩溃(如何防止?)

时间:2014-01-02 18:53:06

标签: vb.net interop

我有一个以SYSTEM身份运行的Windows服务。有时,在某些计算机上(这不能轻易复制),当我调用LoadUserProfile时,整个过程都会消失。

我说的是.NET应用程序,这里是堆栈跟踪:

Application: MyProcess.exe

Framework Version: v4.0.30319

Description: The process was terminated due to an unhandled exception.

Exception Info: System.AccessViolationException

Stack:

   at MyAssembly.MyClass.LoadUserProfile(IntPtr, PROFILEINFO ByRef)

我不确定这个问题是否可以解决,但我希望能够处理这个问题而不是我的过程死亡。我正在使用try..catch以及在我的应用程序中捕获“未处理的异常”。

以下是有关我如何使用该功能的更多信息:

    <StructLayout(LayoutKind.Sequential)> _
        <System.Runtime.InteropServices.ComVisible(False)>
    Public Structure PROFILEINFO
        Public dwSize As Integer
        Public dwFlags As Integer
        Public lpUserName As String
        Public lpProfilePath As String
        Public lpDefaultPath As String
        Public lpServerName As String
        Public lpPolicyPath As String
        Public hProfile As IntPtr
    End Structure

Declare Auto Function LoadUserProfile Lib "userenv" Alias "LoadUserProfile" (ByVal hToken As IntPtr, ByRef lpProfileInfo As PROFILEINFO) As Boolean

                    Dim UserProfile As New PROFILEINFO
                    UserProfile.dwSize = Marshal.SizeOf(UserProfile)
                    UserProfile.lpUserName = strUserName 'strDomain & "\" & strUserName
                    UserProfile.dwFlags = 1
                    Try
                        If LoadUserProfile(token, UserProfile) Then
                            impResult.ProfileLoaded = True
                            hProfile = UserProfile.hProfile
                            IsProfileLoaded = True
                        Else
                            impResult.ErrorOccured = True
                            Dim intErrorCode As Integer = Marshal.GetLastWin32Error()
                            impResult.ErrorString = "Profile was not loaded - error code: " & intErrorCode.ToString
                            Throw New Win32Exception(intErrorCode)
                        End If
                    Catch ex As System.AccessViolationException
                        impResult.ErrorOccured = True
                        impResult.ErrorString = ex.ToString
                    End Try

知道如何解决这个问题吗?谢谢!

0 个答案:

没有答案