little miss confused

Hi All,

I'm trying to write a piece of code to search for all system processes by name

To do this, I'm using :

array<Process^>^matches = Process::GetProcessesByName("notepad");

The problem is, I don't want to use a hard-coded value to search for, I want my function to accept the name of the process as a parameter.

However :

string procName = "notepad";

array<Process^>^matches = Process::GetProcessesByName(procName);

does not work and I get the error:

error C2665: 'System:Big Smileiagnostics:Stick out tonguerocess::GetProcessesByName' : none of the 2 overloads could convert all the argument types

Does anyone know what type I should be passing into GetProcessesByName

I've been trying to find some examples on the web, but everything either returns example with the processName hard-coded or examples that are not in C++.

Any help would be greatly appreciated as this has been driving me round in circles....

Thanks!



Re: Visual C++ General Problems using GetProcessesByName in C++

PenguinAdam

Change

Code Block
string procName

to

Code Block

System::String procName





Re: Visual C++ General Problems using GetProcessesByName in C++

Simple Samples

Try procName.c_str() instead of procName, or use a CLR string instead of a std string. I forget what a CLR string is but I think it is spelled String with an upper-case "S".






Re: Visual C++ General Problems using GetProcessesByName in C++

little miss confused

Hi guys,

Thanks for the quick responses.

I've now got:

System:Tongue Tiedtring ^procName = "notepad"; //thanks to penguinadam

array<Process^>^matches = Process::GetProcessesByName(procName);

but my function needs to use type "const char*" as the name of the process.

I guess my next problem is how to convert the process name to type String...

Thanks all





Re: Visual C++ General Problems using GetProcessesByName in C++

Simple Samples

I think you just need to do it the way that PenguinAdam shows. Your code is not the same and that is the problem. Trust PenguinAdam and do it the way shown.






Re: Visual C++ General Problems using GetProcessesByName in C++

Andreas Masur

little miss confused wrote:

but my function needs to use type "const char*" as the name of the process.

I guess my next problem is how to convert the process name to type String...


Although I do not see why you would need to do that based on your code...from top off my head you could use the 'PtrToStrinChars()' function which is defined in 'vcclr.h':


#include <vcclr.h>

PtrToStringChars(procName);






Re: Visual C++ General Problems using GetProcessesByName in C++

Simple Samples

I don't understand what you mean by:

my function needs to use type "const char*"

If you still need help and you have not solved the "const char*" problem then clarify what you need.

The following code works for me.

Code Block

System :: String ^procName = "devenv";

array<System :: Diagnostics :: Process^>^matches = System :: Diagnostics :: Process :: GetProcessesByName(procName);

System :: Console :: WriteLine(matches[0]->Id);






Re: Visual C++ General Problems using GetProcessesByName in C++

Simple Samples

I am sorry, I think I really misunderstood. One reason I misunderstood is that the caret ("^") looks like an asterisk ("*"). Probably Andreas's solution is the easiest and I assume you have already solved the problem. Also see How to: Convert Between Various String Types, but the relevant conversion it shows is more code than should be required for this simple task.

Also note that this is a language question, so it would be more appropriate in the VC language forum.




Re: Visual C++ General Problems using GetProcessesByName in C++

Simple Samples

Is the following something like what you need

Code Block

void ShowProcess(const char *Name) {

System::String ^procName = gcnew System::String(Name);

array<System::Diagnostics::Process^>^matches = System::Diagnostics::Process::GetProcessesByName(procName);

System::Console::WriteLine(matches[0]->Id);

}






Re: Visual C++ General Problems using GetProcessesByName in C++

little miss confused

Thanks Simple Samples! That is exactly what I needed!

Sorry for not replying sooner - I didn't expect my string conversion problem to be solved on this thread.

I'd been searching for a way of doing this conversion, but could find anything this simple.

Thanks so much for taking the time to look into this for me, it has really made a big difference!





Re: Visual C++ General Problems using GetProcessesByName in C++

Simple Samples

Certainly it feels good when someone is so appreciative. Note that the answer is in the article I reffered to. I felt bad about my initial respnse so I wanted to get a good answer.

Also note that you can get email notification when there are replies to threads you have created or replied to. The notification is called an alert.