Reply

CODE SAMPLE: Get a list of assembly configurations directly from the cfg file

[ Edited ]

 

I've been meaning to do this for a while, and a colleage of mine recently asked me if I had some code for this, so I thought it was about time to see what's in the cfg files...

The reason for this is so that we can give the user the choice of opening an assembly with a configuration preselected in a custom open dialog. To do this we need the names of the configurations, and these are, afaik, only available for an open assembly when using the Solid Edge API.

I haven't checked out the details (ie. what's is actually in the seperate configurations), but basically there are two sub-storages in the main cfg-file storage (Windows Structured Storage 'IStorage' interface. These are 'Configs' and 'Zones'. in the Configs storage there is a stream for each configuration and the stream name is the same as the configuration name.

Here's some C++ code (it's C++/CLI but the managed code can easily be removed from the method, let me know if you have any problems add the code to an ATL project) and I have attached two C++/CLI dlls for all you .Net junkies out there (one for x86, and one for x64), there is a single class (Configuration) with a single static method (GetConfigurationNames).#include "Objbase.h"

#include "propidl.h"

#include

 

System::Collections::Generic::List ^ GetConfigurationNames(System:Smiley Frustratedtring^ filename)

{

/* Convert the path to an unmanaged string */

pin_ptr path = PtrToStringChars(filename);

 

/* Open the file and get a list of the configuration stream names */

CoInitialize(NULL);

 

IStorage* pStorage = NULL;

HRESULT hr = StgOpenStorageEx(path, (STGM_TRANSACTED | STGM_READ),

STGFMT_DOCFILE, NULL, NULL, NULL, IID_IStorage, (void**)&pStorage);

 

if (SUCCEEDED(hr))

{

IEnumSTATSTG* pEnumStorage = NULL;

HRESULT hr = pStorage->EnumElements(NULL, NULL, NULL, &pEnumStorage);

if (SUCCEEDED(hr))

{

ULONG fetched;

while(true)

{

STATSTG statStg;

hr = pEnumStorage->Next(1, &statStg, &fetched);

 

if (SUCCEEDED(hr))

{

/* IPropertySetStorage::Next returns S_OK if the requested

number of items (in this case, 1) have been returned. If

the method returns S_FALSE less items have been returned

than were requested, and we are at the end of the

collection. */

 

if (fetched == 1)

{

if (_wcsicmp(L"Configs", statStg.pwcsName) == 0)

{

/* declare a collection to return */

System::Collections::Generic::List ^ retval =

gcnew System::Collections::Generic::List ();

 

/* Open the configs storage */

IStorage* pConfigs = NULL;

hr = pStorage->OpenStorage(statStg.pwcsName,

NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, NULL, NULL, &pConfigs);

 

if (SUCCEEDED(hr))

{

IEnumSTATSTG* pEnumConfigStorage = NULL;

HRESULT hr = pConfigs->EnumElements(NULL, NULL, NULL, &pEnumConfigStorage);

if (SUCCEEDED(hr))

{

ULONG fetched;

while(true)

{

hr = pEnumConfigStorage->Next(1, &statStg, &fetched);

 

if (SUCCEEDED(hr))

{

if (fetched == 1)

{

/* Copy the name to the collection */

retval->Add(gcnew System:Smiley Frustratedtring(statStg.pwcsName));

}

else

{

// Nothing returned

break;

}

if (hr == S_FALSE) break;

}

else

{

break;

}

}

pEnumConfigStorage->Release();

pConfigs->Release();

 

/* Return the list */

CoUninitialize();

return retval;

}

}

}

}

else

{

// Nothing returned

break;

}

if (hr == S_FALSE) break;

}

else

{

break;

}

}

pEnumStorage->Release();

}

pStorage->Release();

}

 

CoUninitialize();

 

throw gcnew System::ApplicationException("Could not read configuration file");

}

Hope this helps someone out there

Cheers

Calum

 

Posted by: Calum McLellan
Post date: 9/3/2009 10:35:05 PM

2 REPLIES

RE: CODE SAMPLE: Get a list of assembly configurations directly from the cfg file

[ Edited ]

Impressive!

Thanks a lot, Calum

 

Posted by: Julian Guillo
Post date: 9/3/2009 10:40:57 PM

RE: CODE SAMPLE: Get a list of assembly configurations directly from the cfg file

[ Edited ]

Thanks Calum. Can you translate the same code into C#. I couldnt understand this code please.

 

Thanks

Venkata Ramesh V

 

Posted by: Venkata Ramesh Virodhula
Post date: 7/19/2011 4:01:06 AM