Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Re: What is wrong with this very simple code structure



Permlink Replies: 1 - Last Post: Jan 17, 2018 12:29 PM Last Post By: roca robin
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: What is wrong with this very simple code structure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 16, 2018 2:00 PM
roca robin wrote:

*(sorry, your original message got lost in a server error)

CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
IID_ICreateDevEnum, (void**)&dev_enum);

You are not checking the return value for error.

Also, you should be using CLSCTX_INPROC_SERVER instead of CLSCTX_INPROC.

dev_enum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,
&enum_moniker, NULL);

Again, you are not checking the return value for error. This is
especially important if the return value is S_FALSE, meaning the
requested category is empty, in which case your enum_moniker variable
will be set to NULL, which you are also not checking for.

enum_moniker->Next(8, &moniker, &dev_count);

Again, you are not checking the return value for error.

Also, you are setting celt=8 for the number of IMoniker* pointers to
retreive, but you are providing only 1 IMoniker* pointer to be filled
in. If there are more than 1 moniker available, you are going to
corrupt memory. You need to either pass in an array of 8 IMoniker*
variables, or specify celt=1 instead of celt=8.

Also, you are not calling Release() on each IMoniker that is returned.

hr = moniker[i].BindToStorage(0, 0, IID_IPropertyBag, (void **)&pbag);

Your moniker variable is not an array of IMoniker* pointers, it is a
single IMoniker* pointer, so you shouldn't be using array syntax to
deference it, use the '->' operator instead.

pbag->Read(L"FriendlyName", &name, 0);

Again, you are not checking the return value for error.

Now, with all of that said, your code needs to look more like this
instead:

HRESULT hr;
ICreateDevEnum* dev_enum;
IEnumMoniker* enum_moniker;
IMoniker* moniker;
IPropertyBag* pbag;
 
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void**)&dev_enum);
if (SUCCEEDED(hr))
{
    hr =
dev_enum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,
&enum_moniker, NULL);
    if (SUCCEEDED(hr) && (enum_moniker))
    {
        while (enum_moniker->Next(1, &moniker, NULL) == S_OK)
        {
            hr = moniker->BindToStorage(0, 0, IID_IPropertyBag, (void
**)&pbag);
            if(SUCCEEDED(hr))
            {
                VARIANT name;
                VariantInit(&name);
 
                hr = pbag->Read(L"FriendlyName", &name, 0);
                if (SUCCEEDED(hr))
                {
                    ListBox1->Items->Add(name.bstrVal);
                    VariantClear(&name);
                }
 
                pbag->Release();
            }
 
            moniker->Release();
        }
 
        enum_moniker->Release();
    }
 
    dev_enum->Release();
}


Or this:

HRESULT hr;
ULONG dev_count;
ICreateDevEnum* dev_enum;
IEnumMoniker* enum_moniker;
IMoniker* moniker[8];
IPropertyBag* pbag;
 
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void**)&dev_enum);
if (SUCCEEDED(hr))
{
    hr =
dev_enum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,
&enum_moniker, NULL);
    if (SUCCEEDED(hr) && (enum_moniker))
    {
        do
        {
            hr = enum_moniker->Next(8, moniker, &dev_count);
            if (FAILED(hr) || (dev_count == 0))
                break;
 
            for (ULONG i = 0; i < dev_count; ++i)
            {
                hr = moniker[i]->BindToStorage(0, 0, IID_IPropertyBag,
(void **)&pbag);
                if(SUCCEEDED(hr))
                {
                    VARIANT name;
                    VariantInit(&name);
 
                    hr = pbag->Read(L"FriendlyName", &name, 0);
                    if (SUCCEEDED(hr))
                    {
                        ListBox1->Items->Add(name.bstrVal);
                        VariantClear(&name);
                    }
 
                    pbag->Release();
                }
 
                moniker[i]->Release();
            }
        }
        while (true);
 
        enum_moniker->Release();
    }
 
    dev_enum->Release();
}


--
Remy Lebeau (TeamB)
roca robin

Posts: 140
Registered: 9/10/06
Re: What is wrong with this very simple code structure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 17, 2018 12:29 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
No, it's Ok.
I already change the subject title and message body, because I already got it, there was no server error when I posted this.
but that works fine also,
thanks again :D

this is the original messageID or threadID,
https://forums.embarcadero.com/thread.jspa?threadID=276449&tstart=0

I guess the server behaves differently on this for now.

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02