在VC8里调用VBScript.dll的RegularExpression

1、选择添加类--TypeLib中的MFC类。
2、选择从注册表中添加,位置为C:\windows\system32\vbscript.dll
3、生成类文件CMatch.h CMatch2.h CMatchCollection.h CMatchCollection2.h CRegExp.h CRegExp2.h
CSubMatches.h
4、将#import "c:\\windows\\system32\\vbscript.dll\\3" no_namespace
改为#import "progid:VBScript.RegExp" no_namespace,不然会提示找不到文件

加声明
const GUID LIBID_VBScript_RegExp_55 = {0x3F4DACA7, 0x160D, 0x11D2,{ 0xA8, 0xE9, 0x00,0x10, 0x4B, 0x36,0x5C, 0x9F} };
const GUID IID_IRegExp = {0x3F4DACA0, 0x160D, 0x11D2,{ 0xA8, 0xE9, 0x00,0x10, 0x4B, 0x36,0x5C, 0x9F} };
const GUID IID_IMatch = {0x3F4DACA1, 0x160D, 0x11D2,{ 0xA8, 0xE9, 0x00,0x10, 0x4B, 0x36,0x5C, 0x9F} };
const GUID IID_IMatchCollection = {0x3F4DACA2, 0x160D, 0x11D2,{ 0xA8, 0xE9, 0x00,0x10, 0x4B, 0x36,0x5C, 0x9F} };
const GUID IID_IRegExp2 = {0x3F4DACB0, 0x160D, 0x11D2,{ 0xA8, 0xE9, 0x00,0x10, 0x4B, 0x36,0x5C, 0x9F} };
const GUID IID_IMatch2 = {0x3F4DACB1, 0x160D, 0x11D2,{ 0xA8, 0xE9, 0x00,0x10, 0x4B, 0x36,0x5C, 0x9F} };
const GUID IID_IMatchCollection2 = {0x3F4DACB2, 0x160D, 0x11D2,{ 0xA8, 0xE9, 0x00,0x10, 0x4B, 0x36,0x5C, 0x9F} };
const GUID IID_ISubMatches = {0x3F4DACB3, 0x160D, 0x11D2,{ 0xA8, 0xE9, 0x00,0x10, 0x4B, 0x36,0x5C, 0x9F} };
const GUID CLSID_RegExp = {0x3F4DACA4, 0x160D, 0x11D2,{ 0xA8, 0xE9, 0x00,0x10, 0x4B, 0x36,0x5C, 0x9F} };
const GUID CLSID_Match = {0x3F4DACA5, 0x160D, 0x11D2,{ 0xA8, 0xE9, 0x00,0x10, 0x4B, 0x36,0x5C, 0x9F} };
const GUID CLSID_MatchCollection = {0x3F4DACA6, 0x160D, 0x11D2,{ 0xA8, 0xE9, 0x00,0x10, 0x4B, 0x36,0x5C, 0x9F} };
const GUID CLSID_SubMatches = {0x3F4DACC0, 0x160D, 0x11D2,{ 0xA8, 0xE9, 0x00,0x10, 0x4B, 0x36,0x5C, 0x9F} };

5、调用方法如下:
void Test()
{
CoInitialize(NULL);

CLSID clsid;
HRESULT hr = ::CLSIDFromProgID(_T("VBScript.RegExp"), &clsid);
if ( FAILED( hr ))
{
  CoUninitialize();
  return;
}

IClassFactory* pCF;
hr = CoGetClassObject(clsid, CLSCTX_INPROC, NULL, IID_IClassFactory, (void**) &pCF);
if ( FAILED( hr ))
{
  CoUninitialize();
  return;
}

IRegExp2* pObj;
hr = pCF->CreateInstance(NULL, IID_IRegExp2, (void**) &pObj);
pCF->Release();
if ( FAILED( hr ))
{
  CoUninitialize();
  return;
}

pObj->Pattern = _T("^\\d{4}-\\d{4}\\u8881$");
VARIANT_BOOL pMatch = 0;

pMatch = pObj->Test(_T("3233-3456袁"));

if (pMatch)
  cout << "^\\d{4}-\\d{4}\\u8881$ match 3233-3456袁 is true" << endl;
else
  cout << "^\\d{4}-\\d{4}\\u8881$ match 3233-3456袁 is false" << endl;

pMatch = pObj->Test(_T("3233s-3456"));

if (pMatch)
  cout << "^\\d{4}-\\d{4}$ match 3233s-3456 is true" << endl;
else
  cout << "^\\d{4}-\\d{4}$ match 3233s-3456 is false" << endl;

CoUninitialize();
}

你可能感兴趣的:(VBScript)