我需要创建一个与回调函数一起使用的 dll。当我在项目属性中设置 Runtime Libary = Multi-threaded Debug (/MTd) 时,它会生成以下错误消息:
但是当我设置 Runtime Libary = Multi-threaded Debug DLL (/MDd) 时,应用程序可以完美运行
看看我的DLL:
回调过程.h
#include <string>
#ifdef CALLBACKPROC_EXPORTS
#define CALLBACKPROC_API __declspec(dllexport)
#else
#define CALLBACKPROC_API __declspec(dllimport)
#endif
// our sample callback will only take 1 string parameter
typedef void (CALLBACK * fnCallBackFunc)(std::string value);
// marked as extern "C" to avoid name mangling issue
extern "C"
{
//this is the export function for subscriber to register the callback function
CALLBACKPROC_API void Register_Callback(fnCallBackFunc func);
}
回调程序.cpp
#include "stdafx.h"
#include "callbackproc.h"
#include <sstream>
void Register_Callback(fnCallBackFunc func)
{
int count = 0;
// let's send 10 messages to the subscriber
while(count < 10)
{
// format the message
std::stringstream msg;
msg << "Message #" << count;
// call the callback function
func(msg.str());
count++;
// Sleep for 2 seconds
Sleep(2000);
}
}
标准数据文件
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN
// Windows Header Files:
#include <windows.h>
我使用 dll 的应用程序
#include <windows.h>
#include <string>
#include "callbackproc.h"
// Callback function to print message receive from DLL
void CALLBACK MyCallbackFunc(std::string value)
{
printf("callback: %s\n", value.c_str());
}
int _tmain(int argc, _TCHAR* argv[])
{
// Register the callback to the DLL
Register_Callback(MyCallbackFunc);
return 0;
}
我哪里错了?坦克!