一、下载Corba-omniorb开源代码
首先到http://omniorb.sourceforge.net/下载一个win32的OmniORB。文件名为“omniORB-4.1.1-x86_win32_vc6.zip”。然后解压缩至文件夹“ D:\omniORB-4.1.1”。
二、设置环境变量
1)、将“D:\omniORB-4.1.1\bin\x86_win32;”加入到系统环境变量path中;
2)、设置用户环境变量“OMNI_ROOT= D:\omniORB-4.1.1”。
三、编辑IDL文件
使用记事本写一个time.idl文件,idl文件定义了的Time接口,其中包含一个get_gmt()方法。文件内容如下:
interface Time
{
short get_gmt();
};
四、编译IDL文件
在cmd中对idl进行编译:进入time.idl文件所在目录,使用omniidl -bcxx time.idl对time.idl进行编译。其中-bcxx告诉编译器将idl映射为C++,如果编译没有错误,则会在同一个目录下生成两个文件:分别是 time.hh,和timeSK.cc。time.hh是所有接口类型定义所在的文件,服务器和客户端的实现都需要这两个文件,作为相互之间交互的接口。
五、编辑由IDL工具生成的文件
修改time.hh和timeSK.cc两个生成文件扩展名称,将两个文件的后缀名分别改成.h和.cpp。并把timeSK.cpp中的#include "time.hh"改成#include "time.h"。
六、按VC向导新建控制台项目testOrbServer
新建一个VC6 win32 Console的testOrbServer空项目。将time.h和timeSK.cpp拷贝到项目所在目录,并将文件插入项目中,新建一个testOrbServer.cpp的文件。
// testOrbServer.cpp : Defines the entry point for the console application.
//
#include <iostream.h>
#include "time.h"
class Time_impl:public virtual POA_Time
{
public :
virtual short get_gmt();
};
short Time_impl::get_gmt()
{
return 1;
}
int main(int argc, char* argv[])
{
try
{
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
CORBA::Object_var obj= orb->resolve_initial_references("RootPOA");
PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
PortableServer::POAManager_var mgr = poa->the_POAManager();
mgr->activate();
Time_impl time_servant;
Time_var tm = time_servant._this();
CORBA::String_var str = orb->object_to_string(tm);
cout << str << endl;
orb->run();
}
catch (const CORBA::Exception&)
{
cerr << "exception" << endl;
return 1;
}
return 0;
}
七、按VC向导新建控制台项目testOrbClient
新建一个VC6 win32 Console的testOrbClient空项目。将time.h和timeSK.cpp拷贝到项目所在目录,并将文件插入项目中,新建一个testOrbClient.cpp的文件。
// testOrbClient.cpp : Defines the entry point for the console application.
//
#include <iostream.h>
#include "time.h"
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
throw 0;
}
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
CORBA::Object_var obj = orb->string_to_object(argv[1]);
if (CORBA::is_nil(obj))
{
cerr << "Nil Time Reference" << endl;
throw 0;
}
Time_var tm = Time::_narrow(obj);
if (CORBA::is_nil(tm))
{
cerr << "Nil Time Reference" << endl;
throw 0;
}
cout << "Time is " << tm->get_gmt() << endl;
}
catch (const CORBA::Exception&)
{
cerr << "Exception" << endl;
return 1;
}
return 0;
}
八、VC向导工程设置
图8-1 Code Generation下选择“Run-time Library ”选择MultiThreaded DLL
图8-2 增加预定义宏
Preprocessor definitions:
1)、__WIN32__
2)、__x86__
3)、_WIN32_WINNT=0x0400
4)、__NT__,__OSVERSION__=4
图8-3 增加工程依赖的库文件
1、选择Link选项卡,Category选择Input,添加库模块:
1)、ws2_32.lib
2)、mswsock.lib
3)、advapi32.lib
4)、omniORB411_rt.lib
5)、omniDynamic411_rt.lib
6)、omnithread32_rt.lib
2、设置库模块的路径:$(OMNI_ROOT)/lib/x86_32;
3、testOrbClient工程设置与testOrbServer一致。
九、编译测试程序
编译testOrbServer工程,最后生成testOrbServer .exe文件;
编译testOrbClient工程,最后生成testOrbClient.exe文件;
十、测试程序
一、运行testOrbServer.exe
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\Windows\system32>cd D:\omniORB-4.1.1\bin
C:\Windows\system32>d:
D:\omniORB-4.1.1\bin >testOrbServer
IOR:010000000d00000049444c3a54696d653a312e3000000000010000000000000064000000010102000e0000003139322e3130302e36342e353300a0e00e000000fe488c174f000006d0000000000000000200000000000000080000000100000000545441010000001c00000001000000010001000100000001000105090101000100000009010100
二、运行testOrbClient.exe
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\Windows\system32>cd D:\omniORB-4.1.1\bin
C:\Windows\system32>d:
D:\omniORB-4.1.1\bin>testOrbClient IOR:010000000d00000049444c3a54696d653a312e3
000000000010000000000000064000000010102000e0000003139322e3130302e36342e353300a0e00e000000fe488c174f000006d0000000000000000200000000000000080000000100000000545441010000001c00000001000000010001000100000001000105090101000100000009010100
Time is 1
D:\omniORB-4.11\bin>
运行testOrbServer.exe时获得IOR数据,将IOR作为运行testOrbClient.exe的参数。注意参数需要“IOR:××××××××”。调用CORBA对象的get_gmt之后,CORBA的伺服程序servant返回了数值1。