在dll中使用CLR的意外Stackoverflow异常

时间:2015-02-21 13:53:44

标签: dll c++-cli interop unmanaged managed

我有一个接受C dll作为插件的软件。 dll导出两个函数:init和quit。为了测试插件的工作原理,我在NativeWrapper概念之后编写了三个项目:C ++中的托管库(dll),C ++中的本机包装器(也是dll,导出init和quit函数)和C ++测试程序。现在我只是测试init函数。

所有这些都是在VS 2013中构建和运行的,没有任何问题。但是,当我用所述软件插入两个dll时,它会停止工作并抛出stackoverflow异常。我知道stackoverflow异常的最可能原因是无限循环或递归。但我的代码中没有。有没有人有任何线索?如果我删除了互操作电话:

ManagedLib::ManagedClass::SayHello()

然后dll作为插件运行没有问题。在这种情况下,它只输出一次到控制台,这告诉我调用软件中没有循环或递归。

顺便说一句,我必须指出该软件已经存在多年了。所以我不确定问题是否来自使用的开发环境的差异(框架4.5与???)

Managedlib.h:

#pragma once

namespace ManagedLib
{

public ref class ManagedClass
{
public:
  static void SayHello();
};

}

Managedlib.cpp

#include "stdafx.h"

#include "ManagedLib.h"

using namespace System;

using namespace ManagedLib;

void ManagedClass::SayHello()
{
  Console::WriteLine("Hello from the managed world!");
  Console::Out->Flush();
}

Nativewrapper.h:

#pragma once

#include <Windows.h>

#undef __cplusplus
#ifdef __cplusplus
extern "C"
{
#endif

  //EXPORTS 
    int WINAPI Init(HWND hwnd, UINT, int);
    int WINAPI Quit(HWND hwnd);
#ifdef __cplusplus
}
#endif

Nativewrapper.cpp:

#include "stdafx.h"
#include <stdio.h>

#define BUILDING_WRAPPER_DLL

#include "NativeWrapper.h"

int WINAPI Init(HWND hwnd, UINT32, int)
{

    FILE * pConsole;
    AllocConsole();
    freopen_s(&pConsole, "CONOUT$", "wb", stdout);
    printf("Started\n");

    ManagedLib::ManagedClass::SayHello();
    return 1;
}

int WINAPI Quit(HWND hwnd)
{
    return 1;
}

0 个答案:

没有答案