locked
Problem in the DLL calls CoCreateInstance RRS feed

  • Question

  • Hi, All

    MyDll projects in the DllMain calls CoCreateInstance to create an instance of a COM component object has been returned E_NOINTERFACE, and in the EXE project called CoCreateInstance will not be any problems.

    Premise CoInitialize (NULL) initialization not any problems.

    1, I try to use the global class to call CoCreateInstance, will result in deadlock
    2, try to use DLL export other interfaces, such as Init, Init internal call CoCreateInstance in no deadlock, but an error occurs, the return value is E_NOINTERFACE.
    3, tried a thread in DllMain to call CoCreateInstance beginthread instance object, does not deadlock, but the return value is E_NOINTERFACE, not to create specific objects.

    Since the project requires the DLL calls CoCreateInstance to create class instance to operate, has been created to return E_NOINTERFACE, but does not appear in the EXE such a thing, what other way?

    COM DLL is their written arguments in MyDll call has also been checked, no problem (with the EXE which calls the same order).

     

    CoInitialize (NULL);
    IClassFactory * pcf = NULL;
    
    HRESULT hr = CoGetClassObject (CLSID_TEST, CLSCTX_INPROC_SERVER, 0, IID_IClassFactory, (void **) & pcf);
    ICA * pcs = NULL;
    hr = pcf-> CreateInstance (0, IID_ICA, (void **) & pcs) 

     

    The above code in the EXE project to create ICA object, and in the DLL project Why create pcs total is NULL, the return value is E_NOINTERFACE. The EXE project is S_OK.

    Find DLL solution. It can create success.

    OleView can find in their own writing in the COM DLL.



    • Edited by cppcoffee Thursday, October 6, 2011 3:05 AM
    Thursday, October 6, 2011 3:01 AM

All replies

  • MyDll projects in the DllMain calls CoCreateInstance to create an instance of a COM component object has been returned E_NOINTERFACE...

     

    "Calling functions other than TLS, object-creation, and file functions may result in problems that are difficult to diagnose. For example, calling User, Shell, COM, RPC, and Windows Sockets functions (or any functions that call these functions) can cause access violation errors, because their DLLs call LoadLibrary to load other system components. While it is acceptable to create synchronization objects in DllMain, you should not perform synchronization in DllMain (or a function called by DllMain) because all calls to DllMain are serialized. Waiting on synchronization objects in DllMain can cause a deadlock." (c) MSDN

     

    Thursday, October 6, 2011 4:18 AM
  • Now CreateInstance been wrong, how do I change it
    Thursday, October 6, 2011 4:29 AM
  • Your code with IClassFactory::CreateInstance is valid, but its placement is incorrect. Try to place it outside the DllMain.
    Thursday, October 6, 2011 5:15 AM
  • Your code with IClassFactory::CreateInstance is valid, but its placement is incorrect. Try to place it outside the DllMain.


    I try to change to another place CoCreateInstance (not in Dllmain). Is like the rest of CreateInstance
    Failure.

    Detail about my project it, it is a LSP DLL, in the WSAStartup call the CoCreateInstance, but has been the creation failed.

    WSAStartup routine the following code:

     

    int WSPAPI WSPStartup(
        IN WORD  wVersionRequested, 
        OUT LPWSPDATA  lpWSPData, 
        IN LPWSAPROTOCOL_INFO  lpProtocolInfo, 
        IN WSPUPCALLTABLE  UpcallTable, 
        OUT LPWSPPROC_TABLE  lpProcTable 
        )
    {
        int ret = WSAEPROVIDERFAILEDINIT;
    
        MessageBox(NULL, _T("TEST"), _T("DBG"), MB_OK);
    
    ..................................
    
        CoInitialize(NULL);
    
        IClassFactory *pCF=NULL;
        HRESULT hr=::CoGetClassObject(
            CLSID_IWzdSrv,		// name of dll to load
            CLSCTX_INPROC_SERVER,// use dll
            NULL,				// for DCOM, a COSERVERINFO structure that id's the remote server
            // more typically set using OLEView
            IID_IClassFactory,	// the class factory interface (all COM DLL/EXE's must have this interface)
            (LPVOID*)&pCF); 
    
        IUnknown* iWzd = NULL;
        hr = pCF->CreateInstance(
            NULL,				// aggregated COM object (none)
            IID_IWzd,			// class to create and object of
            (LPVOID*) &iWzd);	// returned object pointer
    
    .........................
    
        return ret;
    }

    hr = pCF->CreateInstance creation failed.

     

    Thursday, October 6, 2011 5:39 AM
  • And when are you calling the WSPStartup? Inside DllMain?

     

    Another reason can be associated with marshalling problem. Your DLL can be loaded into thread with apartmant already initialized. You can check the return value of CoInitialize(NULL). So, in this case, the apartmant model isn't changed, and the marshalling code is required. The EXE can load your DLL inside STA, and, in this case, no marshalling code is required.

    So, check the proxy|stab DLL marshalling or TLB marshalling.

     

    Thursday, October 6, 2011 5:50 AM
  • WSAStartup is called Socket LSP need to export to the program.
    Call CoInitialize (NULL), its return value is S_OK.
    I will CoInitialize (NULL) to HRESULT hr = CoInitializeEx (NULL, COINIT_MULTITHREADED); call when the value of hr is S_OK.

    However, CreateInstance is wrong. Hr value is E_NOINTERFACE.

    But I can see in OleView in to the interface.
    • Edited by cppcoffee Thursday, October 6, 2011 6:47 AM
    Thursday, October 6, 2011 6:47 AM
  • Sorry, I have no suggestions anymore.
    Thursday, October 6, 2011 6:53 AM
  • Sorry, I have no suggestions anymore.
     Thx Vi2

    You are very enthusiastic
    • Edited by cppcoffee Thursday, October 6, 2011 7:26 AM
    Thursday, October 6, 2011 7:25 AM