Adding/Extracting Sequences in MSVC

Sometimes we are asked about how to Add/Extract DICOM sequences in Microsoft Visual C++, to avoid replying similar Emails in the further, we put the code here.

Adding Sequence

IDicomDataSetPtr ds;
ds.CreateInstance("DicomObjects.DicomDataSet");

IDicomDataSetsPtr seq;
seq.CreateInstance("DicomObjects.DicomDataSets");

IDicomDataSetPtr seqItem1;
seqItem1.CreateInstance("DicomObjects.DicomDataSet");
IDicomDataSetPtr seqItem2;
seqItem2.CreateInstance("DicomObjects.DicomDataSet");

seqItem1->Name = "seqItem1";
seqItem2->Name = "seqItem2";

seq->Add(seqItem1);
seq->Add(seqItem2);

ds->Attributes->Add(group, element, (IDispatch *)seq);

Extracting Sequence

IDicomDataSetsPtr seq2;
seq2 = (IDicomDataSetsPtr)ds->Attributes->Get_Item(0x88,0x200)->Get_Value();

IDicomDataSetPtr seqItemA;
IDicomDataSetPtr seqItemB;

seqItemA = seq2->Item[(short)(1)];
seqItemB = seq2->Item[(short)(2)];

_bstr_t name1;
_bstr_t name2;
name1 = seqItemA->Name;
name2 = seqItemB->Name;
Relevance: