Delphi Prism 中的操作系统指令
由于我正在编写一个程序,最终将在从相同项目文件编译的 Windows 和 Linux 环境上运行,因此我想测试并查看操作系统指令的效果如何。所以,我写了一个示例代码。该代码似乎意外运行或者只是我的想象。
代码如下:
method MainForm.ControlBtn_Click(sender: System.Object; e: System.EventArgs);
begin
{$IFDEF linux}
MessageBox.Show('This is Linux. Horay!!!', 'mypro',MessageBoxButtons.yesno);
{$ENDIF}
{$IFDEF WIN32}
MessageBox.Show('This is Win32. Horay!!!', 'mypro',MessageBoxButtons.yesno);
{$ENDIF}
{$IFDEF CLR}
MessageBox.Show('This is .NET Framework. Horay!!!', 'mypro',MessageBoxButtons.yesno);
{$ENDIF}
end;
现在,当我在 Windows 上运行此方法时,它会弹出一个消息框,其中显示“这是 .NET Framework”。万岁!!!”我有点期待它在 Windows 上运行。当我在 Linux 下的 Mono 下运行它时,它弹出一个消息框,其中包含相同的消息:“这是 .NET FrameWork。Horay!!!”我期待看到 Linux 消息,即“This is Linux. Horay!!!”如果这段代码工作正常,那么当您确实需要执行仅 Linux、Mac 或 Windows 支持的不同方法时,如何检查您的程序正在哪个平台上运行。
Since I am writing a program that will eventually run on Windows and Linux environment compiled from the same project files, I wanted to test and see how well the Operating System directives are. So, I wrote a sample code. The code seems to run unexpectedly or it just my imagination.
Here is the code:
method MainForm.ControlBtn_Click(sender: System.Object; e: System.EventArgs);
begin
{$IFDEF linux}
MessageBox.Show('This is Linux. Horay!!!', 'mypro',MessageBoxButtons.yesno);
{$ENDIF}
{$IFDEF WIN32}
MessageBox.Show('This is Win32. Horay!!!', 'mypro',MessageBoxButtons.yesno);
{$ENDIF}
{$IFDEF CLR}
MessageBox.Show('This is .NET Framework. Horay!!!', 'mypro',MessageBoxButtons.yesno);
{$ENDIF}
end;
Now, when I run this method on Windows it pops up a message box with 'This is .NET Framework. Horay!!!' I kind of expected that being that it was running on Windows. When I ran it on Linux under Mono, it popped up a message box with the same message, "This is .NET FrameWork. Horay!!!" I was expecting to see Linux message, which is "This is Linux. Horay!!!" If this code is working correctly, then how do you check to see which platform your program is running on in the event you do need to execute different methods only supported by Linux or Mac or windows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编译器指令在编译时评估(因此称为编译器指令)。因此生成的 .exe 将始终声明其编译的平台,而不是其运行的平台。另外,对于 Prism / Oxygene 语言,Delphi-Compiler 指令不是以这种方式定义的。
检索您正在运行的操作系统的方法有点棘手(例如,有多个值表明您在 UNIX 上),但并不太复杂。
第一个地方是 System.Environment.OSVersion.Platform。
此枚举定义 .NET 2.0 中的以下值:
Win32S、Win32Windows、Win32NT、WinCE、Unix、Xbox、MacOSX
。 MacOS 有自己的价值,而 Linux 和其他 Unixoid 系统则共享 Unix 的价值。Mono 还定义了其他值(有关确定平台的信息,请参阅 Mono 常见问题解答条目)。
编辑:一种可能的方法是:
The compiler directives are evaluated at compile time (hence compiler directives). So the resulting .exe will always state the platform it was compiled on, not the one it is running on. Also, the Delphi-Compiler directives are not defined in this way for Prism / Oxygene language.
The way to retrieve the OS you're running on is a bit tricky (there are for example multiple values stating you're on UNIX), but not overly complicated.
The first place to go is
System.Environment.OSVersion.Platform
.This enum defines the following values in .NET 2.0:
Win32S, Win32Windows, Win32NT, WinCE, Unix, Xbox, MacOSX
. MacOS has its own value while Linux and other Unixoid systems share the Unix value.Mono also defines other values too (see the Mono FAQ entry on determining the platform).
Edit: One possible way would be: