Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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?
Posted
Comments
[no name] 31-Mar-14 11:36am    
Compile it to a DLL then use P/Invoke to call it.

"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.
 
Share this answer
 
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;
}
 
Share this answer
 
Comments
mayooran99 31-Mar-14 23:09pm    
@TL Wallace Can I run this code alone without having to convert C++ version of this into a DLL? Because when I try to run this code it is giving me errors. Please advice.
Terence Wallace 1-Apr-14 10:20am    
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: http://www.codeproject.com/Articles/10258/How-to-get-CPU-usage-of-processes-and-threads
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[^]
 
Share this answer
 
Comments
mayooran99 1-Apr-14 11:05am    
@TL Wallace I need to obtain in depth information of the processes. But there are all in native format. So I am having trouble in converting them to C#!!! I found exactly what I need: its this:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686701(v=vs.85).aspx

I created a wrapper class, an interface and then implemented the wrapper class in C#.But since I am not familiar with C++ that much..I am unable to get the values that are printed inside the C++ method into a windows form. If you could help me with that I could manage the rest....
Terence Wallace 2-Apr-14 9:40am    
Unfortunately, I have not done much work in C++. Therefore, I don't have an answer as to how to get information out of a C++ based class. Maybe you can post separate question in the C++ Category about that. Happy coding. I hope that one of these solutions has helped you in some way.
Or, perhaps your trying to get the Process Owner ID? Take a look here: How To Get Process Owner ID and Current User SID
 
Share this answer
 
Comments
CHill60 1-Apr-14 19:31pm    
Try using the reply link on comments rather than using Solutions to have a conversation ... you'll start attracting downvoters otherwise
Terence Wallace 1-Apr-14 20:02pm    
Given the brevity of the intial question. Please explain to me why CP community would not see Solution 2,3 & 4 as three possible separate and distinct solutions.
CHill60 2-Apr-14 8:31am    
Because of the brevity, you were fishing for better information from the OP, but also essentially guessing what that problem might be. Many of the CP community would think that perhaps posting a comment would have been a more appropriate way of eliciting further information. Posting multiple solutions is often viewed as "reputation point hunting" - a practice carried out all to often unfortunately, jading the opinions of many who frequent the Quick Answers forum.
Terence Wallace 2-Apr-14 9:34am    
Well that is eye opening. Thank you. My original intent was to try and help shed some insight into the general nature of the question that might help the OP specifically. In a general sense, my intent was to perhaps help the overall community that might have a similar situation. Reputation points, secondarily, I thought would be based upon the merits of having successfully done that. Thanks again.
CHill60 2-Apr-14 10:14am    
It's a pleasure. There are some who seem to take pleasure in downvoting for the sake of it. Glad to see you aren't a "victim" :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900