加载链接到使用空DllMain的DLL时,应用程序无法启动(0xC0000142)

问题描述 投票:0回答:1

我有一个简单的Visual Studio 2017解决方案,设置了两个项目。第一个项目是一个可执行文件,它将(加载时链接)链接到从第二个项目生成的DLL。第二个项目是一个简单的测试DLL,它导出一个单独的函数,并包含一个空的DllMain入口点。

如果我尝试调试解决方案,则会收到错误消息“应用程序无法正确启动(0xc0000142)。单击”确定“关闭应用程序。”我试着寻找0xc0000142的含义,但从开发的角度来看却找不到任何有用的东西。

如果我从DLL中删除DllMain入口点并重建,一切正常。

这是DLL头(MyMath.h):

#pragma once

#ifdef THE_DLL_EXPORT
  #define API __declspec(dllexport)
#else
  #define API __declspec(dllimport)
#endif

#ifdef __cplusplus
  extern "C" {
#endif

API int AddNumbers(int a, int b);

#ifdef __cplusplus
  }
#endif

这是DLL代码文件(MyMath.cpp):

#include "MyMath.h"
#include <stdio.h>
#include <Windows.h>

extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, LPVOID lpvReserved)
{
}

int AddNumbers(int a, int b)
{
    return a + b;
}

这是第一个使用DLL(Source.cpp)的项目的主代码文件:

#include <iostream>
#include "MyMath.h"
using namespace std;

int main()
{
    int x = 3;
    int y = 4;
    cout << x << " + " << y << " = " << AddNumbers(x, y) << endl;
    cin.get();
    return 0;
}

这里发生了什么?

c++ dll visual-studio-2017
1个回答
1
投票

DllMain没有回归TRUE。返回FALSE或0会导致应用程序失败,错误代码为0xc0000142。

© www.soinside.com 2019 - 2024. All rights reserved.