




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、陳澤斌1106100190我們從一個(gè)簡單的rpc “hello, world!”的例子開始。參考資料:msdn: win32 and com development -> networking -> networkprotocols -> remote procedure calls (rpc)第 1 步:編寫 idl ( interface description language ,接口描述語言)文件idl是一個(gè)通用的工業(yè)標(biāo)準(zhǔn)語言,大家應(yīng)該不陌生,因?yàn)閏om里面也是用它 來描述接口的。hello.idl:uuid(”4556509f618a46cfab3ded736ed6
2、6477”),/ 唯一的 uuid , 用guidgen生成version(i.o)interface helloworld/我們定義的方法void hello(in,stringconst char * psz);void shutdown(void);一個(gè)可選的文件是應(yīng)用程序配置文件(.acf),它的作用是對rpc接口進(jìn)行配 置,例如下面的hello.acf文件:hello.acf:implicit_handle(handle_t helloworld_binding)interface helloworld上面定義了 implicit_handle ,這樣客戶端將綁定句柄hellowor
3、ld_binding 了 , 后面的客戶端代碼中我們會看到。編譯idl文件:>midl hello.idlmicrosoft (r) 32b/64b midl compiler version 6.00.0366copyright (c) microsoft corporation 1991-2002. all rights reserved.processing ahello.idlhello.idlprocessing ahello.acfhello.acf我們可以看到自動生成了 hello.h, hello_s.c, hello_c.c文件,這些叫做rpcstub程序,不過我們可以
4、不管這個(gè)概念,我們只需要知道hello.h里面定義了一個(gè)extern rpc if handle helloworld v1 0 s ifspec;這個(gè)rpc if_handle將在后面用到。第2步:編寫服務(wù)端程序第1步中我們已經(jīng)約定了調(diào)用的接口,那么現(xiàn)在我們開始實(shí)現(xiàn)其服務(wù)端。代碼如下:server.c#include <stdlib.h>#include <stdio.h>include “hello.h” /引用midl生成的頭文件*這是我們在idl中定義的接口方法 * 需要注意一點(diǎn),idl 里面的聲明是:void hello(in,stringconst char
5、 * psz);*但是這里變成了 const unsigned char * ,為什么呢?* 參見 msdn 中的 midl command-line reference -> /char switch*默認(rèn)的編譯選項(xiàng),對idl中的char按照unsigned char處理*/void hello(const unsigned char * psz)printf(,%sn,', psz);/*這也是我們在idl中定義的接口方法,提供關(guān)閉server的機(jī)制*7void shutdown(void)/下面的操作將導(dǎo)致rpcserverlisten()退出rpcmgmtstopserv
6、erlistening(null);rpcserverunregisterlf(null, null, false);int m8in(int argc.char * argv)/用named pipe作為rpc的通道這樣endpoint參數(shù)就是named pipe的名字/按照named pipe的命名規(guī)范,pipepipename,其中pipename可以是除了 /之外的任意字符,那么這里用一個(gè)guid串來命名,可以保證不會重復(fù)rpcserveruseprotseqep(unsigned char *)nncacn_np”,20, (unsigned char *),wpipe8dd5020
7、5-3108-498f-96e8-dbc4ec074cf9n, null);/ 注冊接口 , helloworld_v1_0_sjfspec 是在 midl 生成的 hello.h 中定義 的rpcserverregisterlf(helloworld. v1 0. s.jfspec, null, null);/開始監(jiān)聽,本函數(shù)將一直阻塞rpcserverlisten(1,20,false);return 0;/下面的函數(shù)是為了滿足鏈接需要而寫的,沒有的話會岀現(xiàn)鏈接錯誤void rpc_far* rpc_user midl_user_allocate(size_t len)return(mal
8、loc(le n);void _rpc_user midl_user_free(void _rpc_far *ptr)free(ptr);編譯:>cl /d_win32_winnt=0x500 server.c hello_s.c rpcrt4.lib用于 80x86 的 microsoft (r) 32 位 c/c+ 優(yōu)化編譯器 14.00.50727.42 版 版權(quán)所有(c) microsoft corporation0保留所有權(quán)利。server.chello s.c正在生成代碼microsoft (r) incremental linker version 8.00.50727.4
9、2copyright (c) microsoft corporation. all rights reserved./out:server.exeserver.objhello_s.objrpcrt 4ib編譯時(shí)為什么要指定_win32_winnt=0x500呢?因?yàn)槿绻麤]有的話會報(bào)告 下面的錯誤:hello_s.c(88): fatal error c1189: #error: you need a windows 2000 or later torun this stub because it uses these features:第3步:編寫客戶端程序客戶端的代碼:client.c#i
10、nclude <stdlib.h>#include <stdio.h>#inelude <string.h>include “hello.h” /引用midl生成的頭文件int main(int argc, char * argv)unsigned char * pszstringbinding = null;if ( argc != 2 )printf("usage:%s <hello text>n", argv0);return 1;/用named pipe作為rpc的通道。參見server.c中的rpcserveruse
11、protseqep()部分第3個(gè)參數(shù)networkaddr如果取null ,那么就是連接本機(jī)服務(wù)/否則要取wwservername這樣的格式,例如你的計(jì)算機(jī)名為jack ,那么就是 wjackrpcstringbindingcompose( null, (unsigned char*)nncacn_np”,廣(unsigned char*),'servername',7 null, (unsigned char*)hpipe8dd50205-3108-498f-96e8-dbc4ec074cf9n, null,&pszstringbinding );/綁定接口,這里要和
12、hello.acf的配置一致,那么就是helloworld_bindingrpcbindingfromstringbinding(pszstringbinding, &helloworld_binding );/下面是調(diào)用服務(wù)端的函數(shù)了rpctry exceptif (_stricmp(argv1, 'shutdown'1) = 0 )shutdown();elsehello(unsigned char*)argv1);rpcexcept(l)printf( "rpc exception %dn", rpcexceptioncode();rpcend
13、except/釋放資源rpcstringfree(&pszstringbinding);rpcbindingfree(&helloworld_binding);return 0;/下面的函數(shù)是為了滿足鏈接需要而寫的,沒有的話會岀現(xiàn)鏈接錯誤void rpc_far* rpc_user midl_user_allocate(size_t len)return(malloc(le n);void _rpc_user midl_user_free(void _rpc_far *ptr)free(ptr);編譯:>cl /d_win32_winnt=0x500 client.c hello_c.c rpcrt4.lib用于 80x86 的 microsoft (r) 32 位 c/c+ 優(yōu)化編譯器 14.00.50727.42 版 版權(quán)所有(c) microsoft corporationo保留所有權(quán)利。client.chello_c.c正在生成代碼microsoft (r) incremental linker version 8.00.50727.42copyright (c) microsoft corporatio
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年上海市安全員-C證考試題庫
- 2025江西省建筑安全員《B證》考試題庫
- 賣衣服投資合同范例
- 2025年黑龍江省安全員C證考試(專職安全員)題庫附答案
- 廠房土地改造合同范例
- 二年級口算題庫大全100道
- 北京中學(xué)培訓(xùn)課件
- 深空探測載荷技術(shù)驗(yàn)證報(bào)告
- 抗量子加密的學(xué)術(shù)資源索引
- 2025年重慶市安全員B證考試題庫附答案
- 產(chǎn)科腹部四步觸診要點(diǎn)
- 第10課 人類社會及其發(fā)展規(guī)律-【中職專用】2024年中職思想政治《哲學(xué)與人生》金牌課件(高教版2023·基礎(chǔ)模塊)
- SLT 478-2021 水利數(shù)據(jù)庫表結(jié)構(gòu)及標(biāo)識符編制總則
- 2024年春學(xué)期人教版小學(xué)道德與法治六年級下冊教學(xué)計(jì)劃附教學(xué)進(jìn)度表
- 深度學(xué)習(xí)視角下“尺規(guī)作圖”教學(xué)策略
- 2024 年袋鼠數(shù)學(xué)競賽 等級E(中國區(qū))
- 2024年南京旅游職業(yè)學(xué)院單招職業(yè)適應(yīng)性測試題庫匯編
- 2024-2030中國半導(dǎo)體閥門及管接頭市場現(xiàn)狀研究分析與發(fā)展前景預(yù)測報(bào)告
- 公務(wù)員面試考官培訓(xùn)
- 2024年湖南生物機(jī)電職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫及一套參考答案
- 繽紛天地美食街運(yùn)營方案
評論
0/150
提交評論