托管 CLR - 错误参数

发布于 2024-07-09 20:29:49 字数 1594 浏览 8 评论 0原文

我尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤痕我心 2024-07-16 20:29:49

在这两种情况下,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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文