Using the DicomFileInsert Event Example for C++ 6.0 and later
// Use #import to incorporate the type library information.
// For example:
// #import "C:\Windows\System32\LTDicKrn14n.dll" no_namespace
// #import "C:\Windows\System32\LTDicDir14n.dll" no_namespace
// We are going to make use of some ATL-specific code, so we
// need to include <atlbase.h> and <atlcom.h>. Furthermore, as
// <atlcom.h> is looking for an instance of CComModule named
// _Module, we need to create a dummy object.
#include <atlbase.h>
CComModule _Module;
#include <atlcom.h>
/* Creating the Sink Object */
// Type info for the DicomFileInsert event handler function
_ATL_FUNC_INFO OnDicomFileInsertInfo = {CC_STDCALL,
VT_EMPTY,
4,
{VT_BSTR,
VT_I4,
VT_I2,
VT_BYREF | VT_I2}};
// The class implementing the sink
class CFileInsertSink :
public IDispEventSimpleImpl<1, CFileInsertSink,
&DIID__ILEADDicomDirEvents>
{
private:
ILEADDicomDir* m_pDicomDir;
public:
CFileInsertSink(ILEADDicomDir* pDicomDir)
{
m_pDicomDir = pDicomDir;
m_pDicomDir->AddRef();
// Attach to the source
DispEventAdvise((IUnknown*) m_pDicomDir);
}
virtual ~CFileInsertSink()
{
m_pDicomDir->Release();
// Detach from the source
DispEventUnadvise((IUnknown*) m_pDicomDir);
}
// This function will be called when the DicomFileInsert event
// is fired
void __stdcall OnDicomFileInsert(BSTR bstrFileName, long hDicomDS,
short nStatus, short* pReturnVal)
{
_bstr_t FileName = bstrFileName;
char szBuffer[512];
if (nStatus == DICOMDIR_INSERTDICOMFILE_PREADD)
{
wsprintf(szBuffer, "About to add the file \"%s\".\n\nProceed?",
FileName.operator const char *());
switch (MessageBox(NULL, szBuffer, "DICOM Directory Sample",
MB_YESNOCANCEL))
{
case IDNO:
*pReturnVal = DICOM_ERROR_FORMAT; // Skip the file
return;
case IDCANCEL:
*pReturnVal = DICOM_ERROR_END; // Abort
return;
}
}
else if (nStatus == DICOM_SUCCESS)
{
wsprintf(szBuffer,
"The file \"%s\" has been added successfully." \
"\n\nContinue?", FileName.operator const char *());
if (MessageBox(NULL, szBuffer, "DICOM Directory Sample",
MB_YESNO) == IDNO)
{
*pReturnVal = DICOM_ERROR_END; // Abort
return;
}
}
}
// IDispEventSimpleImpl<> needs a sink map
// Note: The DISPID of the DicomFileInsert event is 1
BEGIN_SINK_MAP(CFileInsertSink)
SINK_ENTRY_INFO(1, DIID__ILEADDicomDirEvents, 1,
OnDicomFileInsert, &OnDicomFileInsertInfo)
END_SINK_MAP()
};
void CreateDicomDir()
{
// Assumes CoInitialize(NULL) has been called
ILEADDicomFactoryPtr
spDicomFactory( __uuidof(LEADDicomFactory));
ILEADDicomKernelPtr spDicomKernel;
LPTSTR pszLic = "LEADTOOLS OCX Copyright (c) 1991-2005"
"LEAD Technologies, Inc.";
spDicomKernel = spDicomFactory->CreateObject
(
"LEADDicomKernel.LEADDicomKernel",
pszLic
);
spDicomKernel->UnlockSupport( L_SUPPORT_MEDICAL,
L_KEY_MEDICAL);
if (spDicomKernel->IsSupportLocked(L_SUPPORT_MEDICAL))
{
spDicomFactory = NULL;
spDicomKernel = NULL;
MessageBox(NULL, "Failed to unlock the Medical Support.",
"DICOM Directory Sample",
MB_OK | MB_ICONERROR);
return;
}
ILEADDicomDirPtr spDicomDir(__uuidof(LEADDicomDir));
// Set the destination folder
spDicomDir->ResetDicomDir("C:\\Medical Images");
// Files in subfolders should also be included
spDicomDir->IncludeSubfolders = VARIANT_TRUE;
// Make the sink
CFileInsertSink* pSink = new CFileInsertSink(spDicomDir);
// Add all the DICOM files to the Directory
spDicomDir->InsertDicomFile("");
delete pSink;
// Save the DICOMDIR File
spDicomDir->SaveDicomDir();
spDicomFactory = NULL;
spDicomKernel = NULL;
spDicomDir = NULL;
// remember to call CoUninitialize() when your
// app no longer needs COM objects.
}