HOWTO: S? d?ng t? ?ng ha OLE t? ?ng d?ng c m?t ch? khng ph?i l C++

D?ch tiu ? D?ch tiu ?
ID c?a bi: 181473 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

TM T?T

N?u b?n c?n chng tr?nh trong C, ch? khng ph?i l C++, OLE t? ?ng ha c th? kh khn b?i v? cc thnh ph?n ?i t?ng m h?nh M-hi-c (COM), cc kh?i xy d?ng OLE, ?c thi?t k? xung quanh nh? phn b? tr c?a m?t l?p h?c C++. Cc t?p tin tiu ? bao g?m b?i WINDOWS.H, tuy nhin, ? ?c xy d?ng trong h? tr? cho bin d?ch C; V? v?y, b?n c th? b?t ch?c hnh vi c?a m?t ?i t?ng C++ v lm cho OLE Cc cu?c g?i t? ?ng ha. Bi vi?t ny m t? cch xy d?ng m?t giao di?n i?u khi?n Win32 ?ng d?ng, s? d?ng C, m b?t ?u ln Microsoft Excel v lm cho n c th? nh?n th?y thng qua t? ?ng ha OLE.

THNG TIN THM

S? d?ng cc phng php sau y:
  1. T?o m?t d? n "?ng d?ng giao di?n i?u khi?n c?a Win32" m?i trong Microsoft Visual C++ 5.0.
  2. Thm m?t t?p tin vn b?n Main.c (khng Main.cpp) cho d? n c?a b?n.
  3. Thm m? sau vo t?p tin Main.c:
          #include <stdio.h>
          #include <windows.h>
    
          void main(void) {
             IDispatch *pDisp; // Main IDispatch pointer.
             unsigned short *ucPtr; // Temporary variable to hold names.
             DISPID dispID; // Temporary variable to hold DISPIDs.
             CLSID clsid; // Holds CLSID of server after CLSIDFromProgID.
             HRESULT hr; // General error/result holder.
             char buf[8192]; // Generic buffer for output.
    
             // IDispatch::Invoke() parameters...
             DISPPARAMS dispParams = { NULL, NULL, 0, 0 };
             VARIANT parm1;
             DISPID dispidNamed = DISPID_PROPERTYPUT;
    
    
             // Initialize OLE Libraries.
             OleInitialize(NULL);
             {
                // Get CLSID for Excel.Application from registry.
                hr = CLSIDFromProgID(L"Excel.Application", &clsid);
                if(FAILED(hr)) {
                   MessageBox(NULL, "Excel not registered.", "Error",
                              MB_SETFOREGROUND);
                   return;
                }
                // Start Excel97, Excel 2000, or Excel 2002 and get its IDispatch pointer.
                hr = CoCreateInstance(&clsid, NULL, CLSCTX_LOCAL_SERVER,
                                      &IID_IDispatch, (void **)&pDisp);
                if(FAILED(hr)) {
                   MessageBox(NULL, "Couldn't start Excel.", "Error",
                              MB_SETFOREGROUND);
                   return;
                }
    
                // Get the 'visible' property's DISPID.
                ucPtr = L"Visible";
                pDisp->lpVtbl->GetIDsOfNames(pDisp, &IID_NULL, &ucPtr, 1,
                                             LOCALE_USER_DEFAULT, &dispID);
    
                sprintf(buf, "DISPID for 'Visible' property = 0x%08lx",
                        dispID);
                MessageBox(NULL, buf, "Debug Notice", MB_SETFOREGROUND);
    
                // Initiate parameters to set visible property to true.
                VariantInit(&parm1);
                parm1.vt = VT_I4;
                parm1.lVal = 1; // true
    
                // One argument.
                dispParams.cArgs = 1;
                dispParams.rgvarg = &parm1;
    
                // Handle special-case for property-puts!
                dispParams.cNamedArgs = 1;
                dispParams.rgdispidNamedArgs = &dispidNamed;
    
                // Set 'visible' property to true.
                hr = pDisp->lpVtbl->Invoke(pDisp,
                   dispID, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
                   DISPATCH_PROPERTYPUT | DISPATCH_METHOD,
                   &dispParams, NULL, NULL, NULL
                );
                if(FAILED(hr)) {
                   sprintf(buf, "IDispatch::Invoke() failed with %08lx", hr);
                   MessageBox(NULL, buf, "Debug Notice", MB_SETFOREGROUND);
                }
    
                // All done.
                MessageBox(NULL, "done.", "Notice", MB_SETFOREGROUND);
             }
             // Uninitialize OLE Libraries.
             OleUninitialize();
    
          }
    					
  4. Bin d?ch v ch?y.

THAM KH?O

? bi?t thng tin t?ng qut hn v? OLE, COM v t? ?ng ha, tham kh?o ? ki?n cu?n sch "Bn trong OLE" b?i Kraig Brockschmidt (Microsoft Press).

? bi?t thm chi ti?t v? IDispatch, tham kh?o ? ki?n Microsoft Visual C++ tr? gip tr?c tuy?n.

? bi?t thm thng tin v? COM, OLE v automating b?ng cch s? d?ng Microsoft Excel Microsoft Visual C++, xem chng 23-27 c?a cu?n sch "Bn trong Visual c ++" b?i David J. Kruglinski (Microsoft Press).

(c) t?p on Microsoft 1999, t?t c? cc quy?n. Nh?ng ng gp c?a Joe Crump, Microsoft Corporation.

Thu?c tnh

ID c?a bi: 181473 - L?n xem xt sau cng: 20 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 97 Standard Edition
T? kha:
kbhowto kbmt KB181473 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:181473

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com