|
Return value is 0 for the GetProcessImageFileName function. Even though it returns garbage data.
|
|
|
|
|
Are you using a UNICODE build?
If so declare the array as TCHAR instead of char .
Or you can try the GetModuleBaseName function.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Not UNICODE .
I'm going to give GetModuleBaseName a try, but according to msdn:
MSDN:
To retrieve the base name of the main executable module for a remote process, use the GetProcessImageFileName or QueryFullProcessImageName function to retrieve the module name and then use the strrchr function as described in the previous paragraph. This is more efficient and more reliable than calling GetModuleBaseName with a NULL module handle.
EDIT/update: GetModuleBaseName also returns garbage...
GetModuleBaseName(ProcessHandle,NULL,processName,1024);
MessageBox(NULL,processName,"test",MB_ICONINFORMATION);
modified on Friday, March 20, 2009 12:02 PM
|
|
|
|
|
You say that the error code is success and still you're getting garbage.
This is highly unlikely.
I believe you're not doing any error checking.
So its probably gone wrong somewhere else.
GetModuleBaseName for instance returns the length of the string copied.
What is this value?
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Oh, I'm sorry. I thought GetModuleBaseName returns an error code instead of the length of the string copied. I assumed the 0 it returned meant success
That explains the garbage data.
EDIT: I checked GetLastError , it's returning an invalid handle error.
Might this be because I'm getting the handle from NtTerminateProcess?
modified on Saturday, March 21, 2009 2:37 PM
|
|
|
|
|
hxhl95 wrote: I have a process handle and I wish to detect if that handle is one to my app.
Call GetProcessId() , and then call EnumWindows() to find the matching process id.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
error C2065: 'GetProcessId' : undeclared identifier , even though I've included windows.h. Does the fact that I'm using VC++ 6.0 make a difference?
|
|
|
|
|
hxhl95 wrote: Does the fact that I'm using VC++ 6.0 make a difference?
Only if you need to define _WIN32_WINNT . Look in winbase.h for the appropriate value (if any).
See here for more.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
winbase.h doesn't contain a definition of GetProcessId . I tried defining it manually with DWORD WINAPI GetProcessId(__in HANDLE ProcessHandle) , but then I get an unresolved external symbol error, even though I'm obviously including kernel32.lib...
|
|
|
|
|
hxhl95 wrote: winbase.h doesn't contain a definition of GetProcessId.
You need to install the Platform SDK.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I have.
|
|
|
|
|
So are you looking in C:\Program Files\Microsoft SDK\include\winbase.h instead of C:\Program Files\Microsoft Visual Studio\VC98\Include\winbase.h? If you add #include <winbase.h></winbase.h> to the top of a file, right-click it and select Open, which of the two files opens?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I did
#include <C:\Program Files\Microsoft Platform SDK\Include\winbase.h><br /> just in case, and it's still giving me an undeclared error. When I open the file the SDK file opens, and I can see the definition of GetProcessId in the file. However the compiler just doesn't find it.
|
|
|
|
|
I'm out of suggestions at this point. This compiles fine for me:
#include <windows.h>
void main( void )
{
GetProcessId(0);
}
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Okay. I'll just try a different approach if I can't get this working by tomorrow.
Thanks a lot for your help
|
|
|
|
|
I have been surfing the web trying to find a solution and I've tried some of them and still can't compile. It is a standard VS generated DlgClass and I just want to launch the worker which is prototyped up top as global. I've tried casting ExecuteThread every way possible pretty much in the thread launch function. Any obvious solutions?
void ExecuteThread(LPVOID lParam)
void CVisualEmulatorDlg::OnBnClickedRun()
{
HWND hWnd = GetSafeHwnd();
CWinThread *pThread = AfxBeginThread(ExecuteThread, hWnd, THREAD_PRIORITY_NORMAL);
}
void ExecuteThread(LPVOID lParam)
{
MessageBox( (HWND)lParam, (LPCWSTR)"Thread Start", (LPCWSTR)"Secondary Thread", MB_OK );
return;
}
|
|
|
|
|
You haven't been reading your documentation!!!
Try this.
UINT __cdecl ExecuteThread( LPVOID pParam );
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
The reason I ended up trying void, along with any other things is that UNIT dosn't show up as a data type. Is there a header that needs to be included? Having trouble researching it because everything that comes back is about unit testing.
From one page it looked like UNIT meant use, int, long, or short, but tried and non of those worked. Tried with and without the __cdecl as well.
Never come across the UNIT before and wishing right now I never had. Code looks basically the same. Any ideas?
void CVisualEmulatorDlg::OnBnClickedRun()
{
HWND hWnd = GetSafeHwnd();
CWinThread *pThread = AfxBeginThread(ExecuteThread, hWnd, THREAD_PRIORITY_NORMAL);
}
UNIT __cdecl ExecuteThread(LPVOID lParam)
{
MessageBox( (HWND)lParam, (LPCWSTR)"Thread Start", (LPCWSTR)"Secondary Thread", MB_OK );
}
|
|
|
|
|
typo typo typo typo
Its UINT not UNIT.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Wow.. sorry guys. Pretty embarased right now...but that fixed it.
Thanks a lot.
|
|
|
|
|
Killiconn wrote:
void ExecuteThread(LPVOID lParam)
Are you missing the ';' behind the function declaration.
By the way what are the compilation errors that you get?
The possible solutions for C2665 are
* Supply a conversion operator.
* Use explicit conversion.
but can't really say if they help you in resolving your problem
You need to google first, if you have "It's urgent please" mentioned in your question.
_AnShUmAn_
|
|
|
|
|
Your worker thread function definition is wrong, check out here[^]
The function should not return void, the function signature should be as follows:
UINT __cdecl MyControllingFunction( LPVOID pParam );
|
|
|
|
|
I am currently writing a multi-threaded application using C++ and MFC. I am trying to write some code to keep track of my memory allocations (new) and my memory frees (delete). I have written aclass called MemoryChecker that is suppose to do this. To store the list of pointers that are allocated,
I am using STL which is not thread safe. To make it thread safe, I put a call to WaitForSingleObject
around the body of the function. I would expect to make this thread safe. Inside the member function
deleteItem, there is a static variable. It is initialized to true but later set to false. Before it is set to false, there is an assert statement that checks that the value is still true. However,
that assert statement is failing (sometimes). Therefore, I am thinking my lock is not working properly.
<br />
void<br />
MemoryChecker::deleteItem( void *ptr )<br />
{<br />
if ( WaitForSingleObject(mutexDeleteHandle, INFINITE) == WAIT_OBJECT_0 ) {<br />
if ( ptr == 0 ) {<br />
ReleaseMutex(mutexDeleteHandle);<br />
returneturn;<br />
}<br />
static bool go = true;<br />
bool found = false;<br />
NewList::iterator it = newList.begin();<br />
while ( it != newList.end() ) {<br />
if ( it->ptrValue == ptr ) {<br />
found = true;<br />
break;<br />
}<br />
it ++;<br />
}<br />
if ( found == false )<br />
found = true;<br />
else {<br />
assert( found );<br />
newList.erase( it );<br />
}<br />
assert( go );<br />
go = false;<br />
ReleaseMutex(mutexDeleteHandle);<br />
}<br />
}
I should also tell you that I create the lock, in another thread, with the following call:
mutexDeleteHandle = CreateMutex( NULL, FALSE, NULL );
Please note that the second parameter in CreateMutex is to specify who owns the lock and the
lock may not be owned by the thread making the call to WaitForSingleObject.
I am hoping that somebody here can tell me what I am doing wrong.
Thanks
Bob
modified on Thursday, March 19, 2009 6:26 PM
|
|
|
|
|
Your 'go' variable is static, it will be set to true once, so after you do 'go = false' it will remain false in following calls too.
p.s: Please put all of your code between <pre> and </pre> tags to make it easier to read.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
Thanks for the response. You are 100% right. I am not awake today. I need to reinitialize the go flag every time to make it a valid test.
Now for your second point. I put the code between <pre> and </pre> but it did not preserve the white spacing. I do not know why. Any ideas?
Bob
|
|
|
|