Here's a quick background on what I'm trying to acomplish:
I'm trying to drag drop virtual files by deriving System::Windows::Forms::Li
stView in my own class and using unmanaged code to delay render.
Here's my OnItemDrag override:
1 virtual void OnItemDrag(ItemDragEventAr
gs ^e) override
2 {
3 UINT uFileCount = this->SelectedItems->Count
;
4 UINT uBuffSize = sizeof(FILEGROUPDESCRIPTOR
) +
5 (uFileCount-1) * sizeof(FILEDESCRIPTOR);
6 HGLOBAL hFileDescriptor = GlobalAlloc (
7 GHND | GMEM_SHARE, uBuffSize );
8 if(hFileDescriptor)
9 {
10 FILEGROUPDESCRIPTOR* pGroupDescriptor =
11 (FILEGROUPDESCRIPTOR*) GlobalLock ( hFileDescriptor );
12 if(pGroupDescriptor)
13 {
14 FILEDESCRIPTOR* pFileDescriptorArray =
15 (FILEDESCRIPTOR*)((LPBYTE)
pGroupDesc
riptor + sizeof(UINT));
16 pGroupDescriptor->cItems = uFileCount;
17 int index = 0;
18 m_pDataSrc->m_Files->Remov
eAll();
19 for (int i = 0; i < uFileCount; ++i)
20 {
21 ZeroMemory(&pFileDescripto
rArray[ind
ex],
22 sizeof(FILEDESCRIPTOR));
23
24 pin_ptr<const wchar_t> wch = PtrToStringChars(this->Sel
ectedItems
[i]->Text)
;
25
26 CString fileName(wch);
27
28 lstrcpy ( pFileDescriptorArray[index
].cFileNam
e, fileName );
29
30 m_pDataSrc->m_Files->Add(p
FileDescri
ptorArray[
index].cFi
leName);
31 pFileDescriptorArray[index
].dwFlags = FD_FILESIZE|FD_ATTRIBUTES;
32 pFileDescriptorArray[index
].nFileSiz
eLow = 512;
33 pFileDescriptorArray[index
].nFileSiz
eHigh = 0;
34 pFileDescriptorArray[index
].dwFileAt
tributes = FILE_ATTRIBUTE_NORMAL;
35 index++;
36 }
37 }
38 else
39 {
40 GlobalFree ( hFileDescriptor );
41 }
42 }
43 GlobalUnlock ( hFileDescriptor );
44
45 FORMATETC etcDescriptor = {
46 RegisterClipboardFormat(CF
STR_FILEDE
SCRIPTOR),
47 NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
48 m_pDataSrc->CacheGlobalDat
a ( RegisterClipboardFormat(
49 CFSTR_FILEDESCRIPTOR), hFileDescriptor, &etcDescriptor );
50
51 FORMATETC etcContents = {
52 RegisterClipboardFormat(CF
STR_FILECO
NTENTS),
53 NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
54
55 m_pDataSrc->DelayRenderFil
eData(
56 RegisterClipboardFormat(CF
STR_FILECO
NTENTS),
57 &etcContents);
58
59 DROPEFFECT dwEffect = m_pDataSrc->DoDragDrop (
60 DROPEFFECT_COPY | DROPEFFECT_MOVE );
61
62 if(dwEffect == DROPEFFECT_NONE )
63 {
64 GlobalFree( hFileDescriptor );
65 }
66 }
I'm getting an AccessViolationException on line 59 - the call to DoDragDrop.
Here's the definition of m_pDataSrc:
protected:
CMyOleDataSource * m_pDataSrc;
It gets instanciated using new in the class constructor.
Here is the implementation of CMyOleDataSource:
[.h file]
1
2 #pragma once
3
4 #include <afxwin.h> // MFC core and standard components
5 #include <afxext.h> // MFC extensions
6
7 #include <afxdisp.h> // MFC Automation classes
8
9 #ifndef _AFX_NO_OLE_SUPPORT
10 #include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
11 #endif
12 #ifndef _AFX_NO_AFXCMN_SUPPORT
13 #include <afxcmn.h> // MFC support for Windows Common Controls
14 #endif // _AFX_NO_AFXCMN_SUPPORT
15 #include <afxole.h>
16
17 // CMyOleDataSource command target
18
19 class CMyOleDataSource : public COleDataSource
20 {
21 DECLARE_DYNAMIC(CMyOleData
Source)
22
23 public:
24 CMyOleDataSource();
25 virtual ~CMyOleDataSource() { delete m_Files; }
26 virtual BOOL OnRenderFileData(LPFORMATE
TC lpFormatEtc,CFile* pFile);
27 CStringArray *m_Files;
28
29 protected:
30 DECLARE_MESSAGE_MAP()
31 };
[.cpp file]
1 // MyOleDataSource.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "MyOleDataSource.h"
6
7
8 // CMyOleDataSource
9
10 IMPLEMENT_DYNAMIC(CMyOleDa
taSource, COleDataSource)
11
12
13 CMyOleDataSource::CMyOleDa
taSource()
14 {
15 m_Files = new CStringArray();
16 }
17
18 BEGIN_MESSAGE_MAP(CMyOleDa
taSource, COleDataSource)
19 END_MESSAGE_MAP()
20
21
22
23 // CMyOleDataSource message handlers
24
25 BOOL CMyOleDataSource::OnRender
FileData(
26 LPFORMATETC lpFormatEtc,CFile* pFile)
27 {
28 if(lpFormatEtc->cfFormat ==
29 RegisterClipboardFormat(CF
STR_FILECO
NTENTS))
30 {
31 HGLOBAL hGlob = NULL;
32 const int buffSize = 512;
33 hGlob = GlobalAlloc(GMEM_FIXED, buffSize);
34 if(hGlob)
35 {
36 LPBYTE pBytes = (LPBYTE)GlobalLock(hGlob);
37 if(pBytes)
38 {
39 memset(pBytes, (int) m_Files->GetAt(
40 lpFormatEtc->lindex)[0], buffSize);
41 pFile->Write(pBytes,buffSi
ze);
42 }
43 GlobalUnlock(hGlob);
44 }
45 GlobalFree(hGlob);
46 return TRUE;
47 }
48 return COleDataSource::OnRenderFi
leData(
49 lpFormatEtc, pFile);
50 }
Any ideas why I would be getting an AccessViolation?
Thanks,
AngryLoop
Start Free Trial