如何使用DLL函數中的複雜返回類型調用後實現的函數(例如,在消費應用程序中實現的函數指針或虛函數)?調用消費應用程序中實現的函數的C++ DLL
我試了下面的方案,但有錯誤。
Test.h:
#ifdef _DLL_IMPL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
typedef string (*test)(void);
extern DLL_EXPORT test __test;
DLL_EXPORT int launch();
Test.cpp的:
#define _DLL_IMPL
#include "Test.h"
test __test = 0;
int launch()
{
string a = __test();
return 0;
}
而消費應用程序是這樣:
Main.cpp的:
#include "Test.h"
#pragma comment(lib, "Test.lib")
string test_impl()
{
string a = "test";
return a;
}
int main(int args, char** argv)
{
__test = &test_impl;
return launch();
}
我有一個後續的錯誤信息:
Windows has triggered a breakpoint in TestRun.exe.
This may be due to a corruption of the heap, which indicates a bug in
TestRun.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while TestRun.exe has focus.
The output window may have more diagnostic information.
我不知道究竟發生了什麼。當我嘗試使用新操作符在消費應用程序中創建的字符指針的返回類型時,也會發生錯誤,並且會使用delete []運算符在DLL函數中釋放它。
有人可以解釋爲什麼錯誤發生的原因,並建議我一些解決方案,這種方案?謝謝!
我不相信你可以在新的應用程序,在dll中刪除。這會導致堆損壞。 – JoeFish
是的,我也相信這就是它不起作用的原因。我真的不知道如何實現我描述的方案(即調用在應用中實現的功能)。 – bestmike007