我试图在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,它对应于:



谁能看到这个问题?

最佳答案

在两种情况下,SafeArrayCreateVector的第二个参数都不应为0吗? MSDN将该值列出为“数组的下限。可以为负。”

关于c++ - 托管CLR-错误的参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/335085/

10-17 00:08