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

时间:2018-07-09 06:41:38

标签: c++ c visual-studio-2017 clr

为了展示问题,我编写了一个简单的“ Hello world” DLL x64 ,将其属性更改为 CLR 它的功能来自Exe (也是x64),并且属性已更改为 CLR

但是我的 exe 打印 Output “ Hello World” 并每次都崩溃,并提供故障模块名称KERNELBASE.dll

当我将两者的属性都更改为无CLR 时,它的工作原理就很好。仅对于x86程序中未观察到的x64应用程序会发生这种情况。

我正在Windows Server 2012上使用Visual Studio2017。

如果有人可以指导我正确的方向,这将非常有帮助。

应用崩溃的详细信息

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 DOT 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;
}

1 个答案:

答案 0 :(得分:0)

最后我明白了。当我试图从我的exe警告中删除警告时,通过该链接https://msdn.microsoft.com/en-us/library/ccthbfk8.aspx来警告是 "warning:Calling managed 'DllMain': Managed code may not be run under loader lock,including the DLL entrypoint and calls reached from the DLL entrypoint"