views:

328

answers:

1

Hello,

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?

+1  A: 

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

You may be right, will try when I get home.
arul
After setting the second parameter to 0 in both cases the error message changed to:-2147352562 / hex 0x8002000e : COR_E_TARGETPARAMCOUNT corerror.hThere was a mismatch between number of arguments providedand the number expected.
arul
Never mind, your solution worked.
arul