Pelles C forum

C language => Windows questions => Topic started by: bitcoin on May 02, 2019, 02:03:45 pm

Title: How i can use COM_regexp?
Post by: bitcoin on May 02, 2019, 02:03:45 pm
Hello, i try to use COM regexp object in my program. I find sample in C++, but Pelles types many errors (unknown UUID and other). How to convert this code to Pelles?

Code: [Select]
// Created by Microsoft (R) C/C++ Compiler Version 13.00.9466 (a3b0d9d4).
//
// d:\vcprojects\mark2\mark2\release\vbscript.tlh
//
// C++ source equivalent of Win32 type library vbscript.dll
// compiler-generated file created 06/11/05 at 20:04:03 - DO NOT EDIT!

#pragma once
#pragma pack(push, 8)

#include <comdef.h>

//
// Forward references and typedefs
//

struct __declspec(uuid("3f4daca7-160d-11d2-a8e9-00104b365c9f"))/* LIBID */ __VBScript_RegExp_55;
struct __declspec(uuid("3f4daca0-160d-11d2-a8e9-00104b365c9f"))/* dual interface */ IRegExp;
struct __declspec(uuid("3f4daca1-160d-11d2-a8e9-00104b365c9f"))/* dual interface */ IMatch;
struct __declspec(uuid("3f4daca2-160d-11d2-a8e9-00104b365c9f"))/* dual interface */ IMatchCollection;
struct __declspec(uuid("3f4dacb0-160d-11d2-a8e9-00104b365c9f"))/* dual interface */ IRegExp2;
struct __declspec(uuid("3f4dacb1-160d-11d2-a8e9-00104b365c9f"))/* dual interface */ IMatch2;
struct __declspec(uuid("3f4dacb2-160d-11d2-a8e9-00104b365c9f"))/* dual interface */ IMatchCollection2;
struct __declspec(uuid("3f4dacb3-160d-11d2-a8e9-00104b365c9f"))/* dual interface */ ISubMatches;
struct /* coclass */ RegExp;
struct /* coclass */ Match;
struct /* coclass */ MatchCollection;
struct /* coclass */ SubMatches;

//
// Smart pointer typedef declarations
//

_COM_SMARTPTR_TYPEDEF(IRegExp, __uuidof(IRegExp));
_COM_SMARTPTR_TYPEDEF(IMatch, __uuidof(IMatch));
_COM_SMARTPTR_TYPEDEF(IMatchCollection, __uuidof(IMatchCollection));
_COM_SMARTPTR_TYPEDEF(IRegExp2, __uuidof(IRegExp2));
_COM_SMARTPTR_TYPEDEF(IMatch2, __uuidof(IMatch2));
_COM_SMARTPTR_TYPEDEF(IMatchCollection2, __uuidof(IMatchCollection2));
_COM_SMARTPTR_TYPEDEF(ISubMatches, __uuidof(ISubMatches));

//
// Type library items
//

struct __declspec(uuid("3f4daca0-160d-11d2-a8e9-00104b365c9f"))
IRegExp : IDispatch
{
    //
    // Raw methods provided by interface
    //

      virtual HRESULT __stdcall get_Pattern (       /*[out,retval]*/ BSTR * pPattern ) = 0;
      virtual HRESULT __stdcall put_Pattern (       /*[in]*/ BSTR pPattern ) = 0;
      virtual HRESULT __stdcall get_IgnoreCase (    /*[out,retval]*/ VARIANT_BOOL * pIgnoreCase ) = 0;
      virtual HRESULT __stdcall put_IgnoreCase (    /*[in]*/ VARIANT_BOOL pIgnoreCase ) = 0;
      virtual HRESULT __stdcall get_Global (        /*[out,retval]*/ VARIANT_BOOL * pGlobal ) = 0;
      virtual HRESULT __stdcall put_Global (        /*[in]*/ VARIANT_BOOL pGlobal ) = 0;
      virtual HRESULT __stdcall Execute (      /*[in]*/ BSTR sourceString,/*[out,retval]*/ IDispatch * * ppMatches ) = 0;
      virtual HRESULT __stdcall Test (        /*[in]*/ BSTR sourceString,   /*[out,retval]*/ VARIANT_BOOL * pMatch ) = 0;
      virtual HRESULT __stdcall Replace (/*[in]*/BSTR sourceString,/*[in]*/BSTR replaceString,/*[out,retval]*/BSTR * pDestString ) = 0;
};

struct __declspec(uuid("3f4daca1-160d-11d2-a8e9-00104b365c9f"))
IMatch : IDispatch
{
    //
    // Raw methods provided by interface
    //

      virtual HRESULT __stdcall get_Value (  /*[out,retval]*/ BSTR * pValue ) = 0;
      virtual HRESULT __stdcall get_FirstIndex (   /*[out,retval]*/ long * pFirstIndex ) = 0;
      virtual HRESULT __stdcall get_Length (      /*[out,retval]*/ long * pLength ) = 0;
};

struct __declspec(uuid("3f4daca2-160d-11d2-a8e9-00104b365c9f"))
IMatchCollection : IDispatch
{
    //
    // Raw methods provided by interface
    //

      virtual HRESULT __stdcall get_Item (    /*[in]*/ long index,   /*[out,retval]*/ IDispatch * * ppMatch ) = 0;
      virtual HRESULT __stdcall get_Count (   /*[out,retval]*/ long * pCount ) = 0;
      virtual HRESULT __stdcall get__NewEnum (   /*[out,retval]*/ IUnknown * * ppEnum ) = 0;
};

struct __declspec(uuid("3f4dacb0-160d-11d2-a8e9-00104b365c9f"))
IRegExp2 : IDispatch
{
    //
    // Raw methods provided by interface
    //

      virtual HRESULT __stdcall get_Pattern (        /*[out,retval]*/ BSTR * pPattern ) = 0;
      virtual HRESULT __stdcall put_Pattern (        /*[in]*/ BSTR pPattern ) = 0;
      virtual HRESULT __stdcall get_IgnoreCase (     /*[out,retval]*/ VARIANT_BOOL * pIgnoreCase ) = 0;
      virtual HRESULT __stdcall put_IgnoreCase (     /*[in]*/ VARIANT_BOOL pIgnoreCase ) = 0;
      virtual HRESULT __stdcall get_Global (         /*[out,retval]*/ VARIANT_BOOL * pGlobal ) = 0;
      virtual HRESULT __stdcall put_Global (         /*[in]*/ VARIANT_BOOL pGlobal ) = 0;
      virtual HRESULT __stdcall get_Multiline (      /*[out,retval]*/ VARIANT_BOOL * pMultiline ) = 0;
      virtual HRESULT __stdcall put_Multiline (      /*[in]*/ VARIANT_BOOL pMultiline ) = 0;
      virtual HRESULT __stdcall Execute (        /*[in]*/ BSTR sourceString, /*[out,retval]*/ IDispatch * * ppMatches ) = 0;
      virtual HRESULT __stdcall Test (        /*[in]*/ BSTR sourceString,    /*[out,retval]*/ VARIANT_BOOL * pMatch ) = 0;
      virtual HRESULT __stdcall Replace (/*[in]*/ BSTR sourceString,/*[in]*/ VARIANT replaceVar,/*[out,retval]*/BSTR * pDestString ) = 0;
};

struct __declspec(uuid("3f4dacb1-160d-11d2-a8e9-00104b365c9f"))
IMatch2 : IDispatch
{
    //
    // Raw methods provided by interface
    //

      virtual HRESULT __stdcall get_Value (        /*[out,retval]*/ BSTR * pValue ) = 0;
      virtual HRESULT __stdcall get_FirstIndex (   /*[out,retval]*/ long * pFirstIndex ) = 0;
      virtual HRESULT __stdcall get_Length (       /*[out,retval]*/ long * pLength ) = 0;
      virtual HRESULT __stdcall get_SubMatches (   /*[out,retval]*/ IDispatch * * ppSubMatches ) = 0;
};

struct __declspec(uuid("3f4dacb2-160d-11d2-a8e9-00104b365c9f"))
IMatchCollection2 : IDispatch
{
    //
    // Raw methods provided by interface
    //

      virtual HRESULT __stdcall get_Item (        /*[in]*/ long index, /*[out,retval]*/ IDispatch * * ppMatch ) = 0;
      virtual HRESULT __stdcall get_Count (       /*[out,retval]*/ long * pCount ) = 0;
      virtual HRESULT __stdcall get__NewEnum (    /*[out,retval]*/ IUnknown * * ppEnum ) = 0;
};

struct __declspec(uuid("3f4dacb3-160d-11d2-a8e9-00104b365c9f"))
ISubMatches : IDispatch
{
    //
    // Raw methods provided by interface
    //

      virtual HRESULT __stdcall get_Item (        /*[in]*/ long index,  /*[out,retval]*/ VARIANT * pSubMatch ) = 0;
      virtual HRESULT __stdcall get_Count (     /*[out,retval]*/ long * pCount ) = 0;
      virtual HRESULT __stdcall get__NewEnum (  /*[out,retval]*/ IUnknown * * ppEnum ) = 0;
};

struct __declspec(uuid("3f4daca4-160d-11d2-a8e9-00104b365c9f"))    RegExp;
    // [ default ] interface IRegExp2

struct __declspec(uuid("3f4daca5-160d-11d2-a8e9-00104b365c9f"))    Match;
    // [ default ] interface IMatch2

struct __declspec(uuid("3f4daca6-160d-11d2-a8e9-00104b365c9f"))    MatchCollection;
    // [ default ] interface IMatchCollection2

struct __declspec(uuid("3f4dacc0-160d-11d2-a8e9-00104b365c9f"))    SubMatches;
    // [ default ] interface ISubMatches

//
// Named GUID constants initializations
//

extern "C" const GUID __declspec(selectany) LIBID_VBScript_RegExp_55 =    {0x3f4daca7,0x160d,0x11d2,{0xa8,0xe9,0x00,0x10,0x4b,0x36,0x5c,0x9f}};
extern "C" const GUID __declspec(selectany) IID_IRegExp                =    {0x3f4daca0,0x160d,0x11d2,{0xa8,0xe9,0x00,0x10,0x4b,0x36,0x5c,0x9f}};
extern "C" const GUID __declspec(selectany) IID_IMatch                =    {0x3f4daca1,0x160d,0x11d2,{0xa8,0xe9,0x00,0x10,0x4b,0x36,0x5c,0x9f}};
extern "C" const GUID __declspec(selectany) IID_IMatchCollection    =   {0x3f4daca2,0x160d,0x11d2,{0xa8,0xe9,0x00,0x10,0x4b,0x36,0x5c,0x9f}};
extern "C" const GUID __declspec(selectany) IID_IRegExp2            =   {0x3f4dacb0,0x160d,0x11d2,{0xa8,0xe9,0x00,0x10,0x4b,0x36,0x5c,0x9f}};
extern "C" const GUID __declspec(selectany) IID_IMatch2                =   {0x3f4dacb1,0x160d,0x11d2,{0xa8,0xe9,0x00,0x10,0x4b,0x36,0x5c,0x9f}};
extern "C" const GUID __declspec(selectany) IID_IMatchCollection2    =   {0x3f4dacb2,0x160d,0x11d2,{0xa8,0xe9,0x00,0x10,0x4b,0x36,0x5c,0x9f}};
extern "C" const GUID __declspec(selectany) IID_ISubMatches            =   {0x3f4dacb3,0x160d,0x11d2,{0xa8,0xe9,0x00,0x10,0x4b,0x36,0x5c,0x9f}};
extern "C" const GUID __declspec(selectany) CLSID_RegExp            =   {0x3f4daca4,0x160d,0x11d2,{0xa8,0xe9,0x00,0x10,0x4b,0x36,0x5c,0x9f}};
extern "C" const GUID __declspec(selectany) CLSID_Match                =   {0x3f4daca5,0x160d,0x11d2,{0xa8,0xe9,0x00,0x10,0x4b,0x36,0x5c,0x9f}};
extern "C" const GUID __declspec(selectany) CLSID_MatchCollection    =   {0x3f4daca6,0x160d,0x11d2,{0xa8,0xe9,0x00,0x10,0x4b,0x36,0x5c,0x9f}};
extern "C" const GUID __declspec(selectany) CLSID_SubMatches        =   {0x3f4dacc0,0x160d,0x11d2,{0xa8,0xe9,0x00,0x10,0x4b,0x36,0x5c,0x9f}};

#pragma pack(pop)

taken here http://rsdn.org/forum/com/1219589.all

Title: Re: How i can use COM_regexp?
Post by: TimoVJL on May 02, 2019, 02:30:05 pm
vbscript.dll have 3 tlbs.

That example:
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <ole2.h>
#include "VBS_3.h"

void vMsgBox(TCHAR *szErr, int nErr)
{
}

int RegExpSearch( LPCTSTR szSearchPattern, LPCTSTR szTextForSearch, LPTSTR szOutBuf, int cbOutBuf, BOOL bReplace)
{

    HRESULT hres;
    static IRegExp * pRegExp = 0;
    static IMatch  * pIMatch = 0;
    static IMatchCollection  * pIMatchCollection = 0;

    VARIANT_BOOL vbMatch = 0;
    VARIANT_BOOL vbIgnoreCase = -1;
    VARIANT_BOOL vbGlobal = -1;


    // create the regex component
    hres = CoCreateInstance( &CLSID_RegExp, NULL, CLSCTX_INPROC_SERVER, &IID_IRegExp, (LPVOID *)&pRegExp);
        if (!SUCCEEDED(hres)) {        // ?????? ???????? RegExp ??????????
            //vMsgBox("RegExp not created!",0);
ERR:                if (hres==REGDB_E_CLASSNOTREG )        vMsgBox("REGDB_E_CLASSNOTREG",0);
            if (hres==CLASS_E_NOAGGREGATION )    vMsgBox("CLASS_E_NOAGGREGATION",0);
            if (hres==E_NOINTERFACE  )        vMsgBox("E_NOINTERFACE",0);
            return -1;
        }
    hres = CoCreateInstance( &CLSID_MatchCollection, NULL, CLSCTX_INPROC_SERVER, &IID_IMatchCollection, (LPVOID *)&pIMatchCollection);
    if (!SUCCEEDED(hres)) {    goto ERR; }

        //
        //vMsgBox("RegExp created!",0);

    //
    //// set the regex pattern
    //CComBSTR bstrPattern(_T("[\\S]+[.]tmp"));
    //BSTR bszPat = _T("vi(.)o");
BSTR bszPat = SysAllocString(L"vi(.)o");
    //BSTR bszSea = _T("oksdhfosjdfoisdjfpsfjdpsdfkptxest;?????i????esttt_vito_ttttttghjkhjkhjlhjkljklk\n");
BSTR bszSea = SysAllocString(L"oksdhfosjdfoisdjfpsfjdpsdfkptxest;?????i????esttt_vito_ttttttghjkhjkhjlhjkljklk\n");

   

    hres =  pRegExp->lpVtbl->put_Pattern(pRegExp,bszPat);
//    pRegExp->lpVtbl->get_IgnoreCase(pRegExp,&vbIgnoreCase);
//    pRegExp->lpVtbl->put_IgnoreCase(pRegExp,FALSE);
//    pRegExp->lpVtbl->get_Global(pRegExp,&vbGlobal);
//    pRegExp->lpVtbl->put_Global(pRegExp,vbGlobal);

    hres = pRegExp->lpVtbl->Test(pRegExp,bszSea, (VARIANT_BOOL *)&vbMatch);
        if (!SUCCEEDED(hres) || !vbMatch) {       
                vMsgBox("RegExp not found!",0);
        }   


SysFreeString(bszPat);
SysFreeString(bszSea);
    //////////// ????? ??????
    // ?????????? ??????????
    if (pIMatchCollection)    pIMatchCollection->lpVtbl->Release(pIMatchCollection);
    if (pIMatch)    pIMatch->lpVtbl->Release(pIMatch);
    if (pRegExp)    pRegExp->lpVtbl->Release(pRegExp);

    return 1;
}
Title: Re: How i can use COM_regexp?
Post by: bitcoin on May 02, 2019, 04:04:27 pm
TimoVJL
thank you! Can you tell me, where you get this includes? I want to learn technology COM and understand where it comes from.

Why there are three includes? I must include all three, or only one, to use this code in all Windows versions?
Title: Re: How i can use COM_regexp?
Post by: TimoVJL on May 02, 2019, 04:17:20 pm
I just dump those TLBs from vbscript.dll and then use WTypeLibList (https://forum.pellesc.de/index.php?topic=7165.msg27179#msg27179). With it you can study typelib.
With TLPEView (https://forum.pellesc.de/index.php?topic=7172.msg27200#msg27200) it's possible to examine exe/dll and with TLPlgSave.dll plugin to save those tlb blocks and open with WTypeLibList.

For example i used ComCpp2C (https://forum.pellesc.de/index.php?topic=7320.msg27784#msg27784) and manually changed rest of it.

WTypeLibList is based from Pelles C Add-In typelib.dll example.
Title: Re: How i can use COM_regexp?
Post by: bitcoin on May 02, 2019, 05:52:07 pm
Can you tell me, why in your code you don't use function CoInitializeEx ?
But in another COM samples it used.
Title: Re: How i can use COM_regexp?
Post by: bitcoin on May 02, 2019, 06:05:54 pm
And how to use .tlb files? If i include it into linker, it says that don't know such files.

without it I see errors:
POLINK: error: Unresolved external symbol '_IID_IRegExp'
...
Title: Re: How i can use COM_regexp?
Post by: TimoVJL on May 02, 2019, 06:18:38 pm
Can you tell me, why in your code you don't use function CoInitializeEx ?
main routine should have it, that was a subroutine.
C programs don't use .tlb.

I just pick up value from VBS_3.h
Code: [Select]
const GUID IID_IRegExp={0x3F4DACA0,0x160D,0x11D2,0xA8,0xE9,0x00,0x10,0x4B,0x36,0x5C,0x9F}
if you #define INITGUID all those GUIDs are defined.
Title: Re: How i can use COM_regexp?
Post by: bitcoin on May 02, 2019, 06:39:34 pm
if you #define INITGUID all those GUIDs are defined.
Yes, with this define all compiled.

But program code fails in call
Code: [Select]
CoCreateInstance( &CLSID_MatchCollection, NULL, CLSCTX_INPROC_SERVER, &IID_IMatchCollection, (LPVOID *)&pIMatchCollection);  with error 80040154, REGDB_E_CLASSNOTREG.

Why ? Win10x64. May be Microsoft delete this COM ?
Title: Re: How i can use COM_regexp?
Post by: bitcoin on May 02, 2019, 10:40:11 pm
I add comments to string and it works.
//hres = CoCreateInstance( &CLSID_MatchCollection, NULL, CLSCTX_INPROC_SERVER, &IID_IMatchCollection, (LPVOID *)&pIMatchCollection);
//    if (!SUCCEEDED(hres)) {    goto ERR; }