锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
榪欑瘒灝辨槸寮濮嬬涓綃囦簡銆?/span>
鐢變簬宸ヤ綔姣旇緝蹇欍傛垜浠娊鍑轟竴涓槦鏈熺殑鏃墮棿錛屾湁鏃墮棿浼氬啓涓鐐廣傛妸榪欎釜緋誨垪鍐欏畬銆傛墍浠ワ紝鏈夊彲鑳芥瘡涓郴鍒楅兘姣旇緝鐭簺銆?/span>
浠庢渶鍩烘湰鐨勮璧鳳紝璁╁ぇ瀹跺交搴曟槑鐧?/span>RPC.
濂戒簡搴熻瘽涓嶅璇翠簡銆傛鏄紑濮嬨?/span>
棣栧厛錛屼綘瑕佺敤RPC錛屽繀欏誨厛鎼炴竻妤氫粈涔堟槸IDL.
Rpc鏄粈涔堬紵
http://www.shnenglu.com/alantop/archive/2007/07/09/27717.html
IDL鏄粈涔堬紵
http://www.shnenglu.com/alantop/archive/2007/07/09/27725.html
涓嬫潵錛屼婦涓緥瀛愩傛庝箞鏍鋒妸涓涓爣鍑嗙▼搴忔敼鎴愮敤IDL璇█鍐欑殑紼嬪簭銆?/span>
榪欐槸涓涓爣鍑嗙▼搴忋?/span>
// File Standalone.cpp #include <iostream>
// Future server function. void Output(const char* szOutput) { std::cout << szOutput << std::endl; }
int main() { // Future client call. Output("Hello Lonely World!"); } |
涓嬫潵鐪嬫垜浠庝箞鎶婂畠鏀逛負涓涓爣鍑?/span>IDL璇█鐨勭▼搴?/span>
鐢?/span>IDL璇█瀹氫箟鎺ュ彛錛?/span>
// File Example1.idl [ // A unique identifier that distinguishes this // interface from other interfaces. uuid(00000001-EAF3-4A7A-A0F2-BCE4C30DA77E),
// This is version 1.0 of this interface. version(1.0),
// This interface will use an implicit binding // handle named hExample1Binding. implicit_handle(handle_t hExample1Binding) ] interface Example1 // The interface is named Example1 { // A function that takes a zero-terminated string. void Output( [in, string] const char* szOutput); } |
涓婇潰榪欎釜鏂囦歡鏄垜浠敤idl璇█瀹氫箟鐨勶紝鎴戜滑瀹氫箟浜嗕竴涓帴鍙?/span>Example1, 瀹冨甫鏈?/span>uuid鍜?/span>version. 榪欎釜鎺ュ彛閲屽畾涔変簡涓涓嚱鏁?/span>Output.
UUID鏄粈涔堬紵
http://www.shnenglu.com/alantop/archive/2007/07/09/27726.html
鎺ュ彛鐨?/span>implicit_handle灞炴э紝鎴戜滑鍚庨潰鍐嶈璁恒?/span>
鎺ヤ笅鏉ュ共浠涔堝憿錛?/span>
鎴戜滑涓轟簡鍦ㄧ▼搴忎腑浣跨敤idl錛屽繀欏婚氳繃閫氳繃緙栬瘧鍣紙midl.exe錛夋妸瀹冪炕璇戞垚瀹㈡埛浠g悊鍜屾湇鍔″櫒瀛樻牴錛?/span> 浠g悊鍜屽瓨鏍瑰皢鍦ㄥ悗闈㈣鎴戜滑鐨勭紪璇戝櫒錛?/span>windows騫沖彴涓嬬殑cl.exe錛夋墍浣跨敤銆?/span>
鏀瑰ソ鐨勬湇鍔″櫒绔▼搴忥細
// File Example1Server.cpp #include <iostream> #include "Example1.h"
// Server function. void Output(const char* szOutput) { std::cout << szOutput << std::endl; }
int main() { RPC_STATUS status;
// Uses the protocol combined with the endpoint for receiving // remote procedure calls. status = RpcServerUseProtseqEp( reinterpret_cast<unsigned char*>("ncacn_ip_tcp"), // Use TCP/IP // protocol. RPC_C_PROTSEQ_MAX_REQS_DEFAULT, // Backlog queue length for TCP/IP. reinterpret_cast<unsigned char*>("4747"), // TCP/IP port to use. NULL); // No security.
if (status) exit(status);
// Registers the Example1 interface. status = RpcServerRegisterIf( Example1_v1_0_s_ifspec, // Interface to register. NULL, // Use the MIDL generated entry-point vector. NULL); // Use the MIDL generated entry-point vector.
if (status) exit(status);
// Start to listen for remote procedure // calls for all registered interfaces. // This call will not return until // RpcMgmtStopServerListening is called. status = RpcServerListen( 1, // Recommended minimum number of threads. RPC_C_LISTEN_MAX_CALLS_DEFAULT, // Recommended //maximum number of threads. FALSE); // Start listening now.
if (status) exit(status); }
// Memory allocation function for RPC. // The runtime uses these two functions for allocating/deallocating // enough memory to pass the string to the server. void* __RPC_USER midl_user_allocate(size_t size) { return malloc(size); }
// Memory deallocation function for RPC. void __RPC_USER midl_user_free(void* p) { free(p); } |
榪欐槸鍒濆鍖栵紝鍜屾敞鍐屾帴鍙g殑浠g爜銆?/span>
鐜板湪鐪嬬湅鎬庝箞鍐欏鎴風
// File Example1Client.cpp #include <iostream> #include "Example1.h"
int main() { RPC_STATUS status; unsigned char* szStringBinding = NULL;
// Creates a string binding handle. // This function is nothing more than a printf. // Connection is not done here. status = RpcStringBindingCompose( NULL, // UUID to bind to. reinterpret_cast<unsigned char*>("ncacn_ip_tcp"), // Use TCP/IP // protocol. reinterpret_cast<unsigned char*>("localhost"), // TCP/IP network // address to use. reinterpret_cast<unsigned char*>("4747"), // TCP/IP port to use. NULL, // Protocol dependent network options to use. &szStringBinding); // String binding output.
if (status) exit(status);
// Validates the format of the string binding handle and converts // it to a binding handle. // Connection is not done here either. status = RpcBindingFromStringBinding( szStringBinding, // The string binding to validate. &hExample1Binding); // Put the result in the implicit binding // handle defined in the IDL file.
if (status) exit(status);
RpcTryExcept { // Calls the RPC function. The hExample1Binding binding handle // is used implicitly. // Connection is done here. Output("Hello RPC World!"); } RpcExcept(1) { std::cerr << "Runtime reported exception " << RpcExceptionCode() << std::endl; } RpcEndExcept
// Free the memory allocated by a string. status = RpcStringFree( &szStringBinding); // String to be freed.
if (status) exit(status);
// Releases binding handle resources and disconnects from the server. status = RpcBindingFree( &hExample1Binding); // Frees the implicit binding handle defined in // the IDL file.
if (status) exit(status); }
// Memory allocation function for RPC. // The runtime uses these two functions for allocating/deallocating // enough memory to pass the string to the server. void* __RPC_USER midl_user_allocate(size_t size) { return malloc(size); }
// Memory deallocation function for RPC. void __RPC_USER midl_user_free(void* p) { free(p); } |
HRESULT Method2( [in] short arg1,[out,retval] short * parg2);
鍦↗ava璇█涓紝搴旇琚槧灝勪負涓嬮潰鐨勫嚱鏁幫細
public short Method2(short arg1);
鑰屽湪Visual Basic涓紝鏂規硶瀹氫箟濡備笅;
Function Method2(arg1 as Integer) as Integer
鍥犱負C++娌℃湁涓撻棬鐨勮繍琛屾椂搴撴潵鏀寔COM鎺ュ彛璁塊棶鎿嶄綔錛屾墍浠icrosoft C++鎶婅繖涓柟娉曟槧灝勭▼涓嬮潰鐨勫嚱鏁幫細
virtual HRESULT __stdcall Method2(short arg1,short* parg2 ) = 0;
//
// interface IVAIS wrapper method implementations
//
inline char IVAIS::ExecVerifyAIS ( _bstr_t AisContent, short length ) {
聽聽聽 char _result;
聽聽聽 HRESULT _hr = raw_ExecVerifyAIS(AisContent, length, &_result);
聽聽聽 if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
聽聽聽 return _result;
}
聽
鏅鴻兘鎸囬拡璋冪敤鍑芥暟鐨勬柟娉曪細
銆銆聽 spFun.CoCreateInstance(...);聽// 絳変環涓?API 鍑芥暟::CoCreateInstance(...)
銆銆聽 spFun.QueryInterface(...);聽// 絳変環涓?API 鍑芥暟::QueryInterface()
銆銆聽
銆銆聽 spFun->Add(...);聽// 璋冪敤鍐呴儴鎺ュ彛鎸囬拡鐨勬帴鍙e嚱鏁?
銆銆聽 // 璋冪敤鍐呴儴鎺ュ彛鎸囬拡鐨凲ueryInterface()鍑芥暟錛屽叾瀹炴晥鏋滃拰 spFun.QueryInterface(...) 涓鏍?br />銆銆聽 spFun->QueryInterface(...);聽
銆銆聽
銆銆聽 spFun.Release();聽// 閲婃斁鍐呴儴鐨勬帴鍙f寚閽堬紝鍚屾椂鍐呴儴鎸囬拡璧嬪間負 NULL
銆銆聽 spFun->Release();聽// 閿欙紒錛侊紒涓瀹氫笉瑕佽繖涔堜嬌鐢ㄣ?br />銆銆聽 // 鍥犱負榪欎釜璋冪敤騫朵笉鎶婂唴閮ㄦ寚閽堟竻絀猴紝閭d箞鏋愭瀯鐨勬椂鍊欎細琚啀嬈¢噴鏀撅紙閲婃斁浜嗕袱嬈★級
鍖呭 (containment) 錛氫竴涓粍浠跺寘鍚叾浠栫粍浠剁殑鎺ュ彛鎸囬拡銆?/span>
鑱氬悎 (aggregation) 錛氬閮ㄧ粍浠剁洿鎺ヤ紶閫掑唴閮ㄧ粍浠剁殑鎺ュ彛鎸囬拡緇欏鎴風殑緇勪歡鐨勫鐢ㄦ満鍒?/span> .