0

RaiseException如果我需要停止执行,我会调用我的代码。正如预期的那样,这会在事件查看器应用程序日志中添加一个条目。我希望事件查看器包含数据。

根据文档RaiseException,该lpArguments参数可以执行此操作,但它需要一个 ULONG_PTR 参数。

这是我的代码: RaiseException(58585,0,0,NULL);

是否有人可以展示如何将指针传递给函数中最后一个参数的字符串RaiseException

4

1 回答 1

0

您可以将所有内容传递给lpArgumentsWin32 RaiseException API。你可以参考下面的代码。

#include <windows.h>
#include <stdio.h>
#include <strsafe.h>

struct MySEHExceptionStruct
{
    DWORD Index;
};

#define EXCEPTION_CUSTOM_1 0x1

void TestRaiseException()
{
    // First allocate space for a MySEHExceptionStruct instance.
    MySEHExceptionStruct* pMySEHExceptionStruct = (MySEHExceptionStruct*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MySEHExceptionStruct));
    if (pMySEHExceptionStruct)
    {
        pMySEHExceptionStruct->Index = 0x999;
    }

    LPVOID lpszMessage2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 255 * sizeof(TCHAR));
    if (lpszMessage2)
    {
        StringCchPrintf((LPTSTR)lpszMessage2, LocalSize(lpszMessage2) / sizeof(TCHAR), TEXT("Exception Message %d."), 2);
    }

    ULONG_PTR lpArguments[2]{};
    if (lpArguments)
    {
        lpArguments[0] = (ULONG_PTR)pMySEHExceptionStruct;
        lpArguments[1] = (ULONG_PTR)lpszMessage2;
    }

    RaiseException(EXCEPTION_CUSTOM_1, 0, 2, lpArguments);
}

DWORD g_nNumberOfArguments;
ULONG_PTR* g_lpArguments;

DWORD FilterFunction(LPEXCEPTION_POINTERS info)
{
    printf("1 ");                     // printed first
    if (info->ExceptionRecord->ExceptionCode == EXCEPTION_CUSTOM_1)
    {
        g_nNumberOfArguments = info->ExceptionRecord->NumberParameters;
        //not sure pointer copy or value copy
        g_lpArguments = info->ExceptionRecord->ExceptionInformation;
        return EXCEPTION_EXECUTE_HANDLER;
    }
    else
    {
        return EXCEPTION_CONTINUE_SEARCH;
    }
}

BOOL CallTestRaiseException()
{
    __try
    {
        TestRaiseException();
    }
    __except(FilterFunction(GetExceptionInformation()))
    {
        if (g_nNumberOfArguments > 0)
        {
            MySEHExceptionStruct* pMySEHExceptionStruct = (MySEHExceptionStruct*)g_lpArguments[0];
            TCHAR* lpszMessage2 = (TCHAR*)g_lpArguments[1];

            HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, pMySEHExceptionStruct);
            HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, lpszMessage2);
        }
        return FALSE;
    }
    return TRUE;
}
于 2021-05-18T03:11:43.690 回答