CoInitialize has not been called
-
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
-
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
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
-
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
-
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
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
-
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
It's unuseful to call
CoInitialize
each time you need to use some COM object; callCoInitialize
only once somewhere in your startup code instead. Be aware that each call toCoInitialize
should be balanced with a call toCoUninitialize
. From the code snippet you posted, it seems that you wrote your application using MFC, then the best places where put the calls are insideCWinApp::InitInstance
andCWinApp::ExitInstance
.