我的目标是从纯 C++ 本机 Windows API 级程序(非托管 C++ 或 C++/CLI)显示 .NET Windows 窗体消息框。
也就是说,出于学习目的,我想用纯 C++ 实现下面评论中显示的 C# 代码:
/*
// C# code that this C++ program should implement:
using System.Windows.Forms;
namespace hello
{
class Startup
{
static void Main( string[] args )
{
MessageBox.Show(
"Hello, world!",
".NET app:",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
}
}
}
*/
#include <stdexcept>
#include <string>
#include <iostream>
#include <stdlib.h> // EXIT_SUCCESS, EXIT_FAILURE
#undef UNICODE
#define UNICODE
#include <windows.h>
#include <Mscoree.h>
#include <comdef.h>
_COM_SMARTPTR_TYPEDEF( ICorRuntimeHost, IID_ICorRuntimeHost ); // ICorRuntimeHostPtr
// #import is an MS extension, generates a header file. Will be replaced with #include.
#import "C:\\WINDOWS\\Microsoft.NET\\Framework\\v1.1.4322\\mscorlib.tlb" \
raw_interfaces_only rename( "ReportEvent", "reportEvent" )
typedef mscorlib::_AppDomainPtr AppDomainPtr;
typedef mscorlib::_ObjectHandlePtr ObjectHandlePtr;
typedef mscorlib::_AssemblyPtr AssemblyPtr;
bool throwX( std::string const& s ) { throw std::runtime_error( s ); }
template< class Predicate >
struct Is: Predicate
{};
template< class Type, class Predicate >
bool operator>>( Type const& v, Is< Predicate > const& check )
{
return check( v );
}
struct HrSuccess
{
bool operator()( HRESULT hr ) const
{
::SetLastError( hr );
return SUCCEEDED( hr );
}
};
void cppMain()
{
ICorRuntimeHostPtr pCorRuntimeHost;
CorBindToRuntimeEx(
L"v1.1.4322", // LPWSTR pwszVersion, // RELEVANT .NET VERSION.
L"wks", // LPWSTR pwszBuildFlavor, // "wks" or "svr"
0, // DWORD flags,
CLSID_CorRuntimeHost, // REFCLSID rclsid,
IID_ICorRuntimeHost, // REFIID riid,
reinterpret_cast<void**>( &pCorRuntimeHost )
)
>> Is< HrSuccess >()
|| throwX( "CorBindToRuntimeEx failed" );
pCorRuntimeHost->Start() // Without this GetDefaultDomain fails.
>> Is< HrSuccess >()
|| throwX( "CorRuntimeHost::Start failed" );
IUnknownPtr pAppDomainIUnknown;
pCorRuntimeHost->GetDefaultDomain( &pAppDomainIUnknown )
>> Is< HrSuccess >()
|| throwX( "CorRuntimeHost::GetDefaultDomain failed" );
AppDomainPtr pAppDomain = pAppDomainIUnknown;
(pAppDomain != 0)
|| throwX( "Obtaining _AppDomain interface failed" );
// This fails because Load requires a fully qualified assembly name.
// I want to load the assembly given only name below + relevant .NET version.
AssemblyPtr pFormsAssembly;
pAppDomain->Load_2( _bstr_t( "System.Windows.Forms" ), &pFormsAssembly )
>> Is< HrSuccess >()
|| throwX( "Loading System.Windows.Forms assembly failed" );
// ... more code here, not yet written.
}
int main()
{
try
{
cppMain();
return EXIT_SUCCESS;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
}
return EXIT_FAILURE;
}
计划是,加载程序集后,继续进行MessageBox
类和调用Show
。但这可能是一种错误的做法。因此,我同样很高兴得到一个答案,它显示了如何在没有找到程序集的完全限定名称的情况下做到这一点(当然,没有对完全限定名称进行硬编码!)。
该gacutil
实用程序显然能够找到完全限定的名称:
C:\test> gacutil /l System.Windows.Forms Microsoft (R) .NET 全局程序集缓存实用程序。版本 4.0.30319.1 版权所有 (c) 微软公司。版权所有。 全局程序集缓存包含以下程序集: System.Windows.Forms,版本=2.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089,处理器架构=MSIL System.Windows.Forms,版本=1.0.3300.0,文化=中性,PublicKeyToken=b77a5c561934e089 System.Windows.Forms,版本=1.0.5000.0,文化=中性,PublicKeyToken=b77a5c561934e089 System.Windows.Forms,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089,处理器架构=MSIL 项目数 = 4 C:\测试> _
但是,如前所述,我不想硬编码任何内容:C++ 代码中硬编码的 .NET 信息不应超过顶部注释中显示的 C# 源代码中的内容,以及支持的最低 .NET 版本。
TIA.,