Managed C++ dll: #define _AFXDLL or do not use /MD[ d]?

时间:2022-09-01 13:35:39

[问题]

Hello all

   

I'm writing a managed C++ dll with will be acting as an intermediate between a lot of old unmanaged legacy C/C++ and a new C# WPF ui. While trying to compile it I'm getting this error:

fatal error C1189: #error: Building MFC application with /MD[ d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[ d]

   

If I define _AFXDLL under the project properties Preprocessor Definitions I get this error:

fatal error C1189: #error: WINDOWS.H already included.  MFC apps must not #include <windows.h>


If I change the threading from /MD[ d] I get this error:

Command line error D8016: '/MTd' and '/clr' command-line options are incompatible

   

The problem seems to be the inclusion of "stdafx.h", which I need for common windows data types I'm going to marshal. What am I doing wrong?

 

【答案】

Project + Properties, General.  Ensure Use of MFC is set to "Use MFC in a Shared Library".

 

From: https://social.msdn.microsoft.com/Forums/vstudio/en-US/553fa850-5e6e-4ecb-a4eb-bbd81f8ff6b6/managed-c-dll-define-afxdll-or-do-not-use-md-d?forum=vcgeneral