我是PInvoking GlobalMemoryStatus在NUnit测试中:
[TestFixture]
public class UnitTest1
{
[DllImport("kernel32.dll")]
private static extern void GlobalMemoryStatus([In, Out] MemoryStatus status);
[Test]
public void TestMethod1()
{
var mem = new MemoryStatus();
GlobalMemoryStatus(mem);
Assert.AreNotEqual(0, mem.dwAvailPageFile);
}
[StructLayout(LayoutKind.Sequential)]
internal class MemoryStatus
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
}在中运行时,此测试工作正常。
但是如果用nunit-console.exe执行,它就会失败。
nunit\nunit-console.exe bin\Debug\UnitTestProject1.dll /framework:net-4.5有了这个输出:
PS C:\UnitTestProject1> nunit\nunit-console.exe bin\Debug\UnitTestProject1.dll /framework:net-4.5
NUnit-Console version 2.6.4.14350
Copyright (C) 2002-2012 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.
Runtime Environment -
OS Version: Microsoft Windows NT 6.1.7601 Service Pack 1
CLR Version: 2.0.50727.5466 ( Net 3.5 )
ProcessModel: Default DomainUsage: Single
Execution Runtime: net-4.5
.F
Tests run: 1, Errors: 0, Failures: 1, Inconclusive: 0, Time: 0.1470761 seconds
Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0
Errors and Failures:
1) Test Failure : UnitTestProject1.UnitTest1.TestMethod1
Expected: not 0
But was: 0如果我使用MSTest,它可以很好地工作:
mstest /testcontainer:bin/debug/unittestproject1.dll有了这个输出:
C:\UnitTestProject1>mstest /testcontainer:bin/debug/unittestproject1.dll
Microsoft (R) Test Execution Command Line Tool Version 11.0.61030.0
Copyright (c) Microsoft Corporation. All rights reserved.
Loading bin/debug/unittestproject1.dll...
Starting execution...
Results Top Level Tests
------- ---------------
Passed UnitTestProject1.UnitTest1.TestMethod1
1/1 test(s) Passed
Summary
-------
Test Run Completed.
Passed 1
---------
Total 1
Results file: C:\UnitTestProject1\TestResults\ddimitrov_BPCANALYTICS01 2015-04-02 14_09_16.trx
Test Settings: Default Test Settings我正在Windows 2008 R2标准x64计算机上运行这个测试,它有6GB的内存。相同的nunit-console测试在Windows8.1上运行良好。
您是否知道我可能做错了什么,以便Win32函数报告不正确的数据?
发布于 2015-04-02 14:49:44
结构为已定义,如下所示:
typedef struct _MEMORYSTATUS {
DWORD dwLength;
DWORD dwMemoryLoad;
SIZE_T dwTotalPhys;
SIZE_T dwAvailPhys;
SIZE_T dwTotalPageFile;
SIZE_T dwAvailPageFile;
SIZE_T dwTotalVirtual;
SIZE_T dwAvailVirtual;
} MEMORYSTATUS;SIZE_T成员是指针大小的,这意味着您的声明在64位进程中是不正确的。对这些成员使用UIntPtr。
发布于 2015-04-02 12:37:31
以下是MSDN的评论
在内存超过4GB的计算机上,MEMORYSTATUS结构可以返回不正确的信息,报告值为-1以表示溢出。如果您的应用程序存在此行为的风险,请使用GlobalMemoryStatusEx函数而不是GlobalMemoryStatus函数。
https://stackoverflow.com/questions/29412790
复制相似问题