如何将下面提到的C ++代码转换为C#代码? [英] How do I convert the below mentioned C++ code to C# code?

查看:87
本文介绍了如何将下面提到的C ++代码转换为C#代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在msdn中看到了这段代码:



http://msdn.microsoft.com/en-us/library/windows/desktop/ms682050(v = vs.85).aspx



这是用C ++编写的。我需要在我的C#应用​​程序中使用它。我可以将它转换为DLL。但是怎么能我在C#appliaction中使用它?此外,当我从控制台应用程序运行它时,它会打印内存地址,而不是'printf'应用程序中每个项目的大小。为什么会这样?

I saw this code in msdn:

"http://msdn.microsoft.com/en-us/library/windows/desktop/ms682050(v=vs.85).aspx"

This is in C++.I need to use this in my C# application.I could convert this into a DLL.But how can I use this in my C# appliaction? Also when I run this from a console application, instead of the size of each item in the 'printf' application, it prints the memory address. why is that?

推荐答案

为什么会这样?



因为你转换错误了。

可能,因为你不熟悉C ++,并且不习惯指针。



不要尝试将C ++转换为C#,除非你非常熟悉这两种语言:它们可能看起来非常相似,但它们是非常非常不同的动物,它们具有共同的语法。这并不意味着很容易从一个转换为另一个。



因此要么像InsertCleverName建议的那样将其编译成DLL,要么寻找一个C#解决方案你想要什么:我怀疑转换它超出你当前的技能组合。
"why is that?"

Because you converted it wrong.
Probably, because you don't know C++ that well, and aren't used to pointers.

Don't try an convert C++ to C# unless you are pretty much familiar with both languages: they may look very similar, but they are very, very different animals which share a common-looking syntax. That doesn't mean it's easy to convert from one to the other.

So either compile it into a DLL as InsertCleverName suggested, or look for a C# solution that does what you want: I suspect that converting it is beyond your current skill set.


using System;

// To ensure correct resolution of symbols, add Psapi.lib to TARGETLIBS
// and compile with -DPSAPI_VERSION=1

private void PrintMemoryInfo(uint processID)
{
	System.IntPtr hProcess;
	PROCESS_MEMORY_COUNTERS pmc = new PROCESS_MEMORY_COUNTERS();

	// Print the process identifier.

	Console.Write("\nProcess ID: {0:D}\n", processID);

	// Print information about the memory usage of the process.

	hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, processID);
	if (hProcess == null)
		return;

	if (GetProcessMemoryInfo(hProcess, pmc, sizeof(PROCESS_MEMORY_COUNTERS)))
	{
		Console.Write("\tPageFaultCount: 0x{0:X8}\n", pmc.PageFaultCount);
		Console.Write("\tPeakWorkingSetSize: 0x{0:X8}\n", pmc.PeakWorkingSetSize);
		Console.Write("\tWorkingSetSize: 0x{0:X8}\n", pmc.WorkingSetSize);
		Console.Write("\tQuotaPeakPagedPoolUsage: 0x{0:X8}\n", pmc.QuotaPeakPagedPoolUsage);
		Console.Write("\tQuotaPagedPoolUsage: 0x{0:X8}\n", pmc.QuotaPagedPoolUsage);
		Console.Write("\tQuotaPeakNonPagedPoolUsage: 0x{0:X8}\n", pmc.QuotaPeakNonPagedPoolUsage);
		Console.Write("\tQuotaNonPagedPoolUsage: 0x{0:X8}\n", pmc.QuotaNonPagedPoolUsage);
		Console.Write("\tPagefileUsage: 0x{0:X8}\n", pmc.PagefileUsage);
		Console.Write("\tPeakPagefileUsage: 0x{0:X8}\n", pmc.PeakPagefileUsage);
	}

	CloseHandle(hProcess);
}

static int Main()
{
	// Get the list of process identifiers.

	uint[] aProcesses = new uint[1024];
	uint cbNeeded;
	uint cProcesses;
	uint i;

	if (!EnumProcesses(aProcesses, sizeof(uint), cbNeeded))
	{
		return 1;
	}

	// Calculate how many process identifiers were returned.

	cProcesses = cbNeeded / sizeof(uint);

	// Print the memory usage for each process

	for (i = 0; i < cProcesses; i++)
	{
		PrintMemoryInfo(aProcesses[i]);
	}

	return 0;
}


您尚未说明您的具体目标。因此,我不确定我们如何帮助你。但是,您可能想看的是这里列出的代码:如何获得进程和线程的CPU使用率 [ ^ ]
You have not stated what your specific goal is. Therefore, I am not certain how we may help you. However, perhaps what you want to look at is the code listed here: How to get CPU usage of processes and threads[^]


这篇关于如何将下面提到的C ++代码转换为C#代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆