Click here to Skip to main content
15,912,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am running the following code for deadlock detection but always I find the error inforing that RunTime and shim failed to start. Why is this issue occurring to me and how to solve this problem? I will be glad if someone could help me in this regard.

C++
int _tmain(int argc, _TCHAR* argv[])
{
	// Bind to the runtime.
	ICLRRuntimeHost *pClrHost = NULL;
	HRESULT hrCorBind = CorBindToRuntimeEx(
		NULL,   // Load the latest CLR version available
		L"wks", // Workstation GC ("wks" or "svr" overrides)
		0,      // No flags needed
		CLSID_CLRRuntimeHost,
		IID_ICLRRuntimeHost,
		(PVOID*)&pClrHost);
    CheckFail(hrCorBind, "Bind to runtime failed (0x%x)");
	// Construct our host control object.
    DHHostControl *pHostControl = new DHHostControl(pClrHost);
	if (!pHostControl)
        Fail("Host control allocation failed");
	pClrHost->SetHostControl(pHostControl);
	// Now, begin the CLR.
	HRESULT hrStart = pClrHost->Start();
    if (hrStart == S_FALSE)
        _ASSERTE(!L"Runtime already started; probably OK to proceed");
    else
        CheckFail(hrStart, "Runtime startup failed (0x%x)");
    // Construct the shim path (i.e. shim.exe).
	WCHAR wcShimPath[MAX_PATH];
    if (!GetCurrentDirectoryW(MAX_PATH, wcShimPath))
        CheckFail(HRESULT_FROM_WIN32(GetLastError()), "GetCurrentDirectory failed (0x%x)");
    wcsncat_s(wcShimPath, sizeof(wcShimPath) / sizeof(WCHAR), L"\\shim.exe", MAX_PATH - wcslen(wcShimPath) - 1);
    // Gather the arguments to pass to the shim.
    LPWSTR wcShimArgs = NULL;
    if (argc > 1)
    {
        SIZE_T totalLength = 1; // 1 is the NULL terminator
        for(int i = 1; i < argc; i++)
        {
            // TODO: add characters for quotes around args w/ spaces inside them
            if (i != 1)
                totalLength++; // add a space between args
            totalLength += _tcslen(argv[i]) + 1;
		}
        wcShimArgs = new WCHAR[totalLength];
        wcShimArgs[0] = '\0';
 
        for(int i = 1; i < argc; i++)
        {
            if (i != 1)
                wcscat_s(wcShimArgs, totalLength, L" ");
            wcsncat_s(wcShimArgs, totalLength, argv[i], wcslen(argv[i]));
		}
	}
    if (wcShimArgs == NULL)
        Fail("Missing program path (host.exe <exePath>)\r\n");
	// And execute the program...
    DWORD retVal;
    HRESULT hrExecute = pClrHost->ExecuteInDefaultAppDomain(
        wcShimPath,
        L"Shim",
        L"Start",
        wcShimArgs,
        &retVal);
    CheckFail(hrExecute, "Execution of shim failed (0x%x)\r\n");
    if (wcShimArgs)
        delete wcShimArgs;
    // Stop the CLR and cleanup.
    pHostControl->ShuttingDown();
    pClrHost->Stop();
    pClrHost->Release();
_getch();
	return retVal;
}
Posted
Updated 9-Jun-10 22:53pm
v4

1 solution

You haven't initialised COM[^]. Given that the CLR hosting API is implemented as COM objects this might be a good start. See the CoInitialize[^] or CoInitializeEx[^] functions.

Add the following to the beginning:
CoInitialize(NULL);

And this to the end:
CoUninitialize();
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900