托管 CLR - 错误参数
我尝试在 C++ 应用程序中托管 CLR,但在调用托管应用程序的入口点时遇到问题。 入口点像往常一样定义:
static void Main(string[] args)
这是实际的 C++ 代码:
CComPtr<_MethodInfo> entryPoint;
hr = assembly->get_EntryPoint(&entryPoint); // this works just fine
if (FAILED(hr))
return hr;
SAFEARRAY *args =
SafeArrayCreateVector(VT_VARIANT, 1, 1); // create an array of the length of 1 ( Main(string[]) )
int argc;
LPWSTR cmdLine = GetCommandLineW();
LPWSTR *argv = CommandLineToArgvW(cmdLine, &argc); // get an array of arguments to this function
VARIANT vtPsa;
vtPsa.vt = (VT_ARRAY | VT_BSTR);
vtPsa.parray = SafeArrayCreateVector(VT_BSTR, 1, argc); // create an array of strings
for (long i = 0; i < argc; i++)
{
SafeArrayPutElement(vtPsa.parray, &i, SysAllocString(argv[i])); // insert the string from argv[i] into the safearray
}
long idx[1] = {0};
SafeArrayPutElement(args, idx, &vtPsa); // insert an array of BSTR into the VT_VARIANT args array
VARIANT obj, result;
VariantInit(&obj);
VariantInit(&result);
try
{
hr = entryPoint->Invoke_3(obj, args, &result); // call the entry point
}
catch(_com_error ex)
{
MessageBox(NULL, ex.ErrorMessage(), "Error", 0);
}
if(FAILED(hr))
{
hr = hr; // added just so I can set a breakpoint
}
我得到的错误代码是 -2146233032,根据 corerror.h 对应于:
十进制 -2146233032 / 十六进制 0x80131538:
COR_E_SAFEARRAYRANKMISMATCH
之间发生了不匹配 数组的运行时等级和等级 记录在元数据中。
任何人都可以看到问题吗?
I'm trying to host the CLR inside my C++ application and I'm having problems invoking the entry point of the managed application.
The entry point is defined as usual:
static void Main(string[] args)
And here's the actual C++ code:
CComPtr<_MethodInfo> entryPoint;
hr = assembly->get_EntryPoint(&entryPoint); // this works just fine
if (FAILED(hr))
return hr;
SAFEARRAY *args =
SafeArrayCreateVector(VT_VARIANT, 1, 1); // create an array of the length of 1 ( Main(string[]) )
int argc;
LPWSTR cmdLine = GetCommandLineW();
LPWSTR *argv = CommandLineToArgvW(cmdLine, &argc); // get an array of arguments to this function
VARIANT vtPsa;
vtPsa.vt = (VT_ARRAY | VT_BSTR);
vtPsa.parray = SafeArrayCreateVector(VT_BSTR, 1, argc); // create an array of strings
for (long i = 0; i < argc; i++)
{
SafeArrayPutElement(vtPsa.parray, &i, SysAllocString(argv[i])); // insert the string from argv[i] into the safearray
}
long idx[1] = {0};
SafeArrayPutElement(args, idx, &vtPsa); // insert an array of BSTR into the VT_VARIANT args array
VARIANT obj, result;
VariantInit(&obj);
VariantInit(&result);
try
{
hr = entryPoint->Invoke_3(obj, args, &result); // call the entry point
}
catch(_com_error ex)
{
MessageBox(NULL, ex.ErrorMessage(), "Error", 0);
}
if(FAILED(hr))
{
hr = hr; // added just so I can set a breakpoint
}
The errorcode I'm getting is -2146233032, which according to corerror.h corresponds to:
for decimal -2146233032 / hex
0x80131538 :
COR_E_SAFEARRAYRANKMISMATCH
A mismatch has occured between the
runtime rank of the array and the rank
recorded in the metadata.
Can anyone see the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这两种情况下,SafeArrayCreateVector 的第二个参数不应该都是 0 吗? MSDN 将该值列为“数组的下限。可以为负数”。
Shouldn't the second parameter to SafeArrayCreateVector be 0 in both cases? MSDN lists that value as "The lower bound for the array. Can be negative."