如何在本地进程中调用远端计算机上的进程
2007-08-04 09:15
351 查看
回答这个问题其实就是RPC的编程:开发RPC应用程序,一个重要的要素就是接口。显然在客户和服务器的接口存根必须基于完全相同的函数定义上;否则RPC会失败。
1.定义接口(HELLO.IDL):
[
uuid(0EB13191-F7C4-11d3-BF4A-00104B17A4FB),/*注意你必须在自己的程序中生成自己的UUID,利用GUIDGEN应用程序,在VISUAL STUDIO中*/
version(1.0)
]
2.应用程序配置文件(HELLO.ACF)
[
implicit_handle(handle_t hello_IfHandle)
//implicit_handle 的使用规定作为全局变量维护句柄
]
interface hello
{
}
3.编译HELLO.IDL
MIDL HELLO.IDL
编译的结果生成HELLO_C.C,HELLO_S.C,HELLO.H
你看一下这些文件,发现了什么?!
4.编写服务器程序
#include <stdlib.h>
#include <stdio.h>
#include "hello.h"
void HelloProc(const unsigned char* pszString)
{
printf("%s/n",pszString);
}
void Shutdown(void)
{
RpcMgmtStopServerListening(NULL);
RpcServerUnregisterIf(NULL,NULL,FALSE);
}
void main(int argc,char* argv[])
{
RpcServerUseProtseqEp((unsigned char *)"ncacn_ip_tcp",20,(unsigned char *)"8000",NULL);
RpcServerRegisterIf(hello_v1_0_s_ifspec,NULL,NULL);
RpcServerListen(1,20,FALSE);
}
void __RPC_FAR* __RPC_USER midl_user_allocate(size_t len)
{
return(malloc(len));
}
void __RPC_USER midl_user_free(void __RPC_FAR *ptr)
{
free(ptr);
}
interface hello
{
void HelloProc([in,string]const unsigned char* pszString);
void Shutdown(void);
}
5.编译服务器程序
CL HELLOS.C HELLO_S.C RPCRT4.LIB
生成HELLOS.EXE
6.编写客户程序
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "hello.h"
void main(int argc,char* argv[])
{
unsigned char* pszStringBinding;
if(argc!=3)
{
printf("Usage:%s hostname string-to-print/n",argv[0]);
exit(1);
}
RpcStringBindingCompose(NULL,(unsigned char*)"ncacn_ip_tcp",(unsigned char*)argv[1],(unsigned char*)"8000",NULL,&pszStringBinding);
RpcBindingFromStringBinding(pszStringBinding,&hello_IfHandle);
if(strcmp(argv[2],"SHUTDOWN"))HelloProc((unsigned char*)argv[2]);
else Shutdown();
RpcStringFree(&pszStringBinding);
RpcBindingFree(&hello_IfHandle);
exit(0);
}
void __RPC_FAR* __RPC_USER midl_user_allocate(size_t len)
{
return(malloc(len));
}
void __RPC_USER midl_user_free(void __RPC_FAR *ptr)
{
free(ptr);
}
7.编译客户程序
CL HELLOC.C HELLO_C.C RPCRT4.LIB
生成HELLOC.EXE
8.测试程序
在两个DOS窗口中运行,一个运行HELLOS,在同一台机器上,另一个运行HELLOC LOCALHOST “HELLO”
看看发生了什么?
1.定义接口(HELLO.IDL):
[
uuid(0EB13191-F7C4-11d3-BF4A-00104B17A4FB),/*注意你必须在自己的程序中生成自己的UUID,利用GUIDGEN应用程序,在VISUAL STUDIO中*/
version(1.0)
]
2.应用程序配置文件(HELLO.ACF)
[
implicit_handle(handle_t hello_IfHandle)
//implicit_handle 的使用规定作为全局变量维护句柄
]
interface hello
{
}
3.编译HELLO.IDL
MIDL HELLO.IDL
编译的结果生成HELLO_C.C,HELLO_S.C,HELLO.H
你看一下这些文件,发现了什么?!
4.编写服务器程序
#include <stdlib.h>
#include <stdio.h>
#include "hello.h"
void HelloProc(const unsigned char* pszString)
{
printf("%s/n",pszString);
}
void Shutdown(void)
{
RpcMgmtStopServerListening(NULL);
RpcServerUnregisterIf(NULL,NULL,FALSE);
}
void main(int argc,char* argv[])
{
RpcServerUseProtseqEp((unsigned char *)"ncacn_ip_tcp",20,(unsigned char *)"8000",NULL);
RpcServerRegisterIf(hello_v1_0_s_ifspec,NULL,NULL);
RpcServerListen(1,20,FALSE);
}
void __RPC_FAR* __RPC_USER midl_user_allocate(size_t len)
{
return(malloc(len));
}
void __RPC_USER midl_user_free(void __RPC_FAR *ptr)
{
free(ptr);
}
interface hello
{
void HelloProc([in,string]const unsigned char* pszString);
void Shutdown(void);
}
5.编译服务器程序
CL HELLOS.C HELLO_S.C RPCRT4.LIB
生成HELLOS.EXE
6.编写客户程序
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "hello.h"
void main(int argc,char* argv[])
{
unsigned char* pszStringBinding;
if(argc!=3)
{
printf("Usage:%s hostname string-to-print/n",argv[0]);
exit(1);
}
RpcStringBindingCompose(NULL,(unsigned char*)"ncacn_ip_tcp",(unsigned char*)argv[1],(unsigned char*)"8000",NULL,&pszStringBinding);
RpcBindingFromStringBinding(pszStringBinding,&hello_IfHandle);
if(strcmp(argv[2],"SHUTDOWN"))HelloProc((unsigned char*)argv[2]);
else Shutdown();
RpcStringFree(&pszStringBinding);
RpcBindingFree(&hello_IfHandle);
exit(0);
}
void __RPC_FAR* __RPC_USER midl_user_allocate(size_t len)
{
return(malloc(len));
}
void __RPC_USER midl_user_free(void __RPC_FAR *ptr)
{
free(ptr);
}
7.编译客户程序
CL HELLOC.C HELLO_C.C RPCRT4.LIB
生成HELLOC.EXE
8.测试程序
在两个DOS窗口中运行,一个运行HELLOS,在同一台机器上,另一个运行HELLOC LOCALHOST “HELLO”
看看发生了什么?
相关文章推荐
- C#如何获取本地计算机的状态
- 如何通过Html网页调用本地安卓app
- Web Service 调用出现“测试窗体只能用于来自本地计算机的请求”解决办法。
- 如何用putty从本地Windows平台上传文件到远端Linux平台
- 阿里云上部署webservice其他机器调用提示“测试窗体只能用于来自本地计算机的请求”问题
- 如何通过组策略将指定用户加入本地计算机管理员组
- 判断本地计算机是否启动指定进程
- IE浏览器中如何安全地调用本地可执…
- 如何通过Html网页调用本地安卓app?
- Js如何调用本地应用程序
- 如何在本地计算机连接使用运行在虚拟机上的oracle数据库
- java如何调用本地的浏览器
- 如何将已有的本地Git 库推送到远端仓库?
- 如何查看本地端口被进程占用的情况?
- 如何在jsp中显示本地计算机上的图片文件
- Mysql 本地计算机无法启动 mysql 服务 错误 1067:进程意外终止。
- 关闭系统进程,以及如何调用CMD并执行命令
- c#关闭系统进程以及如何调用cmd并执行命令
- 如何查看计算机那个端口被占用,以及被那个进程所占用
- MapReduce程式如何调用第三方和本地库