2008-12-02 45 views
3

我想在我的C++應用程序內託管CLR,並且遇到了調用託管應用程序入口點的問題。 入口點被定義爲平常:主機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
數組中的 運行時間級別與元數據中記錄的級別 之間發生不匹配。

任何人都可以看到問題嗎?

回答

3

在這兩種情況下SafeArrayCreateVector的第二個參數不應該爲0嗎? MSDN將該值列爲「數組的下限,可能爲負數」。

+0

你可能是對的,當我回家時會嘗試。 – arul 2009-01-29 01:51:46