1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#include <windows.h>
#include <cstdio>
const CLSID CLSID_XLApplication = {0x00024500,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
const IID IID_Application = {0x000208D5,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
int main()
{
DISPPARAMS NoArgs = {NULL,NULL,0,0};
IDispatch* pXLApp = NULL;
IDispatch* pXLWorkbooks = NULL;
IDispatch* pXLWorkbook = NULL;
IDispatch* pXLWorksheets = NULL;
IDispatch* pXLWorksheet = NULL;
IDispatch* pXLRange = NULL;
DISPPARAMS DispParams;
DISPID dispidNamed;
VARIANT vArgArray[1];
VARIANT vResult;
HRESULT hr;
LCID lcid;
CoInitialize(NULL);
hr=CoCreateInstance(CLSID_XLApplication, NULL, CLSCTX_LOCAL_SERVER, IID_Application, (void**)&pXLApp);
if(FAILED(hr))
goto Sad_Ending;
printf("CoCreateInstance() Succeeded!\n");
printf("pXLApp = %u\n",(unsigned)pXLApp);
lcid=GetUserDefaultLCID();
// Set _Application::Visible
VariantInit(&vArgArray[0]);
vArgArray[0].vt=VT_BOOL, vArgArray[0].boolVal=TRUE;
dispidNamed = DISPID_PROPERTYPUT;
DispParams.rgvarg = vArgArray;
DispParams.rgdispidNamedArgs = &dispidNamed;
DispParams.cArgs = 1;
DispParams.cNamedArgs = 1;
VariantInit(&vResult);
hr=pXLApp->Invoke(558,IID_NULL,lcid,DISPATCH_PROPERTYPUT,&DispParams,&vResult,NULL,NULL);
//572 Get _Application::Workbooks >> Gets pXLWorkbooks
VariantInit(&vResult);
hr=pXLApp->Invoke(572,IID_NULL,lcid,DISPATCH_PROPERTYGET,&NoArgs,&vResult,NULL,NULL);
if(FAILED(hr))
goto Sad_Ending;
pXLWorkbooks=vResult.pdispVal;
printf("pXLWorkbooks = %d\n",(int)pXLWorkbooks);
// Call Workbooks::Open() - 682 >> Gets pXLWorkbook
VariantInit(&vResult);
vArgArray[0].vt = VT_BSTR;
vArgArray[0].bstrVal = SysAllocString(L"C:\\Code\\CodeBlks\\XL_Dispatch\\Book1.xls");
DispParams.rgvarg = vArgArray;
DispParams.rgdispidNamedArgs = &dispidNamed;
DispParams.cArgs = 1;
DispParams.cNamedArgs = 0;
hr=pXLWorkbooks->Invoke(682,IID_NULL,lcid,DISPATCH_METHOD,&DispParams,&vResult,NULL,NULL);
if(FAILED(hr))
goto Sad_Ending;
SysFreeString(vArgArray[0].bstrVal);
pXLWorkbook=vResult.pdispVal;
printf("pXLWorkbook = %d\n",(int)pXLWorkbook);
// Call Workbook::Worksheets() //Member Get Worksheets <494> () As XLSheets >> Gets pXLWorksheets
// Call Workbook::Sheets() //Member Get Sheets <485> () As XLSheets
//[id(0x000001e5), propget, helpcontext(0x000101e5)] HRESULT Sheets([out, retval] Sheets** RHS);
//[id(0x000001ee), propget, helpcontext(0x000101ee)] HRESULT Worksheets([out, retval] Sheets** RHS);
VariantInit(&vResult);
hr=pXLWorkbook->Invoke(494,IID_NULL,lcid,DISPATCH_PROPERTYGET,&NoArgs,&vResult,NULL,NULL);
if(FAILED(hr))
goto Sad_Ending;
pXLWorksheets=vResult.pdispVal;
printf("pXLWorksheets = %u\n",(unsigned)pXLWorksheets);
// Member Get Item <170> (In Index As Variant<0>) As IDispatch >> Gets pXLWorksheet
// [id(0x000000aa), propget, helpcontext(0x000100aa)] IDispatch* Item([in] VARIANT Index);
VariantInit(&vResult);
vArgArray[0].vt = VT_BSTR;
vArgArray[0].bstrVal = SysAllocString(L"Sheet2");
DispParams.rgvarg = vArgArray;
DispParams.rgdispidNamedArgs = &dispidNamed;
DispParams.cArgs = 1;
DispParams.cNamedArgs = 0;
hr=pXLWorksheets->Invoke(0xaa,IID_NULL,lcid,DISPATCH_PROPERTYGET,&DispParams,&vResult,NULL,NULL);
if(FAILED(hr))
goto Sad_Ending;
pXLWorksheet=vResult.pdispVal;
printf("pXLWorksheet = %u\n",(unsigned)pXLWorksheet);
SysFreeString(vArgArray[0].bstrVal);
// Worksheet::Select()
VariantInit(&vResult);
VARIANT varReplace; varReplace.vt = VT_BOOL; varReplace.boolVal = VARIANT_TRUE;
dispidNamed=0;
DispParams.rgvarg = &varReplace;
DispParams.rgdispidNamedArgs = &dispidNamed;
DispParams.cArgs = 1;
DispParams.cNamedArgs = 1;
hr=pXLWorksheet->Invoke(0xeb,IID_NULL,lcid,DISPATCH_METHOD,&DispParams,&vResult,NULL,NULL);
if(FAILED(hr))
goto Sad_Ending;
//Member Get Range <197, 0xc5> (In Cell1 As Variant<0>, Opt In Cell2 As Variant<1>) As XLRange
//[id(0x000000c5), propget, helpcontext(0x000100c5)] HRESULT Range([in] VARIANT Cell1, [in, optional] VARIANT Cell2, [out, retval] Range** RHS);
VariantInit(&vResult);
vArgArray[0].vt = VT_BSTR,
vArgArray[0].bstrVal = SysAllocString(L"A1");
DispParams.rgvarg = vArgArray;
DispParams.rgdispidNamedArgs = 0;
DispParams.cArgs = 1; // Try to get Range
DispParams.cNamedArgs = 0; // Invoke _Worksheet::Range("A1") << returns IDispatch** to dispinterface Range
hr=pXLWorksheet->Invoke(197,IID_NULL,lcid,DISPATCH_PROPERTYGET,&DispParams,&vResult,NULL,NULL);
if(FAILED(hr))
{
printf("Failed To Get Range!\n");
goto Sad_Ending;
}
pXLRange=vResult.pdispVal;
printf("pXLRange = %u\n",(unsigned)pXLRange);
//Member Get Value <6> () As Variant
VariantClear(&vArgArray[0]);
hr=pXLRange->Invoke(6,IID_NULL,lcid,DISPATCH_PROPERTYGET,&NoArgs,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
wprintf(L"vResult.bstrVal = %s\n",vResult.bstrVal);
VariantClear(&vResult);
}
getchar();
pXLApp->Invoke(0x0000012e,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_METHOD,&NoArgs,NULL,NULL,NULL);
Sad_Ending: // Workbooks::Close() 277
if(pXLRange)
pXLRange->Release();
if(pXLWorksheet)
pXLWorksheet->Release();
if(pXLWorksheets)
pXLWorksheets->Release();
if(pXLWorkbook)
pXLWorkbook->Release();
if(pXLWorkbooks)
pXLWorkbooks->Release();
if(pXLApp)
pXLApp->Release();
getchar();
CoUninitialize();
return 0;
}
/*
CoCreateInstance() Succeeded!
pXLApp = 2595372
pXLWorkbooks = 2478556
pXLWorkbook = 2592940
pXLWorksheets = 2588260
pXLWorksheet = 2583332
pXLRange = 2589684
vResult.bstrVal = This Is Sheet #2
*/
| |