Solved

Where can I find CoInitializeEx(...)

Posted on 2001-08-30
2
461 Views
Last Modified: 2013-11-20
I am using the clipboard for the first time and following
Swanke's book "Visual C++ MFC Programming by Example.",
example 76.

This serializes my doc to the clipboard:

    ...
    CSharedFile file(GMEM_MOVEABLE|GMEM_DDESHARE);

    CArchive storeArchive(&file
     , CArchive::store | CArchive::bNoFlushOnDelete);
    TRY {
        Serialize(storeArchive, m_selections);
        // flush and close the memory file
        storeArchive.Close();
    }
    CATCH_ALL(e)
    {
       ...
    }
    END_CATCH_ALL

    // pick out the memory file
    HGLOBAL hMem = file.Detach();
    if(!hMem) return;

    // now stick this file into the clip board. the data source object
    // will be deleted when the clipboard is emptied
    COleDataSource* pDS = new COleDataSource();
    pDS->CacheGlobalData(theApp.GetDocClipboardFormat(), hMem);
    pDS->SetClipboard();
}


When I get the the clip board part, I get an exception thrown with the message that I haven't called CoInitialize().

When I look in the doc, it says that I shouldn't use this in any new apps but should call CoInitializeEx(NULL). My problem is that I can't find this routine. No header file in MFC 6.0 seems to contain it and their is only one reference to CoInitialize(...) when I do a find on the MFC source code.

I have put the call to CoInitialize(NULL) in the InitInstance of the main app and that seems to work.

However, my questions are:

1. Should I be using CoInitializeEx(NULL)?

2. If so, which header file defines it?



0
Comment
Question by:emitchell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
JackThornton earned 50 total points
ID: 6442280
(1) There is nothing inherently wrong with using CoInitialize; however, you will only be able to initialize a single-threaded apartment that way. To declare a multi-threaded apartment thread, you need to use CoInitializeEx with COINIT_MULTITHREADED.

(2) OBJBASE.H

- jack
0
 

Author Comment

by:emitchell
ID: 6442491
That's where the definition is but when I included this into my main app file, CoInitializeEx() was still missing. It turns out the the definition is surrounded by:

#if (_WIN32_WINNT >= 0x0400 ) || defined(_WIN32_DCOM) // DCOM
WINOLEAPI  CoInitializeEx(LPVOID pvReserved, DWORD dwCoInit);
#endif // DCOM

When I define _WIN32_DCOM to make it active my main app compiles correctly.

I assume the MSVisual Studio wizard would have defined this correctly if I had asked for the right thing when I had created the project. I didn't realise that the clipboard needed access to DCOM.

Thanks for the help. Solved my problem
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 137
Generic progress indicator 6 121
Execute multiple curl cmds with sleep and send output to file 10 121
mapAB Challlenge 35 186
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question