Pelles C forum

C language => Beginner questions => Topic started by: bitcoin on December 05, 2018, 06:17:16 pm

Title: How to use COM in plain C?
Post by: bitcoin 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? :'(
Title: Re: How to use COM in plain C?
Post by: TimoVJL on December 06, 2018, 11:26:33 am
Articles by Jeff Glatt (https://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=88625)
COM objects in plain C... (https://forum.pellesc.de/index.php?topic=4575.msg17378#msg17378)
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;
}
Title: Re: How to use COM in plain C?
Post by: bitcoin on April 10, 2019, 05:30:18 am
How to write data into new file? I tried to create new file with IFileOperation::NewItem , it works, but file is empty.

Or IFileOperation can't write such as WriteFile? I must to re-open file with CreateFile?