Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoCreateInstance returns "Class not registered"

Tags:

c++

c#

dll

managed

I've been at this for hours now and it's starting to get upsetting. :(

I have a COM DLL that I created in .NET 4.0, that I want to be able to access with a legacy application made in VC++ 6. Its visible to COM, I already successfully created the TLB and SNK files, moved it over to the machine with the legacy C++ code, #imported the TLB, everything compiles fine.

Ran the following:

RegAsm ProtracFunctions.dll /codebase

gacutil /i ProtracFunctions.dll

They both are successful.

When I go to launch my application, as soon as I hit CoCreateInstance I'm given the message "Class not registered".

I noted in RegEdit that my DLL does appear to be registered. Thats when I did some research, downloaded ProcMon, and realized that its looking at different locations. The CLSID is slightly different, and if I try to modify the ProtracFunctions.reg file (to use the GUID that ProcMon is picking up) that RegAsm gives me, and it DOES make an impact in ProcMon (more "SUCCESS" messages than "NAME NOT FOUND" messages at least), but I'm missing it seems, a ton of registry locations. Such as the key "TreatAs", "InprocServerX86", etc.

If anyone could tell me:

A) What I'm doing wrong in the first place

or

B) A list of exactly what registry values are added when you "register" a COM DLL so I can go in there and manually do it myself. (not ideal, I realize).

TIA!

My code:

CoInitialize(NULL);
CComQIPtr <ProtracFunctions::IDockingStation> spTestCom;
HRESULT hRes = spTestCom.CoCreateInstance(CLSID_ProtracDCS, 0, CLSCTX_ALL);

if (SUCCEEDED (hRes))
{
    printf("Created the instance");

    unsigned char Ret;
    unsigned char ErrCode;
    SAFEARRAY *pSA;

    spTestCom->DockConnect(3, 19200, &Ret);
    spTestCom->GetTagReads(1, &ErrCode, &pSA); 

    spTestCom->PowerOffReader(1, &Ret);
    spTestCom->DockDisconnect();

    spTestCom.Release ();
}
else
{
   _com_error error(hRes);
   LPCTSTR errorText = error.ErrorMessage();

   AfxMessageBox(errorText);

   //automatic cleanup when error goes out of scope
}

Added Notes:

The development machine is running Win XP 32 bit, and the "machine" with the legacy application is actually a Virtual Machine on the dev computer, also running XP.

Also, when I run my application, and I choose my test menu item that fires the above code, the first time I get the error "Class not registered" and if I click it again after that I'm seeing: "No such interface supported" ... Very odd.

like image 219
kogh Avatar asked Mar 19 '12 19:03

kogh


2 Answers

I think you need to embedded a manifest in your assembly. See this URL. http://blogs.msdn.com/b/cheller/archive/2006/08/24/718757.aspx

You can also make it works without having to register the assembly (Regasm.exe) with this code.

{
    DWORD cookie;
    ACTCTX actctx = {sizeof(actctx)};
    actctx.lpSource = L"YOUR ASSEMBLY FULL PATH";
    actctx.lpResourceName = MAKEINTRESOURCE(1); //MAYBE 2 FOR DLL
    actctx.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID;   
    HANDLE hActCtx = CreateActCtx(&actctx);
    BOOL fOK = ActivateActCtx(hActCtx, &cookie);
    if (!fOK)
        return E_FAIL;

    CComPtr<CSharpComInterface::ITestCSharpInterface> spCSharpTest;
    HRESULT hr = spCSharpTest.CoCreateInstance(__uuidof(CSharpComInterface::CoClass));

    if (hr != S_OK)
    {
        cout << "Failed to create instance CSharpComInterface::CoClass" <<endl;
    }
    else
    {
        cout << "Load Successfully : CSharpComInterface::CoClass" << endl;
        spCSharpTest->TestMethod();
    }

    DeactivateActCtx(0, cookie);
    ReleaseActCtx(hActCtx);
}
like image 60
Aing Avatar answered Oct 14 '22 04:10

Aing


This entry on Regasm.exe in MSDN says you should not use /codebase option if your assembly is in the GAC.

like image 1
Jacob Seleznev Avatar answered Oct 14 '22 06:10

Jacob Seleznev