NO

Author Topic: How to use COM in plain C?  (Read 3006 times)

Offline bitcoin

  • Member
  • *
  • Posts: 179
How to use COM in plain C?
« on: December 05, 2018, 06:17:16 PM »
Hello!
Can you tell me the simple way to use COM in plain C (not C++). Is there some books,  manuals ? Or i must to learn object-oriented programming?

I don't want to write my own COM-objects - but only use Microsoft COM (example, IFileOperation);
code https://docs.microsoft.com/en-us/windows/desktop/api/shobjidl_core/nf-shobjidl_core-ifileoperation-copyitem don't compile in Pure C.

Why so, or this impossible? :'(

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: How to use COM in plain C?
« Reply #1 on: December 06, 2018, 11:26:33 AM »
Articles by Jeff Glatt
COM objects in plain C...
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#include <shobjidl.h>

HRESULT CopyItem(PCWSTR pszSrcItem, PCWSTR pszDest, PCWSTR pszNewName)
{
    //
    // Initialize COM as STA.
    //
    HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    if (SUCCEEDED(hr))
    {
        IFileOperation *pfo;
 
        //
        // Create the IFileOperation interface
        //
        hr = CoCreateInstance(&CLSID_FileOperation,
                              NULL,
                              CLSCTX_ALL,
                              &IID_IFileOperation,
                              (void**)&pfo);
        if (SUCCEEDED(hr))
        {
            //
            // Set the operation flags. Turn off all UI from being shown to the
            // user during the operation. This includes error, confirmation,
            // and progress dialogs.
            //
            hr = pfo->lpVtbl->SetOperationFlags(pfo, FOF_NO_UI);
            if (SUCCEEDED(hr))
            {
                //
                // Create an IShellItem from the supplied source path.
                //
                IShellItem *psiFrom = NULL;
                hr = SHCreateItemFromParsingName(pszSrcItem,
                                                 NULL,
                                                 &IID_IShellItem,
                                                 (void**)&psiFrom);
                if (SUCCEEDED(hr))
                {
                    IShellItem *psiTo = NULL;
 
                    if (NULL != pszDest)
                    {
                        //
                        // Create an IShellItem from the supplied
                        // destination path.
                        //
                        hr = SHCreateItemFromParsingName(pszDest,
                                                         NULL,
                                                         &IID_IShellItem,
                                                         (void**)&psiTo);
                    }
                   
                    if (SUCCEEDED(hr))
                    {
                        //
                        // Add the operation
                        //
                        hr = pfo->lpVtbl->CopyItem(pfo, psiFrom, psiTo, pszNewName, NULL);
                    if (NULL != psiTo)
                    {
                        psiTo->lpVtbl->Release(psiTo);
                    }
                }
               
                psiFrom->lpVtbl->Release(psiFrom);
            }
           
            if (SUCCEEDED(hr))
            {
                //
                // Perform the operation to copy the file.
                //
                hr = pfo->lpVtbl->PerformOperations(pfo);
            }       
        }
       
        //
        // Release the IFileOperation interface.
        //
        pfo->lpVtbl->Release(pfo);
    }
        CoUninitialize();
    }
    return hr;
}
May the source be with you