Click here to Skip to main content
15,885,546 members
Home / Discussions / COM
   

COM

 
QuestionUsing a COM Server in an Visual Studio .vcproj Pin
Juergen_8010-Sep-09 4:44
Juergen_8010-Sep-09 4:44 
AnswerRe: Using a COM Server in an Visual Studio .vcproj [modified] Pin
Stuart Dootson10-Sep-09 5:27
professionalStuart Dootson10-Sep-09 5:27 
GeneralRe: Using a COM Server in an Visual Studio .vcproj Pin
Juergen_8010-Sep-09 22:51
Juergen_8010-Sep-09 22:51 
GeneralRe: Using a COM Server in an Visual Studio .vcproj Pin
Stuart Dootson10-Sep-09 23:10
professionalStuart Dootson10-Sep-09 23:10 
GeneralRe: Using a COM Server in an Visual Studio .vcproj Pin
Juergen_8010-Sep-09 23:59
Juergen_8010-Sep-09 23:59 
GeneralRe: Using a COM Server in an Visual Studio .vcproj Pin
Stuart Dootson11-Sep-09 0:55
professionalStuart Dootson11-Sep-09 0:55 
GeneralRe: Using a COM Server in an Visual Studio .vcproj Pin
Juergen_8011-Sep-09 1:58
Juergen_8011-Sep-09 1:58 
GeneralRe: Using a COM Server in an Visual Studio .vcproj Pin
Stuart Dootson11-Sep-09 10:58
professionalStuart Dootson11-Sep-09 10:58 
Here's a full example using ATL and #import - the comments should be informative, I hope Smile | :) It references the Excel COM server, obviously running in a separate process. It tells Excel it wants to catch application-specific events (as defined by the interface AppEvents), by telling it what object to send the events to.

#include <iostream>
#include <atlcom.h>

// Reference the COM server - you can put the .exe's path in the string instead
// of the libid.
#import "libid:00020813-0000-0000-C000-000000000046" version("1.6") auto_search no_dual_interfaces raw_dispinterfaces rename("DialogBox", "excelDialogBox") rename("RGB", "excelRGB") rename("DocumentProperties", "excelDocumentProperties") rename("SearchPath", "excelSearchPath") rename("CopyFile", "excelCopyFile") rename("ReplaceText", "excelReplaceText")

// This defines the signature of the event I want to handle
_ATL_FUNC_INFO SheetChangeInfo = { CC_CDECL, VT_EMPTY, 2, { VT_DISPATCH, VT_DISPATCH } };

// This is the connection point sink class. The event interface is 
// Excel::AppEvents
class ExcelAppEventHandler : public IDispEventSimpleImpl<1, ExcelAppEventHandler, &__uuidof(Excel::AppEvents)>
{
public:
   ExcelAppEventHandler(bool& doneFlag) : done_(doneFlag) { done_ = false; }
   BEGIN_SINK_MAP(ExcelAppEventHandler)
      SINK_ENTRY_INFO(1, __uuidof(Excel::AppEvents), 0x0000061c, &ExcelAppEventHandler::SheetChange, &SheetChangeInfo)
   END_SINK_MAP()

   void _stdcall SheetChange(IDispatch * Sh, struct Excel::Range * Target)
   {
      done_ = true;
   }
private:
   bool& done_;
};

// This function uses the connection point sink class
_bstr_t GetActiveWorkbookName(Excel::_ApplicationPtr xl)
{
   if (Excel::_WorkbookPtr wb = xl->ActiveWorkbook)
   {
      try
      {
         return wb->FullName;
      }
      catch(_com_error& e)
      {
         std::cout << "EXCEPTION!!!\n";
         std::cerr << CT2CA(e.ErrorMessage()) << std::endl;

         bool done;
         // Instantiate the connection point sink
         ExcelAppEventHandler app(done);
         // Tell the COM server that you want to catch events
         if (SUCCEEDED(app.DispEventAdvise(xl)))
         {
            // Put in a message loop to allow COM to work - we're waiting
            // until an event's been received (indicated by done)
            MSG msg;
            while (!done && GetMessage(&msg, NULL, 0, 0) > 0) {
               TranslateMessage(&msg);
               DispatchMessage(&msg);
            }            
            // Tell the COM server we don't want to catch events any more
            app.DispEventUnadvise(xl);
            return GetActiveWorkbookName(xl);
         }
      }
   }
   return _bstr_t();
}

int _tmain(int argc, _TCHAR* argv[])
{
   CoInitializeEx(0, COINIT_APARTMENTTHREADED);
   {
      // This is an interface pointer to Excel's Application interface
      Excel::_ApplicationPtr xl;
      // Get a reference to a running Excel - you can use CreateObject
      if (SUCCEEDED(xl.GetActiveObject(__uuidof(Excel::Application))))
      {
         std::cout << "Getting name\n";
         std::cout << GetActiveWorkbookName(xl) << std::endl;
      }
   }
   CoUninitialize();
	return 0;
}


Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

AnswerRe: Using a COM Server in an Visual Studio .vcproj Pin
Vi211-Sep-09 1:45
Vi211-Sep-09 1:45 
Questioncall to a com sometimes fails [modified] Pin
morefalt10-Sep-09 3:57
morefalt10-Sep-09 3:57 
AnswerRe: call to a com sometimes fails Pin
Md. Marufuzzaman11-Sep-09 4:07
professionalMd. Marufuzzaman11-Sep-09 4:07 
GeneralRe: call to a com sometimes fails Pin
morefalt11-Sep-09 5:17
morefalt11-Sep-09 5:17 
AnswerRe: call to a com sometimes fails Pin
Md. Marufuzzaman11-Sep-09 6:08
professionalMd. Marufuzzaman11-Sep-09 6:08 
QuestionRegistering .NET DLL without regasm [Solved] Pin
hasan03050699-Sep-09 19:29
hasan03050699-Sep-09 19:29 
AnswerRe: Registering .NET DLL without regasm Pin
Md. Marufuzzaman10-Sep-09 6:01
professionalMd. Marufuzzaman10-Sep-09 6:01 
GeneralRe: Registering .NET DLL without regasm Pin
hasan030506911-Sep-09 22:46
hasan030506911-Sep-09 22:46 
GeneralRe: Registering .NET DLL without regasm Pin
Md. Marufuzzaman12-Sep-09 3:46
professionalMd. Marufuzzaman12-Sep-09 3:46 
QuestionCOM Connection Points: Problem to call an function out of an sink class Pin
Juergen_809-Sep-09 2:04
Juergen_809-Sep-09 2:04 
AnswerRe: COM Connection Points: Problem to call an function out of an sink class Pin
Jürgen Jung9-Sep-09 3:38
Jürgen Jung9-Sep-09 3:38 
QuestionRe: COM Connection Points: Problem to call an function out of an sink class Pin
Juergen_809-Sep-09 4:59
Juergen_809-Sep-09 4:59 
AnswerRe: COM Connection Points: Problem to call an function out of an sink class Pin
Stuart Dootson9-Sep-09 10:31
professionalStuart Dootson9-Sep-09 10:31 
QuestionShell Extension + COM permission Pin
Kedrr8-Sep-09 2:25
Kedrr8-Sep-09 2:25 
QuestionCOM Connection Points: Problem to create an instance of an sink object. Pin
Juergen_808-Sep-09 0:01
Juergen_808-Sep-09 0:01 
AnswerRe: COM Connection Points: Problem to create an instance of an sink object. Pin
Stuart Dootson8-Sep-09 3:34
professionalStuart Dootson8-Sep-09 3:34 
AnswerRe: COM Connection Points: Problem to create an instance of an sink object. Pin
Juergen_808-Sep-09 4:07
Juergen_808-Sep-09 4:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.