c++ - 调用我的 CLR 项目时 KernelBase.dll 出现未处理的异常

标签 c++ exception clr unmanaged managed

我得到这个异常:

Unhandled exception at 0x75374B32 (KernelBase.dll) in LogLoaderUnmanaged.exe: 0xE0434352 (parameters: 0x80070002, 0x00000000, 0x00000000, 0x00000000, 0x74040000).

当我使用此代码(Application .exe 类型项目的一部分)调用我的 CLR 项目时:

int _tmain(int argc, _TCHAR* argv[])
{
    _tprintf_s(_T("Press enter to start logging messages."));
    _getch();
    std::string app("Application");
    std::string domain("Domain");
    std::string message("Message");
    UnmanagedLoggerClient::LogError(Debug, app.c_str(), domain.c_str(), message.c_str());
    _tprintf_s(_T("Done."));
}

错误发生在调用 LogError 时,它在我的 CLR DLL header 中定义如下:

#ifdef UNMANAGEDLOGGERCLIENT_EXPORTS
#define WIN32PROJECT_API __declspec(dllexport)
#else
#define WIN32PROJECT_API __declspec(dllimport)
#endif

enum UnmanagedLogLevel
{
    Debug = 0,
    Error = 1
};

static class WIN32PROJECT_API UnmanagedLoggerClient
{
public:
    static void LogError (UnmanagedLogLevel level, const char* app, const char* domain, const char* message);
};

在实现中,该方法非常简单:

void UnmanagedLoggerClient::LogError(UnmanagedLogLevel level, const char* app, const char* domain, const char* message)
{
    LoggerClient::LogLevel logLevel = static_cast<LoggerClient::LogLevel>(level);
    LoggerClient::Logger::LogError(logLevel, gcnew String(app), gcnew String(domain), gcnew String(message), DateTime::Now);
}

知道为什么会这样吗?我不是真正的 C++ 专家,而且我还没有找到任何有用的在线搜索此问题的信息。非常感谢任何输入!

最佳答案

您正在使用一种非常脆弱的方式来初始化 CLR,因此诊断很差。您的异常诊断中显示“找不到文件”错误代码,错误代码为 0x80070002。

您成功启动了 CLR,异常代码是托管异常,但它找不到文件。确保所有可执行文件都存在于与您的 EXE 相同的目录中。如果没有帮助,请使用 SysInternals 的 ProcMon,您会看到它正在搜索文件但找不到。

关于c++ - 调用我的 CLR 项目时 KernelBase.dll 出现未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17304691/

相关文章:

.net - 我怎么知道哪个运行时主机当前正在运行我的代码?

c++ - 多个文件出现错误 C2027 : use of undefined type,

python - 为什么捕获 StopIteration 时无法跳出循环?

c# - 作为非托管应用程序的子窗口的 Windows 窗体

python - 处理或引发 Python 异常是否有效?

python - 崩溃后Python脚本无法重新启动

.net - 从托管 powershell 中的脚本或命令检索参数

c++ - 不可全局访问的单例

c++ - 优化索引数组求和

c++ - tr1 的 unordered_map 中的自定义分配器