The first com program does not use Microsoft.

Source: Internet
Author: User

One sentence: if you really understand DLL, it is too easy to implement COM. I wrote a series of DLL articles in my blog.

Of course, here I am talking about the implementation of C ++. Because com utilizes the dynamic connection feature of DLL and encapsulates the surface object, it cannot help but restrict the DLL to achieve dynamic link, it is precisely because of these restrictions that the special method of writing COM is caused. Or the encapsulation method is specialized.

// Below is the DLL part

//.............................. Interface. H ...........................

// # Define struct Interface
// The identifier of a class or interface
# Define ino 10
# Define iano 11
# Define ibno 12
//................
# Define Cano 20
//...................

Class I
{
Public:
Virtual int addref () = 0;
Virtual bool release () = 0;
Virtual bool QueryInterface (INT I _id, void ** poutinterface) = 0;
};

Class IA
{
Public:
Virtual void fna1 (int I) = 0;
Virtual void fna2 (int I) = 0;
};

Class IB
{
Public:
Virtual void fnb1 (int I) = 0;
Virtual void fnb2 (int I) = 0;
};
 

//...................................... .............................

//................ A. H ................................

# Pragma warning (Disable: 4786)

# If! Defined (afx_a_h1_bb099d15_a5f3_4e71_8760_1d2f67814bc41_encoded _)
# Define afx_a_h1_bb099d15_a5f3_4e71_8760_1d2f67814bc41_encoded _

# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000
//..............................
# Include "interface. H"
// # Include "iunknown. H"
# Include <map>
# Include <string>
Using namespace STD;
//................

Class CA: Public IA, public IB, public I
{
Public:
CA ();
Virtual ~ CA ();
Virtual int addref ();
Virtual bool release ();
Virtual bool QueryInterface (INT I _id, void ** poutinterface );
Virtual void fna1 (int I );
Virtual void fna2 (int I );
Virtual void fnb1 (int I );
Virtual void fnb2 (int I );
Protected:
Int m_iref;
Map <int, void *> m_map_iidtoi;
Public:
Typedef Map <int, void *>: value_type vtmapiidtoi;
Typedef Map <int, void *>: iterator itmapiidtoi;
};

Extern "C"
{
_ Declspec (dllexport)/* bool */void createinstance (INT cls_id, int I _id, // in
Void ** ppi); // out
};

// Only one export function is used to generate class objects.
# Endif //! Defined (afx_a_h1_bb099d15_a5f3_4e71_8760_1d2f67814bc41_encoded _)

//...................................... ................................

//...................................... ................

// A. cpp: Implementation of the CA class.
//
//////////////////////////////////////// //////////////////////////////

# Include "stdafx. H"
# Include "A. H"
# Include <iostream>
Using namespace STD;

// Trycom3.cpp: defines the entry point for the DLL application.

Bool apientry dllmain (handle hmodule,
DWORD ul_reason_for_call,
Lpvoid lpreserved
)
{
Switch (ul_reason_for_call)
{
Case dll_process_attach:
Case dll_thread_attach:
Case dll_thread_detach:
Case dll_process_detach:
Break;
}
Return true;
}

// This is the constructor of a class that has been exported.
// See trycom3.h for the class definition
//////////////////////////////////////// //////////////////////////////
// Construction/destruction
//////////////////////////////////////// //////////////////////////////

CA: Ca ()
{
// This-> m_iref = 1;
This-> m_map_iidtoi.insert (vtmapiidtoi (Ino, static_cast <void *> (static_cast <I *> (this ))));
This-> m_map_iidtoi.insert (vtmapiidtoi (iano, static_cast <Ia *> (this); // ×á ¶ ¯» áת »»
This-> m_map_iidtoi.insert (vtmapiidtoi (ibno, static_cast <IB *> (this )));
}

CA ::~ CA ()
{

}
Int CA: addref ()
{
++ M_iref;
Return m_iref;
}

Bool CA: release ()
{
M_iref --;
If (m_iref = 0)
{
Delete this;
}
Return true;
}

Bool CA: QueryInterface (INT I _id, void ** poutinterface)
{
CA: itmapiidtoi it = This-> m_map_iidtoi.find (I _id );
If (IT = This-> m_map_iidtoi.end ())
{
* Poutinterface = NULL;
Return false;
}
* Poutinterface = (* It). Second;
Return true;
}
Void CA: fna1 (int I)
{
Cout <"CA: IA: fna1 verison = 2" <"input para:" <I <Endl;
 
}
Void CA: fna2 (int I)
{
Cout <"CA: IA: fna2 verison = 2" <"input para:" <I <Endl;
}
Void CA: fnb1 (int I)
{
Cout <"CA: IA: fnb1 verison = 2" <"input para:" <I <Endl;
}
Void CA: fnb2 (int I)
{
Cout <"CA: IA: fnb2 verison = 2" <"input para:" <I <Endl;
}

//////////////////////////
// Bool
Void createinstance (INT cls_id, int I _id, // in
Void ** ppi) // out
{
If (cls_id = Cano)
{
Ca * P = new CA;
P-> QueryInterface (I _id, PPI );
If (* PPI = NULL)
{
Return; // false;
}
Return; // true ;/**/
}
Else
{
* PPI = NULL;
Return; // true;
}
}
//...................................... ..................................

// The following is the client part.

// Usetrycom3.cpp: defines the entry point for the console application.
//

# Include "stdafx. H"
# Include "windows. H"
# Include "../interface. H"
# Include <iostream>
# Include <string>
Using namespace STD;

Int main (INT argc, char * argv [])
{
Hmodule Hm =: loadlibrary ("E: // lianxi // dll_lib_dcom_com // trycom3 // debug // trycom3.dll ");
If (Hm = NULL)
{
Cout <"loadlibrary error" <Endl;
Return 0;
}
Typedef/* bool */void (* pfncreateinstance) (INT, Int, void **);
Pfncreateinstance pfnci = (pfncreateinstance): getprocaddress (HM, "createinstance ");
If (pfnci = NULL)
{
Cout <"Get createinstance address error/N ";
Return 0;
}
I * Pi = NULL;
Pfnci (Cano, Ino, (void **) & PI );
If (Pi = NULL)
{
Cout <"Get PI error/N ";
Return 0;
}
Pi-> addref ();
Ia * Pia = NULL;
Pi-> QueryInterface (iano, (void **) & PIA );
If (PIA = NULL)
{
Cout <"get Pia error/N ";
Return 0;
}
Pi-> addref ();
Pia-> fna1 (10 );
Pia-> fna2 (11 );
 
IB * PIB = NULL;
Pi-> QueryInterface (ibno, (void **) & PIB );
If (PIB = NULL)
{
Cout <"Get PIB error/N ";
Return 0;
}
Pi-> addref ();
PIB-> fnb1 (20 );
PIB-> fnb2 (21 );

Pi-> release ();
Pi-> release ();
Return 0;
}

//..................................

I have found that many books introducing com do not provide a good introduction to the explicit DLL method. Or explicitly export the Class Using DLL. In fact, it is precisely because of the DLL feature that COM must be written in this way. Or COM is caused by the encapsulation of DLL features (or limitations.

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.