Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CoInitialize has not been called

CoInitialize has not been called

Scheduled Pinned Locked Moved C / C++ / MFC
help
5 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MsmVc
    wrote on last edited by
    #1

    Hi All I am getting error CoInitialize has not been called.Code is here

    void Ctest::OnCopyData()
    {
    ::CoInitialize(NULL);
    COleDataSource* pSource = CopyTextList();
    if (!pSource)
    return;

    pSource->SetClipboard();
    

    }

    COleDataSource* Ctest::CopyTextFromList()
    {
    USES_CONVERSION;

    CSharedFile sf(GMEM\_MOVEABLE | GMEM\_DDESHARE | GMEM\_ZEROINIT);
     CString str;
    int nPos=m\_test.GetItemCount();
    for(int i=0;i<nPos;i++)
    {
    	CString GetData=m\_test.GetItemText(i,0);
    	str+=GetData;
    	str += \_T("\\r\\n");
        sf.Write(T2A(str.GetBuffer(1)), str.GetLength());
        str.ReleaseBuffer();
    }
    
     char c = '\\0';
     sf.Write(&c, 1);
    
      DWORD dwLen = (DWORD) sf.GetLength();
    HGLOBAL hMem = sf.Detach();
    if (!hMem)
        return NULL;
    
    hMem = ::GlobalReAlloc(hMem, dwLen, GMEM\_MOVEABLE | GMEM\_DDESHARE | GMEM\_ZEROINIT);
    if (!hMem)
        return NULL;
    
    // Cache data
    COleDataSource\* pSource = new COleDataSource();
    pSource->CacheGlobalData(CF\_TEXT, hMem);
    
    return pSource;
    

    }

    Please help me

    K A N S 4 Replies Last reply
    0
    • M MsmVc

      Hi All I am getting error CoInitialize has not been called.Code is here

      void Ctest::OnCopyData()
      {
      ::CoInitialize(NULL);
      COleDataSource* pSource = CopyTextList();
      if (!pSource)
      return;

      pSource->SetClipboard();
      

      }

      COleDataSource* Ctest::CopyTextFromList()
      {
      USES_CONVERSION;

      CSharedFile sf(GMEM\_MOVEABLE | GMEM\_DDESHARE | GMEM\_ZEROINIT);
       CString str;
      int nPos=m\_test.GetItemCount();
      for(int i=0;i<nPos;i++)
      {
      	CString GetData=m\_test.GetItemText(i,0);
      	str+=GetData;
      	str += \_T("\\r\\n");
          sf.Write(T2A(str.GetBuffer(1)), str.GetLength());
          str.ReleaseBuffer();
      }
      
       char c = '\\0';
       sf.Write(&c, 1);
      
        DWORD dwLen = (DWORD) sf.GetLength();
      HGLOBAL hMem = sf.Detach();
      if (!hMem)
          return NULL;
      
      hMem = ::GlobalReAlloc(hMem, dwLen, GMEM\_MOVEABLE | GMEM\_DDESHARE | GMEM\_ZEROINIT);
      if (!hMem)
          return NULL;
      
      // Cache data
      COleDataSource\* pSource = new COleDataSource();
      pSource->CacheGlobalData(CF\_TEXT, hMem);
      
      return pSource;
      

      }

      Please help me

      K Offline
      K Offline
      KarstenK
      wrote on last edited by
      #2

      CoInitialize(NULL) does some stuff, so you better call it some time (100ms) before you need it to be finished. Tip: you only need to call it one time per thread/app. Best in some startup code liek oninitdialog. Press F1 for help or google it. Greetings from Germany

      1 Reply Last reply
      0
      • M MsmVc

        Hi All I am getting error CoInitialize has not been called.Code is here

        void Ctest::OnCopyData()
        {
        ::CoInitialize(NULL);
        COleDataSource* pSource = CopyTextList();
        if (!pSource)
        return;

        pSource->SetClipboard();
        

        }

        COleDataSource* Ctest::CopyTextFromList()
        {
        USES_CONVERSION;

        CSharedFile sf(GMEM\_MOVEABLE | GMEM\_DDESHARE | GMEM\_ZEROINIT);
         CString str;
        int nPos=m\_test.GetItemCount();
        for(int i=0;i<nPos;i++)
        {
        	CString GetData=m\_test.GetItemText(i,0);
        	str+=GetData;
        	str += \_T("\\r\\n");
            sf.Write(T2A(str.GetBuffer(1)), str.GetLength());
            str.ReleaseBuffer();
        }
        
         char c = '\\0';
         sf.Write(&c, 1);
        
          DWORD dwLen = (DWORD) sf.GetLength();
        HGLOBAL hMem = sf.Detach();
        if (!hMem)
            return NULL;
        
        hMem = ::GlobalReAlloc(hMem, dwLen, GMEM\_MOVEABLE | GMEM\_DDESHARE | GMEM\_ZEROINIT);
        if (!hMem)
            return NULL;
        
        // Cache data
        COleDataSource\* pSource = new COleDataSource();
        pSource->CacheGlobalData(CF\_TEXT, hMem);
        
        return pSource;
        

        }

        Please help me

        N Offline
        N Offline
        Niklas L
        wrote on last edited by
        #3

        Are you aware CopyTextList is called, but you supplied code for CopyText**From**List? Also you only need CoInitialize once. Do that in your start up routine (CWinApp::InitInstance if it's MFC)

        home

        1 Reply Last reply
        0
        • M MsmVc

          Hi All I am getting error CoInitialize has not been called.Code is here

          void Ctest::OnCopyData()
          {
          ::CoInitialize(NULL);
          COleDataSource* pSource = CopyTextList();
          if (!pSource)
          return;

          pSource->SetClipboard();
          

          }

          COleDataSource* Ctest::CopyTextFromList()
          {
          USES_CONVERSION;

          CSharedFile sf(GMEM\_MOVEABLE | GMEM\_DDESHARE | GMEM\_ZEROINIT);
           CString str;
          int nPos=m\_test.GetItemCount();
          for(int i=0;i<nPos;i++)
          {
          	CString GetData=m\_test.GetItemText(i,0);
          	str+=GetData;
          	str += \_T("\\r\\n");
              sf.Write(T2A(str.GetBuffer(1)), str.GetLength());
              str.ReleaseBuffer();
          }
          
           char c = '\\0';
           sf.Write(&c, 1);
          
            DWORD dwLen = (DWORD) sf.GetLength();
          HGLOBAL hMem = sf.Detach();
          if (!hMem)
              return NULL;
          
          hMem = ::GlobalReAlloc(hMem, dwLen, GMEM\_MOVEABLE | GMEM\_DDESHARE | GMEM\_ZEROINIT);
          if (!hMem)
              return NULL;
          
          // Cache data
          COleDataSource\* pSource = new COleDataSource();
          pSource->CacheGlobalData(CF\_TEXT, hMem);
          
          return pSource;
          

          }

          Please help me

          A Offline
          A Offline
          Aescleal
          wrote on last edited by
          #4

          Try helping yourself. Here are some things to try: - Find which line is causing the error - When you've done that step through the code in a debugger and see if the function with CoInitialize() in is executed is before that - Check that for every CoInitialize you've got a CoUninitialize Cheers, Ash

          1 Reply Last reply
          0
          • M MsmVc

            Hi All I am getting error CoInitialize has not been called.Code is here

            void Ctest::OnCopyData()
            {
            ::CoInitialize(NULL);
            COleDataSource* pSource = CopyTextList();
            if (!pSource)
            return;

            pSource->SetClipboard();
            

            }

            COleDataSource* Ctest::CopyTextFromList()
            {
            USES_CONVERSION;

            CSharedFile sf(GMEM\_MOVEABLE | GMEM\_DDESHARE | GMEM\_ZEROINIT);
             CString str;
            int nPos=m\_test.GetItemCount();
            for(int i=0;i<nPos;i++)
            {
            	CString GetData=m\_test.GetItemText(i,0);
            	str+=GetData;
            	str += \_T("\\r\\n");
                sf.Write(T2A(str.GetBuffer(1)), str.GetLength());
                str.ReleaseBuffer();
            }
            
             char c = '\\0';
             sf.Write(&c, 1);
            
              DWORD dwLen = (DWORD) sf.GetLength();
            HGLOBAL hMem = sf.Detach();
            if (!hMem)
                return NULL;
            
            hMem = ::GlobalReAlloc(hMem, dwLen, GMEM\_MOVEABLE | GMEM\_DDESHARE | GMEM\_ZEROINIT);
            if (!hMem)
                return NULL;
            
            // Cache data
            COleDataSource\* pSource = new COleDataSource();
            pSource->CacheGlobalData(CF\_TEXT, hMem);
            
            return pSource;
            

            }

            Please help me

            S Offline
            S Offline
            Sauro Viti
            wrote on last edited by
            #5

            It's unuseful to call CoInitialize each time you need to use some COM object; call CoInitialize only once somewhere in your startup code instead. Be aware that each call to CoInitialize should be balanced with a call to CoUninitialize. From the code snippet you posted, it seems that you wrote your application using MFC, then the best places where put the calls are inside CWinApp::InitInstance and CWinApp::ExitInstance.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups