rpc程序编译错误,怎么解决?

kgisme171 2010-09-06 12:07:13
按照网上的例子写了一个helloworld的rpc的程序。
首先写一个my.idl文件加入到Console application的工程里面
[
uuid("4556509F-618A-46CF-AB3D-ED736ED66477"), // 唯一的UUID,用 GUIDGen 生成
version(1.0)
]

interface HelloWorld
{
// 我们定义的方法
void Hello([in,string]const char * psz);
void Shutdown(void);
}
编译以后,把my_s.c和my_h.h加入到服务器的工程里面,在包含了main函数的文件里面实现Hello,Shutdown,MIDL_user_free和MIDL_user_allocate。链接选项加上了rpcrt4.lib。代码如下:
extern "C"{
void Hello(handle_t IDL_handle,const unsigned char*psz){
printf("glm server:%s\n",psz);
}
void Shutdown(handle_t IDL_handle){
RpcMgmtStopServerListening(NULL);
RpcServerUnregisterIf(NULL,NULL,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);
}
}

加不加extern "C"都出现下面这样的错误:
1>my_c.obj : error LNK2001: unresolved external symbol _MIDL_user_free@4
1>my_c.obj : error LNK2001: unresolved external symbol _MIDL_user_allocate@4
1>my_c.obj : error LNK2019: unresolved external symbol _NdrClientCall2 referenced in function _Hello
1>e:\visual studio 2010\Projects\myproj\Debug\myclient.exe : fatal error LNK1120: 3 unresolved externals
1>C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\Microsoft.Cpp.Win32.Targets(268,5): error MSB6006: "link.exe" exited with code 1120.

我应该如何解决呢?

...全文
241 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
asker161 2010-09-07
  • 打赏
  • 举报
回复
1.首先,idl文件名要和声明的接口同名
2.把实现函数Hello,midl_user_allocate,midl_user_free写到单独的一个c文件里面实现
3.#pragma comment(lib,"rpcrt4")
#pragma comment(lib,"ole32")

这样编译就能通过了。
kgisme171 2010-09-07
  • 打赏
  • 举报
回复
有人回答一下吗
kgisme171 2010-09-06
  • 打赏
  • 举报
回复
我用pragma命令加入了缺少的选项,但是还是不行

#include"stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include"my_h.h"
#pragma comment(lib,"rpcrt4")
#pragma comment(lib,"ole32")
//extern "C"{
void Hello(handle_t IDL_handle,const unsigned char*psz){
printf("glm server:%s\n",psz);
}
void Shutdown(handle_t IDL_handle){
RpcMgmtStopServerListening(NULL);
RpcServerUnregisterIf(NULL,NULL,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);
}
//}
int main(int argc,char* argv[]){
RpcServerUseProtseqEp((unsigned char*)"ncacn_np",20,(unsigned char*)"\\pipe\\{4556509F-618A-46CF-AB3D-ED736ED66477}",NULL);
RpcServerRegisterIf(HelloWorld_v1_0_s_ifspec,NULL,NULL);
RpcServerListen(1,20,FALSE);
return 0;
}
kgisme171 2010-09-06
  • 打赏
  • 举报
回复
缺少什么Lib呢? 从错误信息,看不出任何的东西。

谢谢!!!!!
gw_net 2010-09-06
  • 打赏
  • 举报
回复
这是链接错误,
你把缺少的lib加到linker input中就好了

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧