|
In C++ i am creating a mutex to guard the writing to a file.
I have 4 threads that call a method in a .dll. The method in that dll, will write to a file when needed. I have a questions surrounding the mutex. First off :
1. Do i use CreateMutex or OpenMutex
2. If i use CreateMutex, can someone guide me on where to find the
attribute list for the 1st parameter.
3. On both Create and Open Murtex, the last parameter 'LPCSTR' is supposed
to be a name of the mutex. Can i just create any name? Should it
be a GUID?
Thanks in advance
|
|
|
|
|
LCI wrote: I have 4 threads that call a method in a .dll
If you want only intraprocess synchronisation, IMHO then go for CriticalSection, and don't bother about the Mutex questions.
|
|
|
|
|
I used this same concept for writing to another file and had no issues.
Here is what i do:
HANDLE hmutex = OpenMutex(MUTEX_ALL_ACCESS. FALSE, TEXT("2354fg-45gefgg"));
if (NULL != hMUTEX)
{
WaitforSingleObject(hMutex, INFINITE);
Begin writing to file
}
ReleaseMutex(hMutex);
CloseHandle(hMutex);
Looks like the mutex returns null so i never get to write to my file.
Maybe there is something wrong with the name(third parameter)??
|
|
|
|
|
OpenMutex get the mutex Handle for an existing mutex. You must have a Mutex Created with CreateMutex. what about Critical section?
|
|
|
|
|
|
LCI wrote: 1. Do i use CreateMutex or OpenMutex
In the thread initializations, use CreateMutex(). If the mutex already exists then you'll get a
handle to it and GetLastError() returns ERROR_ALREADY_EXISTS. If it's a new mutex you'll still get
a handle to it.
As others have mentioned, you don't need a named mutex if your threads are all in the same process -
which includes your DLL.
LCI wrote: 2. If i use CreateMutex, can someone guide me on where to find the
attribute list for the 1st parameter.
Windows security is beyond the scope of this board, but there is documentation[^]
Pass NULL if in doubt.
LCI wrote: . On both Create and Open Murtex, the last parameter 'LPCSTR' is supposed
to be a name of the mutex. Can i just create any name? Should it
be a GUID?
You can use a GUID (converted to a character string). Any unique string.
But again, no need for a named mutex if you only have one process - you can use a critical section instead.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi all,
I have a TCHAR string i want to retrieve first element of this string.
Which API i should use for getting this....
Thanks in advance
|
|
|
|
|
TCHAR firstChar = yourTCharString[0];
|
|
|
|
|
neha.agarwal27 wrote: i want to retrieve first element of this string.
what do you mean by element, may be the first token seperated by delimiters.
Specify exactly what you need.
|
|
|
|
|
pls help me to Get the caption of active window...
::GetActiveWindow()->GetWindowText(strCap); //ERROR
error C2039: 'GetWindowTextA' : is not a member of 'HWND__'
c:\program files\microsoft visual studio\vc98\include\windef.h(195) : see declaration of 'HWND__'
|
|
|
|
|
I don't know what exactly are you trying to do. But for the code you've posted I see the following:
GetActiveWindow() return a HWND handle, there are not members functions to access from it. So, after saving your handle like this:
HWND hwnd = GetActiveWindow();
Then you could call GetWindowText like this:
GetWindowText(hwnd, strCap, sizeof(strCap))
Demian.
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my telephone."
-Bjarne Stroustrup, computer science professor, designer of C++
programming language (1950- )
|
|
|
|
|
Or he can combine them into one:
GetWindowText(GetActiveWindow(), strCap, sizeof(strcap)); That assumes that strcap is a definitive size. The last parameter may need to change if it happens to be a CString or strring or pointer to some buffer.
Good luck.
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
|
|
|
|
|
Oh yeah, of course.
Demian.
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my telephone."
-Bjarne Stroustrup, computer science professor, designer of C++
programming language (1950- )
|
|
|
|
|
ptr_Electron wrote: ::GetActiveWindow()->GetWindowText(strCap); //ERROR
use CWnd::GetActiveWindow()
|
|
|
|
|
Rajkumar R wrote: use CWnd::GetActiveWindow()
Only if he is using MFC.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
see this, ::GetActiveWindow()->GetWindowText(strCap); //ERROR
It looks like calling a member function using a class instance, possibly CWnd * (from GetActiveWindow), and :: makes the Global scope rather CWnd.
Rajesh R Subramanian wrote: Only if he is using MFC
I implicitly telling him to move to MFC.
|
|
|
|
|
Rajkumar R wrote: I implicitly telling him to move to MFC.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
Rajkumar R wrote: I implicitly telling him to move to MFC.
Why this bad suggestion?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
to use CWnd::GetActiveWindow()
|
|
|
|
|
yeap am using MFC, thank you all Friends...
CWnd::GetActiveWindow()->GetWindowText(strCap); did the job
|
|
|
|
|
Probably he is (there's namespace scope specification).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
namespace scope can be used in non-MFC C++.
|
|
|
|
|
It is redundant: if you don't need it, don't use it.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
int CMyNonMFCFrameWorkNameSpace::GetWindowText(HWND hWnd,
LPTSTR lpString,
int nMaxCount
)
{
CMyNonMFCFrameWorkLog log("GetWindowText");
return ::GetWindowText(hWnd, lpString, nMaxCount);
}
There are useful example with other API's, though the above is not a so useful example, it is possible to have such situation that is not redundant. otherwise namespace concept is not needed. I can give example exactly that match OPs statement without MFC classes and other namespace.
|
|
|
|
|
Rajkumar R wrote: the above is not a so useful example
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|