How to introduce HWND in C++/CLI code?

Big picture -- I have regular C++ code (some functions put in the namespaces) and I have C# code which should call this C++ stuff. As I was told the only way to do this is to write C++/CLI wrapper. Based on what I read I try to use the headers from reg.C++ and link against reg.C++ dll.

The first problem I have is HWND . In reg.C++ it almost comes from thin air (I don't have even proper include for it, and VS accepts it), in CLI/C++ I explicitly added include path to c:Program Files (x86)Microsoft SDKsWindowsv7.1AInclude (*) so I could add

#include <windef.h>

in C++/CLI code but it in turn includes winnt.h and I got errors in this file. I sense I included reg.C++ into CLI/C++ code and it is not suitable for that.

So where there is HWND definition for C++/CLI?

(*) This directory, because in reg.C++ project VS found the definition of HWND there.

Update Later I installed Windows Development Kit 8.0, changed the paths to point to it, included windows.h however then I got errors within the included files. For comparison I asked my colleague to create blank project for me, I added it to my solution and it worked. So I believe now is some odd thing with setup of my VS it cannot create properly CLI/C++ project.


In C# you use System.IntPtr for win32 handles, so you should use System::IntPtr in C++ which can be cast into HWND safely

look at this for example code:

http://blogs.msdn.com/b/yvesdolc/archive/2007/09/10/c-cli-intptr-to-an-hwnd.aspx


You mention that with native C++, HWND is resolved (or declared) without an include, however you may want to look again. This most certainly isn't the case. It is possible you may be using a precompiled header (ie stdafx.h) which generally includes windows.h, which in turn, includes windef.h. Otherwise, and explicit include exists or HWND will not be defined.

Next, C++/CLI does not prohibit you from including windows.h and using a HWND (This is common, and I have several projects which do exactly that). For instance, with D3D interop or incorporating the Media Foundation libraries, passing the HWND is most often necessary.

You may want to start by ensuring your wrapper library compiles with bare skeleton code, including windows.h.

I strongly prefer C++/CLI over PInvoke, and you may already know PInvoke is an option as well, removing the need for the wrapper. But PInvoke does have marshaling overhead, and is a bit fragile with respect to the entry point signatures.

I hope this helps. I would be happy to help with subsequent questions which may be impending.

链接地址: http://www.djcxy.com/p/59618.html

上一篇: (CLR)C ++ / CLI在表单文件中包含头文件?

下一篇: 如何在C ++ / CLI代码中引入HWND?