Application fails to start (0xC0000142) when load-linking to DLL that uses empty DllMain

0

I have a simple Visual Studio 2017 solution set up with two projects. The first project is an executable that links (load-time linking) to a DLL generated from the second project. The second project is a simple test DLL that exports a single function, and contains an empty DllMain entry point.

If I try to debug the solution, I get an error that says "The application was unable to start correctly (0xc0000142). Click OK to close the application." I tried searching for the meaning of 0xc0000142, but couldn't find anything useful from a development point of view.

If I remove the DllMain entry point from the DLL and rebuild, everything works fine.

Here is the DLL header (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

Here is the DLL code file (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;
}

And here is the main code file from the first project that uses the 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;
}

What is going on here?

c++
dll
visual-studio-2017
asked on Stack Overflow Dec 21, 2017 by user2498421

1 Answer

3

DllMain wasn't returning TRUE. Returning FALSE or 0 causes the application to fail with error code 0xc0000142.

answered on Stack Overflow Dec 21, 2017 by user2498421

User contributions licensed under CC BY-SA 3.0