To summarize the method for calling the COM component in VC (you can add)

Source: Internet
Author: User
Prerequisites:
The COM server is an in-process server, and the DLL name is simpcom. dll. This component has only one interface ifoo, and this interface has only one method hresult sayhello (void)


Call in SDK
============================================
1. The simplest and most common method is to use the # Import Type Library and the smart pointer packaging class provided by VC.
DEMO code:

# Import "D: \ temp \ Vc \ simpcom \ debug \ simpcom. dll" no_namespace
Coinitialize (null );

Ifooptr spfoo = NULL;
Spfoo. createinstance (_ uuidof (FOO ));
Spfoo-> sayhello ();
Spfoo. release ();/* dizzy. The smart pointer was designed to prevent users from worrying about this, but I found that if you do not call it manually, A memory access error occurs after the program exits. I tried it on the console. Which of the following heroes knows what is going on */
Couninitialize ();



2. Import the *. H, * _ I. c file generated by midl.exe and call it using the cocreateinstance function.

DEMO code:
/* Add the * _ I. c file to the project. For example, the simpcom_ I .c file in this example defines the guid value of the class and interface. If it is not introduced, a connection error occurs. */

# Include "D: \ temp \ Vc \ simpcom. H"
Coinitialize (null );

Ifoo * pfoo = NULL;
Hresult hR = cocreateinstance (clsid_foo, null, clsctx_all, iid_ifoo, (void **) & pfoo );
If (succeeded (HR) & (pfoo! = NULL ))
{
Pfoo-> sayhello ();
Pfoo-> release ();
}

Couninitialize ();

3. Use cocreateinstance to obtain the class factory object interface directly using cogetclassobejct, and then use the createinstance method of this interface to generate an instance.
DEMO code:
/* Preparations are described in method 2 */
Iclassfactory * PCF = NULL;
Hresult hR = cogetclassobject (clsid_foo, clsctx_all, null, iid_iclassfactory, (void **) & PCF );
If (succeeded (HR) & (PCF! = NULL ))
{
Ifoo * pfoo = NULL;
HR = PCF-> createinstance (null, iid_ifoo, (void **) & pfoo );
If (succeeded (HR) & (pfoo! = NULL ))
{
Pfoo-> sayhello ();
Pfoo-> release ();
}
PCF-> release ();
}



4. You do not need to use cocreateinstance or cogetclassobject to directly obtain dllgetclassobject from the DLL, and then generate class objects and class instances (this method is suitable for you to use a component, but do not want to register this component in the Registry)

DEMO code:
/* Preparations are described in method 2. In fact, you only need to get the definition of CLSID and IID and interface */

Typedef hresult (_ stdcall * pfngco) (refclsid, refiid, void **);
Pfngco fngco = NULL;
Hinstance hdllinst = loadlibrary ("d :\\ Temp \ Vc \ simpcom \ debug \ simpcom. dll ");
Fngco = (pfngco) getprocaddress (hdllinst, "dllgetclassobject ");
If (fngco! = 0)
{
Iclassfactory * PCF = NULL;
Hresult hR = (fngco) (clsid_foo, iid_iclassfactory, (void **) & PCF );
If (succeeded (HR) & (PCF! = NULL ))
{
Ifoo * pfoo = NULL;
HR = PCF-> createinstance (null, iid_ifoo, (void **) & pfoo );
If (succeeded (HR) & (pfoo! = NULL ))
{
Pfoo-> sayhello ();
Pfoo-> release ();
}
PCF-> release ();
}
}
Freelibrary (hdllinst );


Call in MFC
============================================
In addition to the above methods, there is also a more convenient method in MFC, that is, using classwizard to generate a packaging class using the type library, however, the premise is that the COM Component Interface must be derived from idispatch.

Specific Method:
1. Press Ctrl + W to bring up the Class Wizard, press add class to bring up a new menu, select from a type Libarary, and then go to simpcom. DLL, and then all the interfaces in the simpcom will come out. After selecting the interface packaging class you want to generate, The Wizard will automatically generate the corresponding. CPP and. h file.
In this way, you can use the COM component like a common class in your MFC project.

DEMO code:

Coinitialize (null );

Ifoo Foo;
If (FOO. createdispatch ("simpcom. foo ")! = 0)
{
Foo. sayhello ();
Foo. releasedispatch ();
}

Couninitialize ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.