📄 siclient.cpp
字号:
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
// 如果SIServerLib.dll文件没有找到,你需要创建SIServer解决方案,然后执行/Bin
//目录的CreateTypeLib.bat脚本文件,生成该文件
#using "../bin/SIServerLib.dll"
using namespace System;
using namespace SIServerLib;
using namespace Reflection; //只用于后期绑定
//这个类用于说明如何进行早期绑定
__gc class CSIEB
{
public:
//构造函数
CSIEB()
{ //创建一个COM组件对象的实例,也就是说生成一个运行态可
//调用的包装类,这个类负责组件实例化
pSystemInfo = new CDGetSystemInfo;
}
// 返回当前主机的名字
String* PrintComputerName()
{
if (pSystemInfo)
{
// 从Com组件服务器获得主机名
return String::Concat(S"当前主机的名字为",pSystemInfo->GetMyComputerName());
}
else
return (String*) 0;
}
private:
CDGetSystemInfo *pSystemInfo;
};
// Class to demonstrate late binding.
__gc class CSILB
{
public:
// Constructor
CSILB()
{
// Get Type object for COM server. It uses ProgID of DClock class.
typ = Type::GetTypeFromProgID("SIServer.DGetSystemInfo");
// obtain instance of COM object.
obj = Activator::CreateInstance(typ);
// Create array of parameters.
args = new Object*[0];
}
// Returns string that has current time.
// If bAddDate is not 0, it returns date and time,
// if bAddDate is 0, returns time only.
String* PrintComputerName()
{
if (!typ || !obj)
return (String*) 0;
return String::Concat(S"当前主机的名字为",typ->InvokeMember("GetMyComputerName", BindingFlags::InvokeMethod, Type::DefaultBinder, obj,0));
}
private:
Type *typ;
Object *obj;
Object* args[];
};
// This is the entry point for this application
#ifdef _UNICODE
int wmain(void)
#else
int main(void)
#endif
{
String *result;
// Create and use managed C++ class that uses early binding.
try
{
CSIEB *objEB;
objEB = new CSIEB();
result = objEB->PrintComputerName();
if (result)
Console::WriteLine(result);
else
Console::WriteLine(S"出错信息: 无法得到主机名字");
// 创建使用一个后期绑定的托管C++类
CSILB *objLB;
objLB = new CSILB();
result = objLB->PrintComputerName();
if (result)
Console::WriteLine(result);
else
Console::WriteLine(S"出错信息: 无法得到主机名字");
}
catch(Exception* e)
{
Console::WriteLine("错误信息: {0}", e->Message);
}
Console::Write("\n\n按回车键继续");
Console::ReadLine();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -