Results 1 to 2 of 2

Thread: What is the equivalent release function for CoCreateInstance?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008

    What is the equivalent release function for CoCreateInstance?

    I know that the Windows API function CoCreateInstance lets you create an instance of a specified class with a specified interface that's implemented by that class (based on the inteface GUID and class GUID you provide). However, how do you delete this instance? Is there a Windows API function that likewise lets you delete an instance? I know that for kernel32 objects you have CloseHandle, and for GDI objects you have DeleteObject and ReleaseObject. I'm not sure what the API function is to delete a COM object though.

  2. #2
    Join Date
    Jul 2010

    Re: What is the equivalent release function for CoCreateInstance?

    No specific API; it's released when the reference counter of the object you created hits 0.

    VB handles this automatically for you. Set object = Nothing will do it immediately. Once in a great while, you have reason to override VB's automatic management; you can cast it to an unrestricted IUnknown, call AddRef, then it has a reference count different from what VB is aware of, so won't be released until you call Release an equal number of times as AddRef.

    All COM objects (what you're creating with CoCreateInstance) must implement IUnknown, and the standard implementation for Release looks like this:

    ULONG CMyObj::Release()
        if (--m_dwRef == 0)
            delete this;
            return 0;
        return m_dwRef;
    That's where it's freed when the counter hits 0.

    ...if you *really* wanted an API for it; there's a helper function; IUnknown_AtomicRelease in shlwapi.dll. You really shouldn't call that from VB though. Probably wind up crashing.
    Last edited by fafalone; Jul 23rd, 2023 at 02:47 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width