搭建软件工程时,若涉及C++和C#混合编程,或者说想在C++搭建的工程里调用C#编译的动态库,则需要对C++工程进行简单配置,并且操作符也要做出一些特定变化。这里以VS2019平台下搭建的C++工程为例,调用C#编译的卫星轨道计算动态库,最终通过卫星两行根数成功计算出一段时间的卫星位置信息。
1.新建C++控制台程序
2.找到并复制C#编译的动态库文件,最好同时也复制pdb文件
3.粘贴到生成的exe文件所在目录
4.包含动态库,使用命名空间
#using "x64\Debug\Zeptomoby.OrbitTools.Orbit.dll"
#using "x64\Debug\Zeptomoby.OrbitTools.Core.dll"
using namespace std;
using namespace System;
using namespace Zeptomoby::OrbitTools;
5.公共语言配置
6.语言配置
7.编写C++调用C#代码
String^ str1 = "CALIPSO";
String^ str2 = "1 29108U 06016B 22214.84110821 .00000284 00000+0 65015-4 0 9993";
String^ str3 = "2 29108 098.3279 174.9743 0001318 081.8853 278.2498 14.63004442 806";
Tle ^tle1 = gcnew Tle(str1, str2, str3);
Satellite ^sat1 = gcnew Satellite(tle1);
DateTimeKind kind = DateTimeKind::Utc;//Utc;
DateTime ^utc = gcnew DateTime(2022, 8, 30, 4, 0, 0, kind);
for (int i = 0; i < 24 * 60; i++)
{
DateTime ^t_utc = utc->AddMinutes(1 * i);
Eci ^eci1 = sat1->PositionEci(*t_utc);
double x1, y1, z1, vx1, vy1, vz1;
x1 = eci1->Position->X;
y1 = eci1->Position->Y;
z1 = eci1->Position->Z;
vx1 = eci1->Velocity->X;
vy1 = eci1->Velocity->Y;
vz1 = eci1->Velocity->Z;
cout << i << " " << x1 << " " << y1 << " " << z1 << endl;
}
8.注意符号的改变
重点关注符号^ -> * gcnew 下面是C#里面的原始代码,对比变化:
string str1 = "CALIPSO";
string str2 = "1 29108U 06016B 22214.84110821 .00000284 00000+0 65015-4 0 9993";
string str3 = "2 29108 098.3279 174.9743 0001318 081.8853 278.2498 14.63004442 806";
Tle tle1 = new Tle(str1, str2, str3);
Satellite sat1 = new Satellite(tle1);
DateTimeKind kind = DateTimeKind.Utc;
DateTime utc = new DateTime(2022, 8, 30, 4, 0, 0, kind);
for (int i = 0; i < 24*60; i++)
{
DateTime t_utc = utc.AddMinutes(1*i);
Eci eci1 = sat1.PositionEci(t_utc);
double x1, y1, z1, vx1, vy1, vz1;
x1 = eci1.Position.X;
y1 = eci1.Position.Y;
z1 = eci1.Position.Z;
vx1 = eci1.Velocity.X;
vy1 = eci1.Velocity.Y;
vz1 = eci1.Velocity.Z;
Console.WriteLine(i + " " + x1 + " " + y1 + " " + z1);
Console.WriteLine();
}
9.编译运行,大功告成