Use atl_com to create the first local server

Source: Internet
Author: User
Tags cmath

Use atl_com to create the first local server
Vc6
1. file --> New --> Project ---> atl com Project
Set Project name: localserver

2. Select executable under server type, and then select finish

3. the system automatically generates some files, which are important:
Localserver. IDL // interface definition file
Localserver. cpp // executable file (localserver.exe is generated after the connection is compiled)
Localserverps. mk // makefile

3. Add a new interface definition in localserver. IDL

// If you do not know the meaning of the above things, complete IDL.
// Note that you can use the idea to generate an interface, and you can also generate components (classes that implement this interface) that correspond to this interface)
// Use the following method: insert --> new ATL Object --> select simple object in object -->
In the displayed dialog box, select "ATL object wizard properties", select "names", and enter "mathb" in "shortname.
Then select attribute and select appartment, custom, yes, support isuppoerterrorinfo
Select OK. A new imath interface is added to IDL and a new component cmath is defined. This component implements imath, then we will make this component implement more interfaces ..
At this time, the system generates a new file math. H. cmath header file.
Math. cpp cmath implementation file.
The registar script file of the math. RGS math component. With him, you don't have to write something to register the component to the Registry.

4. Add a function for imath
In classview, place the cursor at imath and select Add method. Then fill in the return value, function name, parameter
Select OK, and the system also adds the corresponding code to the cmath.
// Here, the system adds the following content to the IDL file:
[
Object,
UUID (73dedcfd-c87b-4e68-b6d1-a36fa3316dcd ),
 
Helpstring ("imath interface "),
Pointer_default (unique)
]
Interface imath: iunknown
{
[Helpstring ("Method Add")] hresult add ([in] int ipara1, [in] int ipara2, [out] int * presult );
};
// Add the following content for math. h:
// Cmath
Class atl_no_vtable cmath:
Public ccomobjectrootex <ccomsinglethreadmodel>,
Public ccomcoclass <cmath, & clsid_math>,
Public isupporterrorinfo,
Public imath ,//...........
Public iadvancedmath //.....................
{
Public:
Cmath ()
{
}

Declare_registry_resourceid (idr_math)

Declare_protect_final_construct ()

Begin_com_map (cmath)
Com_interface_entry (imath)
Com_interface_entry (iadvancedmath)
Com_interface_entry (isupporterrorinfo)
End_com_map ()

// Isupportserrorinfo
Stdmethod (interfacesupportserrorinfo) (refiid riid );

// Imath
Public:
Stdmethod (ADD) (/* [in] */INT ipara1,/* [in] */INT ipara2,/* [out] */int * presult ); //.,....

Public:
// Iadvancedmath
// Manually added interface
Stdmethod (advancedadd) (/* In */INT ipara1,/* [in] */INT ipara2,/* [out] */int * presult );
};

// Content added to math. cpp
Stdmethodimp cmath: add (INT ipara1, int ipara2, int * presult)
{
// Todo: add your implementation code here
* Presult = ipara1 + ipara2;
Cout <"cmath: Add/N ";
Return s_ OK;
}
5. manually add the new interface iadvancedmath (the wizard used above defines an interface and creates a component for it each time. Therefore, you cannot use the Wizard to add multiple interfaces to a component ). Add a function
Add:
// Add Manual
[
Object,
UUID (63ffa301-5bb0-4e0a-95cf-92c22ff1b796 ),
 
Helpstring ("iadvancedmath interface "),
Pointer_default (unique)
]
Interface iadvancedmath: iunknown
{
// Added functions
[Helpstring ("Method Add")] hresult advancedadd ([in] int ipara1, [in] int ipara2, [out] int * presult );
};

 

[
UUID (0c01d7a4-d53a-4df2-9d7a-1f76140e1c04 ),
Version (1.0 ),
Helpstring ("localserver 1.0 Type Library ")
]
Library localserverlib
{
Importlib ("stdole32.tlb ");
Importlib ("stdole2.tlb ");

[
UUID (82139624-d5ff-4418-8a11-eaa7ef381147 ),
Helpstring ("math class ")
]
Coclass math
{
[Default] interface imath; // automatically added
Interface iadvancedmath; //
};
};
Add public iadvancedmath to cmath to inherit iadvancedmath.
Add for cmath
Public:
// Iadvancedmath
Stdmethod (advancedadd) (/* In */INT ipara1,/* [in] */INT ipara2,/* [out] */int * presult );

And its implementation:
Stdmethodimp cmath: advancedadd (/* In */INT ipara1,/* [in] */INT ipara2,/* [out] */int * presult)
{
* Presult = ipara1 + ipara2;
 
Cout <"cmath: advancedadd/N ";
Return s_ OK;
}
6.establish localserver.exe
7. Enter the command line in the current directory.
Execute the command: midl localserver. IDL // This step can be omitted
Run the command: nmake-F localserverps. mk // to generate proxy_stub.dll // The DLL name may be different. Here is an example.
Run the following command: regsvr32.exe proxy_stub.dll // register the stub proxy DLL

//.............
In the preceding steps: Add Interfaces and implementation interfaces (components)
Register proxy stubs at the same time.
Note that there are not many files to be modified! ·!!! ··
//////////////////////////////////////// //////////////////
//............
Generate the client:
Create a simple console program:

# Include "stdafx. H"
// The Order of the following two contained files is important
# Include "../localserver. H"
# Include "../localserver_ I .c"

# Include <iostream>
Using namespace STD;

Int main (INT argc, char * argv [])
{
Cout <"in client: init COM/N ";
If (failed (: coinitialize (null )))//......
{
Cout <"init com error/N ";
Return 0;
}
Imath * PM;
Hresult hr;

HR =: cocreateinstance (clsid_math ,//..........
Null, clsctx_local_server, iid_imath, (void **) & PM );
If (failed (HR ))
{
Cout <"Get pimath error hR =" <HR <"/N ";
If (hR = regdb_e_classnoreg)
{
Cout <"regdb_e_classnoregulatory" <Endl;
}
If (hR = class_e_noaggregation)
{
Cout <"class_e_noaggregation" <Endl;
}
: Couninitialize ();
Return 0;
}
Int rs = 0;
PM-> Add (10, 20, & RS );
Cout <"result is:" <RS <Endl;
Iadvancedmath * PAM;
HR = PM-> QueryInterface (iid_iadvancedmath, (void **) & Pam );
If (failed (HR ))
{
Cout <"Get Pam error hR =" <HR <"/N ";
If (hR = regdb_e_classnoreg)
{
Cout <"regdb_e_classnoregulatory" <Endl;
}
If (hR = class_e_noaggregation)
{
Cout <"class_e_noaggregation" <Endl;
}
PM-> release ();
: Couninitialize ();
Return 0;
}
Pam-> advancedadd (50,100, & RS );
Cout <"result is:" <RS <Endl;
PM-> release ();
Pam-> release ();
: Couninitialize ();
Return 0;
}
You can directly run the client after it is created ..
(If the server has not been started, the client will start the server in some way ..)

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.