Hosting CLR - Bad parameters
Asked Answered
D

1

3

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?

Decompound answered 2/12, 2008 at 19:11 Comment(0)
P
3

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."

Predestine answered 29/1, 2009 at 1:10 Comment(1)
After setting the second parameter to 0 in both cases the error message changed to: -2147352562 / hex 0x8002000e : COR_E_TARGETPARAMCOUNT corerror.h There was a mismatch between number of arguments provided and the number expected.Decompound

© 2022 - 2024 — McMap. All rights reserved.