在做一個東西的時候,里面涉及到COM編程的一些東西,但我只是呼叫別人寫好的com組建,這讓我有點懵逼。感覺和平時用的庫不一樣啊,給了一個檔案里面介紹了一些函式的用法,但我不知道這些函式該怎么呼叫啊,不知道我的問題說清楚了沒有,愿意幫助我的大神加我QQ:1785703760,第一次在這發帖,不知道該怎么玩,積分較少,希望各位大神能體諒一下,謝謝啦!!
uj5u.com熱心網友回復:
可能沒有說清楚,有什么問題可以直接問我uj5u.com熱心網友回復:
僅供參考:// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Visual C++ Language Reference and related
// electronic documentation provided with Microsoft Visual C++.
// See these sources for detailed information regarding the
// Microsoft Visual C++ product.
// NOTE: This example will only work with Excel8 in Office97
// Compile with cl /GX comexcel.cpp
// TO DO: Edit the #import paths
//#pragma message ("Make sure you go to Tools.Options.Directories and add the paths to mso97.dll and vbeext1.olb. Mso97.dll will usually be in c:\\\"Program Files\"\\\"Microsoft Office\"\\Office, and vbeext1.olb will be in c:\\\"Program Files\"\\\"Common Files\"\\\"Microsoft Shared\"\\VBA")
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\Office11\\mso.dll" no_namespace rename("DocumentProperties", "DocumentPropertiesXL")
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\VBA6\\VBE6EXT.OLB" no_namespace
#import "C:\\Program Files\\Microsoft Office\\OFFICE11\\excel.exe" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces
#pragma warning (disable:4192 4146)
#include <stdio.h>
#include <tchar.h>
void dump_com_error(_com_error &e)
{
_tprintf(_T("Oops - hit an error!\n"));
_tprintf(_T("\a\tCode = %08lx\n"), e.Error());
_tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
_tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
_tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
}
// If this is placed in the scope of the smart pointers, they must be
// explicitly Release(d) before CoUninitialize() is called. If any reference
// count is non-zero, a protection fault will occur.
struct StartOle
{
StartOle() { CoInitialize(NULL); }
~StartOle() { CoUninitialize(); }
} _inst_StartOle;
void main()
{
using namespace Excel;
_ApplicationPtr pXL;
try
{
pXL.CreateInstance(L"Excel.Application");
pXL->Visible = VARIANT_TRUE;
WorkbooksPtr pBooks = pXL->Workbooks;
_WorkbookPtr pBook = pBooks->Add((long)xlWorksheet);
_WorksheetPtr pSheet = pXL->ActiveSheet;
RangePtr pRange;
pRange = pSheet->Range["A21"];
pRange->Value2 = 75L;
//pRange->NumberFormatLocal = "@";
_CommandBarsPtr pCmdbars = pXL->CommandBars;
int iCmdbars = pCmdbars->GetCount();
Sleep(1000);
pRange = pSheet->Range["20:20"];
pRange->Insert( (long)Excel::xlDown );
pRange->Merge();
Sleep(1000);
pBook->Saved = VARIANT_TRUE;
pXL->Quit();
}
catch(_com_error &e)
{
dump_com_error(e);
pXL->Quit();
}
}
uj5u.com熱心網友回復:
COM 在使用的時候,需要介面的定義 和 classid。
你看看給你的 .idl 檔案就可以了。
創建服務。
IXXX pXXX = NULL;
HRESULT hr = CoCreateInstance(<classid>, NULL, IID_PPV_ARGS(&pXXX));
// 使用 IXXX 的實體的方法。
pXXX->Release(); // 使用完后釋放之。
別人提供的一般都有樣例的,先看看。
uj5u.com熱心網友回復:
有idl檔案,但是,idl檔案加載入編譯器,編譯器會生成相應的.h和.c檔案,這些都有,把他們用include包含進來就可以呼叫庫函式了嗎?我的問題有點多,大神,能不能留下你的聯系方式,謝謝了
uj5u.com熱心網友回復:
有dll檔案,老師,近期看了一點com編程的書,你的代碼能知道大概意思。就我這個問題,給的有idl檔案,dll檔案,還有idl生成的.c和.h檔案,檔案里面簡單介紹了函式的使用方法,可我呼叫的時候該需要包含哪個檔案???我試過直接包含.h檔案,但.h檔案里類的成員函式都是虛函式,沒找到函式的實作,這樣看來他就是一個虛基類根本就不能定義物件,老師,懵逼了,不知道該怎么辦了,需要的到你的聯系方式,給我指點一下,謝謝您
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/136660.html
