在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Recently my friend Yoel had just a wonderful idea. We have an old Win32 C++ application, and we wanted to add a serious logging infraestructure so we can provide better support in case the application crashes. So Yoel came with the idea of using an existing framework for logging: LOG4NET The only problem was, how can we integrate these two together. One way was problably exporting a .NET object as COM. But Yoel had a better idea. Create a C++ Managed application that will comunicate with the LOG4NET assemblies, and export functions so the native applications can use that. How great is that. Well he finally made it, and this is the code of how he did it. First he created a C++ CLR Empty project and set its output type to Library. In the references we add a refrence to the Log4Net Library. We add a .cpp code file and we call it Bridge.cpp. Here is the code for it: #include <atlstr.h> using namespace System; /// <summary> /// Example of how to simply configure and use log4net /// </summary> public:static void ReportMessageWarning(char* msg) static void ReportMessageInfo(char* msg) extern "C" _declspec(dllexport) void ReportMessageWarning(char* msg) _declspec(dllexport) void ReportMessageInfo(char* msg) _declspec(dllexport) void ReportMessageDebug(char* msg) Ok. That's all. Now we have a managed C++ DLL that exposes some functions as an standard C++ DLL and we can use it with native win32 applications. Let's do a test. Lets create a Win32 Console application. And add this code: // Log4NetForC++.cpp : Defines the entry point for the console application. _declspec(dllimport) void ReportMessageWarning(char* msg); int _tmain(int argc, _TCHAR* argv[]) http://blogs.artinsoft.net/mrojas/archive/2008/06/19/log4net-for-c.aspx |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论