Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I easily use a COM component in Native Visual C++

I am trying to build an app that uses a COM component in VisualStudio ´05 in native C++. The mix of native and managed desciptions of things in the MSDN totally wrecked my brain. (I think the MSDN is a total mess in that respect) I need a short and simple native C++ sample of code to load my Component and make it usable. I am ok with the compiler creating wrappers and the like.

Please don't advise me to use the dialog based MFC example, because it does not work with this component and is in itself a huge pile of c... code.

Can this be an issue native com vs managed com?

I am totally lost, please give me some bearings...

EDIT: Thanks for all the help. My problem is that all I have is a registered dll (actually the OCX, see below) . I (personally) know what the Interface should look like, but how do I tell my program? There are no headers that define IDs for Interfaces that I could use. But I read that the c++ compiler can extract and wrap it up for me. Anyone know how this is done?

CLARIFICATION: I have only the OCX and a clue from the documentation of the component, what methods it should expose.

like image 407
AndreasT Avatar asked Jul 17 '09 21:07

AndreasT


People also ask

How do I reference a DLL in Visual Studio C++?

In Visual C++, click the Browse tab in the Add References dialog box. Click Browse, locate the component that you want on your local drive, and then click OK. The component is added to the Selected Components field.

Does Visual Studio community have C?

Visual Studio Code is a lightweight, cross-platform development environment that runs on Windows, Mac, and Linux systems. The Microsoft C/C++ for Visual Studio Code extension supports IntelliSense, debugging, code formatting, auto-completion. Visual Studio for Mac doesn't support Microsoft C++, but does support .

How do I add a DLL to a C++ project in Visual Studio?

On the menu bar, choose File > New > Project to open the New Project dialog box. In the left pane of the New Project dialog box, select Installed > Visual C++ > Windows Desktop. In the center pane, select Dynamic-Link Library (DLL).

How do I get CLR in Visual Studio?

In the Visual Studio IDE, the /clr compiler option can be individually set on the Configuration Properties > C/C++ > General page of the Property Pages dialog. However, we recommend you use a CLR template to create your project.


2 Answers

Fully working example (exactly what you need) from my blog article: How to Call COM Object from Visual Studio C++?

// https://helloacm.com/how-to-call-com-object-from-visual-studio-c/
#include <iostream>
#include <objbase.h>
#include <unknwn.h>
#include <Propvarutil.h>
#import "wshom.ocx" no_namespace, raw_interfaces_only  

using namespace std;

int main() {
    HRESULT hr;
    CLSID clsid;
    CoInitializeEx(nullptr, COINIT_MULTITHREADED);  
    CLSIDFromProgID(OLESTR("WScript.Shell"), &clsid);   
    IWshShell *pApp = nullptr;
    hr = CoCreateInstance(clsid, nullptr, CLSCTX_INPROC_SERVER, __uuidof(IWshShell), reinterpret_cast<LPVOID *>(&pApp));
    if (FAILED(hr) || pApp == nullptr) {
        throw "Cannot Create COM Object";
    }
    int out;
    VARIANT s;
    InitVariantFromInt32(0, &s);
    VARIANT title;
    InitVariantFromString(PCWSTR(L"title"), &title);
    VARIANT type;
    InitVariantFromInt32(4096, &type);
    BSTR msg = ::SysAllocString(L"Hello from https://helloacm.com");
    pApp->Popup(msg, &s, &title, &type, &out);
    CoUninitialize();
    cout << "Out = " << out;
    return 0;
}
like image 126
justyy Avatar answered Sep 21 '22 10:09

justyy


The bare minimums for instantiating a COM object are as follows:

1) Must have a COM apartment available.

This is accomplished by most applications by calling CoInitialize / CoInitializeEx to setup the COM library and the initial COM apartment if it is the first time for a process.

2) Call CoCreateInstance / CoCreateInstanceEx to create an object, and specify flags to denote how it will be instantiated.

3) Properly balance calls of AddRef and Release on the interfaces of any COM components that you create, calling the last Release() when you are done using a COM component.

-

In a managed application, #1 is almost always handled for you. #2 is abstracted away if you import a reference to a COM library, and you can just use the imported names as if they were .NET class definitions and such. #3 is automatically handled for you, but your needs may vary. Unfortunately, there are sometimes quirks in how references are handled in managed applications, which can cause COM objects to stick around longer than intended. The Marshal helper class in System.Runtime has methods that can help you out there if you encounter issues.

-

In an unmanaged application, you will have to do some legwork if you are creating an application from scratch.

  1. Call CoInitialize/CoInitializeEx early in your application's main thread to setup the apartment.
  2. When your application's main thread is about to exit, call CoUninitialize() to close out the apartment.
  3. For additional threads that you create, you should also call CoInitialize/CoInitializeEx when they start if you need to use COM objects from those threads. Also, depending on your application you may want to set the apartment parameters.
  4. For those threads, also call CoUninitialize() when they are exiting to cleanup properly.
like image 43
meklarian Avatar answered Sep 18 '22 10:09

meklarian