|
Hello,
So i am sure that this is a commonly understood thing, but me and my friend have a bad feeling that there is a reason NOT to do the following.
Example:
a class is defined like this:
class Lock {
public:
Lock (Mutex& m) : mutex (m) {...use mutex...}
...
private:
Mutex& mutex;
}
then we were thinking if it was safe to create a lock like this:
...
Lock l (Mutex m);
...
correct me if I am wrong, but then m is scoped with l. This works if I only want m to exist for l.
It is similar to doing:
Mutex m;
Lock l (m);
but in this case I have a ref to m and i have to make sure they are scoped together.
Is there any taboo no-nos to doing the prior method? Some pitfalls I am not seeing?
|
|
|
|
|
thelonesquirrely wrote: then we were thinking if it was safe to create a lock like this:
...
Lock l (Mutex m);
...
correct me if I am wrong, but then m is scoped with l. This works if I only want m to exist for l.
No, that's incorrect. You're declaring a function, l, which takes a Mutex parameter called m and returning a lock. You cannot declare and define a variable in a function call. To demonstrate this, run this code:
#include <iostream>
class Mutex
{
public:
Mutex() { std::cout << "Mutex::Mutex\n"; }
~Mutex() { std::cout << "Mutex::~Mutex\n"; }
};
class Lock
{
public:
Lock(Mutex& m) : m_(m) { std::cout << "Lock::Lock\n"; }
~Lock() { std::cout << "Lock::~Lock\n"; }
private:
Mutex& m_;
};
int main()
{
Lock l(Mutex m);
}
You'll get no output, because no objects are being created or destroyed!
thelonesquirrely wrote: Is there any taboo no-nos to doing the prior method? Some pitfalls I am not seeing?
The main pitfall is that you never define a Lock variable, so you're not going to get any locking behaviour.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
How to join search two data tables in different database by ado?
Thanks!
|
|
|
|
|
By using database.owner.table notation.
"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
|
|
|
|
|
First,thank you,my friend!
However,how many _connection objects and _recordset objects should I use?
With great pleasure if you can show me some simple C++ code!
|
|
|
|
|
jiabin_007_love wrote: However,how many _connection objects and _recordset objects should I use?
One of each should suffice.
"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
|
|
|
|
|
DavidCrow wrote: One of each should suffice.
However,a _recordset object can only correspond to one _connection object, and one _connection object can only link one database.
I need operate two data table in different database in one sql operator.
|
|
|
|
|
I have a created a C# windows service (Serv.exe) which is responsible for performing various tasks at the request of a running application (A.exe), some of these can take long periods of time and I need a way to know the status of my requested operation (running on the service) from the calling application (A.exe).
Currently the way I have started to implement this is by using the ControlService(handle, command, status) to send a custom command to the Service to perform the task being requested, I am also using QueryServiceStatus(handle, status) to get the status of the service from the SCM - so the basic plumbing is there and working.
Now the problem I have is, after sending my ControlService Command (which works fine) the calling application (A.exe) keeps running and, at a certain point, it needs to know if the task it requested of the service is finished or not - therefore I am looking for a way to Query the Service to report a custom status, not the standard running, stopped, pending, paused state of the actual service but the status of the request I made using the ControlService() request.
Is this at all possible?
Any help or hints would be immensily appreciated.
Thanks,
|
|
|
|
|
Is it possible to intercept the WinMM PlaySound function?
I seem to recall from ages past how to hook Windows API calls, but I'm not sure that I remember that correctly.
Marc
Will work for food.
Interacx
I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner
|
|
|
|
|
Hi Marc,
Absolutely. I believe the PlaySound Function[^] is exported from winmm.dll and is a wrapper for PlaySoundW so you could just hook PlaySoundW and get both. The Microsoft Detours[^] works very well although I believe it requires a licence for commercial use. There are also many free libraries (some here on codeproject) but your product support with free libraries may be limited or non-existent.
Best Wishes,
-David Delaune
|
|
|
|
|
Greetings, I'm using MS SAPI for speech recognition in my project. I have the TTS and
the Standard Speech Recognition working perfectly, but I'm now trying to use the
dynamic Grammar interface or the "grammar builder".
The problem I think is with the ISpGrammarBuilder pointer that I am using in this code snipped:
CComPtr<ISpGrammarBuilder> cpGrammarBuilder;
SPSTATEHANDLE hStateTravel;
hr = cpGrammarBuilder->GetRule(L"Travel", 0, SPRAF_TopLevel | SPRAF_Active, TRUE, &hStateTravel);
I'm basing this code on this MS example code.
The code compiles correctly but I always get this Debug Assertion Failure on line 172 in atlcomcli.h or this function:
_NoAddRefReleaseOnCComPtr<T>* operator->() const throw()
{
ATLASSERT(p!=NULL);
return (_NoAddRefReleaseOnCComPtr<T>*)p;
}
Can anyone tell me what this function does? and if I'm using this ISpGrammarBuilder pointer correctly?
Any help is greatly appreciated
|
|
|
|
|
You never assign the pointer a value - that's why you get the assertion failure. The Microsoft example omits that rather important step
Largo65 wrote:
_NoAddRefReleaseOnCComPtr<T>* operator->() const throw()
{
ATLASSERT(p!=NULL);
return (_NoAddRefReleaseOnCComPtr<T>*)p;
}
Can anyone tell me what this function does? and if I'm using this ISpGrammarBuilder pointer correctly?
This function implements CComPtr 's operator-> , allowing you to use the underlying interface pointer's methods through the managed pointer. All it needs to do is return something which also has an operator-> , whether implicitly defined (i.e. a pointer) or explicitly defined (a struct/class).
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I was a little bit bored and tried this inline asm example:
Calling C Functions in Inline Assembly MSDN
But I get this error :
"Unhandled exception at ... Access violation writing location..."
at the instruction after the call instruction, even if I insert a NOP there.
Started with a vs2005 win32 console project and I copy pasted the code in "main" to my "_tmain" function
and declared the strings outside the main function
Another function that I created before did work (I think, didn't got any errors ):
int increase (int a)
{
return ++a;
}
....
push 1
call increase
pop ebx
|
|
|
|
|
This is happening because you are dynamically linking with the C runtime. Change the assembly code in the Microsoft example to:
call DWORD PTR printf
Another way to fix it is to change your C runtime linking to /MT or /MTd
Best Wishes,
-David Delaune
|
|
|
|
|
thankyouverymuch , works now.
now I just have to figure out why it works
call printf
call DWORD PTR printf
So does this means that the first call instruction wants to use the "__imp__printf" directly
as an address where the printf function is?
And the second call instruction uses __imp__printf as a pointer to this addres?
I suppose it is like this because the address of the printf function is unkown before runtime
because the library is dynamically linked?
|
|
|
|
|
Sounds like you pretty much understand why the Microsoft sample failed. Here is an old MSDN article with more info:
__declspec(dllimport) and __declspec(dllexport)[^]
invader82 wrote: now I just have to figure out why it works
Ahhh but isnt it great learning something new? Here are two more ways you could have fixed the Microsoft sample assembly code. I leave it as an excercise to the reader to figure out why it works.
[Method 1]
call dword ptr [esi+printf]
[Method 2]
mov esi,printf
call esi
Feeling like a programming dinosaur,
-David Delaune
|
|
|
|
|
thx for the article very usefull
Method 1 doesn't work and I don't see how it could work.
It uses the value of esi as a base address and adds the printf function address to it
I guess this would only work if esi == 0
method 2 is quite straight forward. it just puts the address of the function into esi
and performs an indirect call to that function using the addres in esi.
thanks anyway
|
|
|
|
|
|
|
Gajendra Sharma wrote: Is it true also for Unix Sockets
It is.
|
|
|
|
|
Gajendra Sharma wrote: Is it true also for Unix Sockets?
Sockets are not platform/OS dependent.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Gajendra Sharma wrote: Is it true also for Unix Sockets?
Yes.
The Microsoft implementation was originally designed/derived from Unix Berkeley sockets[^] and for the most part they are compatiable. There have been many Windows specific extensions in Microsoft Winsock2 but for standard TCP/IP and UDP they are nearly identical implementations.
Best Wishes,
-David Delaune
|
|
|
|
|
Thanks all for the valuable replies.
Best Regards.
Gajendra Sharma
Gajendra
|
|
|
|
|
I want to use an ocx in Vista session 0 using hidden window/dialog. This is giving me crash consistently. Has anyone tried this successfully?
|
|
|
|
|
Hello,
i have the following problem that i want to describe with codesnippets:
i have the class CGCxPageElement. Its the Base class of CGCxPane:
class CGCxPageElement : public CGCxHtmlTemplate {
DECLARE_DYNCREATE(CGCxPageElement)
public:
CGCxPageElement();
CGCxPageElement(const CGCxPageElement &e) {
m_strTemplate = e.m_strTemplate;
m_strContent = e.m_strContent;
}
CGCxPageElement& operator=(const CGCxPageElement &e) {
m_strTemplate = e.m_strTemplate;
m_strContent = e.m_strContent;
return *this;
}
virtual ~CGCxPageElement();
protected:
CString m_strContent;
public:
virtual void SetContent(CString strContent);
virtual void SetContent(UINT nIDResource);
virtual CString MakeHtml();
};
class CGCxPane : public CGCxPageElement {
DECLARE_DYNCREATE(CGCxPane)
public:
CGCxPane();
CGCxPane(const CGCxPane &e) {
m_strTemplate = e.m_strTemplate;
m_strContent = e.m_strContent;
m_strTitle = e.m_strTitle;
}
CGCxPane& operator=(const CGCxPane &e) {
m_strTemplate = e.m_strTemplate;
m_strContent = e.m_strContent;
m_strTitle = e.m_strTitle;
return *this;
}
virtual ~CGCxPane();
protected:
CString m_strTitle;
public:
void SetTitle(CString strTitle);
virtual CString MakeHtml();
};
then i have an array of CGCxPageElement objects. i have to add GCxPane and other Object inherited from CGCxPageElement to it:
CArray<CGCxPageElement, CGCxPageElement&> m_Elements;
now here comes the problem: inside another class i want to go through the CGCxPageElement array and call the method MakeHtml, but not that one of the base class but of the child e.g. CGCxPane
for (int i = 0; i < m_Elements.GetCount(); i++) {
CGCxPageElement elem = m_Elements[i];
strContent.Append(elem.MakeHtml());
}
1. how can i find out what class is stored in m_Elements originally?
2. how can i call the MakeHtml() method of this class and not of the base class?
i am not a pro in inheritance...but for now i am stuck. any casting like
CGCxPane pane = (CGCxPane) m_Elements[i];
does not work and i don't want to hadrcode the CGCxPane inside because there will be some more CGCxPageElement object not only CGCxPane.
any help would be much appreciated!
regards,
rudi
|
|
|
|