error C2440: 'function' : cannot convert f

2020-04-20 15:34发布

While trying to host the CLR, I keep getting this:

error C2440: 'function' : cannot convert from 'const IID' to 'DWORD'

My code:

ICLRRuntimeHost *host = NULL;
HRESULT result = CorBindToRuntime(NULL, L"wks", CLSID_CLRRuntimeHost, 
    IID_ICLRRuntimeHost, (PVOID*)&host);

This is in C, by the way. Not C++.

EDIT: When I compile this with C++, it works just fine. Shouldn't it behave the same in either language?

标签: c clr-hosting
2条回答
霸刀☆藐视天下
2楼-- · 2020-04-20 16:08

From guiddef.h:

#ifndef _REFIID_DEFINED
#define _REFIID_DEFINED
#ifdef __cplusplus
#define REFIID const IID &
#else
#define REFIID const IID * __MIDL_CONST
#endif
#endif

#ifndef _REFCLSID_DEFINED
#define _REFCLSID_DEFINED
#ifdef __cplusplus
#define REFCLSID const IID &
#else
#define REFCLSID const IID * __MIDL_CONST
#endif
#endif

In other words, in C++, those two are references, and in C, they are pointers. You need to use:

ICLRRuntimeHost *host = NULL;
HRESULT result = CorBindToRuntime(NULL, L"wks", &CLSID_CLRRuntimeHost,
    &IID_ICLRRuntimeHost, (PVOID*)&host);
查看更多
Fickle 薄情
3楼-- · 2020-04-20 16:14

The last parameter of CorBindToRuntime is defined as LPVOID*, not PVOID*. Maybe thats the problem?

HRESULT CorBindToRuntime (
        [in]  LPCWSTR     pwszVersion, 
        [in]  LPCWSTR     pwszBuildFlavor, 
        [in]  REFCLSID    rclsid, 
        [in]  REFIID      riid, 
        [out] LPVOID FAR  *ppv
);
查看更多
登录 后发表回答