C++/CLI DLL 和 x64 位 Exe 的应用程序崩溃

App Crash for C++/CLI DLL and Exe of x64 bit

只是为了展示问题,我写了一个简单的 DLL x64 of "Hello world" 将其 属性 更改为CLR 从同样是 x64 的 Exe 调用其函数 属性 更改为 CLR.

但是我的 exe 打印了 Output "Hello World" 并且每次都崩溃并给出故障模块名称 KERNELBASE.dll

当我将两者的 属性 更改为 NO CLR 时,它工作得很好。这仅发生在 x64 应用程序中,在 x86 程序中未观察到。

我在 Windows Server 2012 上使用 Visual Studio 2017。

如果有人能指导我正确的方向,那将非常有帮助。

应用崩溃详情

Problem Event Name: APPCRASH
  Application Name: consuming_using_clr.exe
  Application Version:  0.0.0.0
  Application Timestamp:    5b42fff3
  Fault Module Name:    KERNELBASE.dll
  Fault Module Version: 6.3.9600.17055
  Fault Module Timestamp:   532954fb
  Exception Code:   c0020001
  Exception Offset: 0000000000005bf8
  OS Version:   6.3.9600.2.0.0.16.7
  Locale ID:    1033
  Additional Information 1: 8e72
  Additional Information 2: 8e72455f15a8480830570fcb3c4abf60
  Additional Information 3: f8d5
  Additional Information 4: f8d519c5149c6c561af747d4db7e910a

DLL dot.h 文件

// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the DLLWINDOWSDESKTOPWIZARD_EXPORTS
// symbol defined on the command line. This symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see 
// DLLWINDOWSDESKTOPWIZARD_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef DLLWINDOWSDESKTOPWIZARD_EXPORTS
#define DLLWINDOWSDESKTOPWIZARD_API __declspec(dllexport)
#else
#define DLLWINDOWSDESKTOPWIZARD_API __declspec(dllimport)
#endif
#using<System.dll>
#include<iostream>
using namespace System;
using namespace std;
// This class is exported from the Dll_windows_desktop_wizard.dll
class DLLWINDOWSDESKTOPWIZARD_API CDllwindowsdesktopwizard 
{

public:
    CDllwindowsdesktopwizard(void);
    ~CDllwindowsdesktopwizard(void);
    void helloworld();

};

*DLL 点 CPP 文件 *

// Dll_windows_desktop_wizard.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "Dll_windows_desktop_wizard.h"


// This is the constructor of a class that has been exported.
// see Dll_windows_desktop_wizard.h for the class definition

CDllwindowsdesktopwizard::CDllwindowsdesktopwizard(void)
{
}

CDllwindowsdesktopwizard::~CDllwindowsdesktopwizard(void)
{
}

void CDllwindowsdesktopwizard::helloworld()
{
    Console::WriteLine("Hello World");
}

EXE调用DLL函数

#include<iostream>
#include "Dll_windows_desktop_wizard.h"
using namespace System;

int main()
{
    CDllwindowsdesktopwizard lv_obj;
    lv_obj.helloworld();
    return 0;
}

dllMain.cpp

#include "stdafx.h"

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

最后我得到了 it.Came 这个 link https://msdn.microsoft.com/en-us/library/ccthbfk8.aspx 因为我试图从我的 exe 警告中删除警告 "warning:Calling managed 'DllMain': Managed code may not be run under loader lock,including the DLL entrypoint and calls reached from the DLL entrypoint"