|
|
There is an example on the page that was given to you.
By the way, "not works" is not really a clear description of the problem, it could be anything...
|
|
|
|
|
Hi,
IIRC the string scanning function sscanf can do that for you like so:
int result;
sscanf("3D", "%2x", &result);
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
const char s[] = "3D";
int i = (s[0]<='9' ? s[0]-'0': s[0]-'A'+10) << 4 | (s[1]<='9' ? s[1]-'0': s[1]-'A'+10);
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
[My articles]
|
|
|
|
|
now that will choke on lower-case hex numbers, won't it?
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hi,
I am Developing an application in which i have windows mobile connected to pc.
Handset is connected to PC via USB .
In my Project i m invoking mobile by using PC.
we are using CeCreateProcess() function (RAPI API) for invoking mobile Application.
I deployed mobile agent exe in mobile.
when mobile is connected successfully,than i run my appliaction and connect with mobile device its forking fine,but if now device is disconnected and i again connectd the device to PC,its get the device connection but
when i invoking mobile by CeCreateProcess() than Error is returned by WSARecv and WSARecvFrom to indicate that the remote party has initiated a graceful shutdown sequence.
How can I solve this issue??
please help me for this
thanks in advance.
To accomplish great things, we must not only act, but also dream;
not only plan, but also believe.
|
|
|
|
|
I get the error message:
PlanGAUL.obj : error LNK2001: unresolved external symbol "short __cdecl ProcessArgs(int,char * *)" (?ProcessArgs@@YAFHPAPAD@Z)
This function comes to my program from a library of mine. The function is declared as follows:
extern bool __cdecl ProcessArgs(int argc, char **argv); in the header file and the same way in source file from which the library is build.
As I tried to find the cause of this error I had encountered the difference between two decorated names of the same function.
In the library there is:
?ProcessArgs@@YA_NHPAPAD@Z
and in "expected" decorated name in my program there is:
?ProcessArgs@@YAFHPAPAD@Z
The difference is in signature which denotes the return type that precedes the letter "H" (the first parameter type, here int ). In the library, that is "_N" and in my program - "F", which means short (originally bool ). What makes the difference between decorating and what does the "_N" mean, used instead of "F"? I have not found the description of "_N" signature. Compare e.g. http://www.kegel.com/mangle.html
Regards
|
|
|
|
|
This article will probably help you. - Microsoft Visual C++ Name Mangling[^]
To avoid name mangling you should use extern "C" in your function signature.
extern "C" bool __cdecl ProcessArgs(int argc, char **argv);
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
The function ProcessArgs is compiled as C++ code and used in C++ code so I'm not sure if extern "C" is appropriate declaration.
|
|
|
|
|
liquid_ wrote: The function ProcessArgs is compiled as C++ code and used in C++ code
Using the same compiler (you know mangling details are compiler-specific)?
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
[My articles]
|
|
|
|
|
That implies to me that there's maybe a #define turning your bool into a short? Or possibly some strange compiler setting?
I've just attempted to build this code with VS2008 and it complains about a missing function called ?ProcessArgs@@YA_NHPAPAD@Z , i.e. same as in your library:
extern bool __cdecl ProcessArgs(int argc, char **argv);
int main(int argc, char **argv)
{
ProcessArgs(argc, argv);
}
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Stuart Dootson wrote: That implies to me that there's maybe a #define turning your bool into a short?
Indeed, there was redefinition bool to short . I've forgotten that.
Solutions are simpler than they seem to us. Thanks a lot.
|
|
|
|
|
Hallo,
I have an application that uses a secondary thread to excute a very long function, what i need is, how to terminate the secondary thread "CLEANLY" if the main appliction is going to be closed while the seconndary thread is still excuting the verx long function? i started with : ON_WM_Close() in the View.cpp file, but i do not know how to continue: The secondary thread should become a Message if the the x or End in the Main application werde clicked. Please help(with code).
|
|
|
|
|
The secondary thread can execute its code within a spin loop, which can wait for an event to "happen" (with a wait time of 0 ms). While you want to close your application, just set that particular event so that the secondary thread would have a chance to clean itself up properly. You could similarly use another event (with a timeout) that the secondary thread would "set" to inform the main thread that it has finished cleaning up and is on its way to terminate.
Take a look at WaitForSingleObject[^].
Attempt it, and if you get stuck somewhere, or if you don't understand any of it, post it here (with relevant source code, if any).
MTRecalc[^] sample makes use of events, which you can refer to.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
bool PrintMy Results(const CString& strMyResult)
{
bool bTerminate = false;
while(!bTerminate && nCount < arrMyResults.GetSize())
{
if (::WaitForSingleObject(m_End_Event.m_hObject,
0) == WAIT_OBJECT_0)
{
bTerminate = true;
continue;
}
nCount++;
// Code to print the Array-Elements
// I am hier inside the secondary thread wich is in another // class as the Doc class, how can I till him hier that the //application is going to be finsihed and he must finsh the printing //of the last line of the Array?
}
}
|
|
|
|
|
WARNING: I don't have VS on this machine, I've typed the code out manually and I've no way to compile it. I've verified whatever I can, but please make sure that it works fine before you use it. Use it at your own risk, keep out of reach of children, do not expose to sunlight, etc.,
Global handle variables to store the event:
HANDLE hDieNow, hImDead;
Function where you'd create the events (preferably InitDialog or something like that):
CMyDialog::OnInitDialog()
{
hDieNow = CreateEvent(NULL, TRUE, FALSE, NULL);
hImDead = CreateEvent(NULL, TRUE, FALSE, NULL);
return TRUE;
}
Thread function:
UINT ThreadFunc(LPVOID pFuncParams)
{
while(true)
{
if(WaitForSingleObject(hDieNow, 0) == WAIT_OBJECT_0)
break;
else
{
Sleep(100);
OutputDebugString(_T("Thread probe...\n"));
}
}
SetEvent(hImDead);
return false;
}
Application close handler function:
CMyDialog::OnClose()
{
SetEvent(hDieNow);
DWORD dwResult = WaitForSingleObject(hImDead, 5000);
}
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
thanks. I am using SDI application, so i will put the code in BOOL OnNewDocument() instead of OnInitDialg(). Now a general question: why can i not use ON_WM_CLOSE() to tell the small thread to terminate himself and if yes how? Thanks a lot
|
|
|
|
|
susanne1 wrote: I am using SDI application, so i will put the code in BOOL OnNewDocument() instead of OnInitDialg().
Do it wherever. You just need to create those events, before you would use it. But be sure on that, or the WaitFor... call is going to fail and you'll not get desired results.
susanne1 wrote: Now a general question: why can i not use ON_WM_CLOSE() to tell the small thread to terminate himself and if yes how?
What do you mean how? I just demonstrated that to you with an example in my previous post. Just set the event that the worker thread is spinning on (waiting to be set) and then wait for the worker thread to set the "I'm dead" event (which is kind of an acknowledgment from the thread that the cleanup is complete).
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
As you've shown this code, here's a potential solution:
- Ensure there is a
volatile LONG variable (let's call it lTerminate) visible to both your main thread's code and your second thread's code. Initialise that variable to 0 before the second thread is started - When you start the second thread, store its handle somewhere it can be seen by the code that will terminate the thread.
- The second thread's loop should look like this:
bool PrintMy Results(const CString& strMyResult)
{
while(!lTerminate && nCount < arrMyResults.GetSize())
{
nCount++;
}
} - When you want to exit, set lTerminate to 1 with an appropriate function (as I'll show you below) and wait for the thread's handle to be set, as that indicates the thread has exitted:
::InterlockedExchange(&lTerminate, 1);
::WaitForSingleObject(hSecondThread, INFINITE);
Easy as...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Sweet! Have my vote, sir.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
When asked to close, the primary thread needs to signal an event that the secondary thread is monitoring. Once signaled, the primary thread would then wait for the secondary thread to finish. This is very easy to do with two CEvent objects.
[edit]
I now see that Rajesh has an excellent example.
[/edit]
"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
|
|
|
|
|
Hi all,
i m using SuspendThread to suspend the thread after this the next statement to be executed is Message box for confirmation of termination of thread,but when this line execute the control not display after this and window becomes unresponsive,i mean message box not return any value.
CWinThread* load_file;
load_file=AfxBeginThread(Load_ThreadProc,NULL,THREAD_PRIORITY_HIGHEST,0,0,NULL);
void CLoad_Modeless::OnBnClickedStopBtn()
{
if(load_file!=NULL)
{
::SuspendThread(load_file->m_hThread);
if(AfxMessageBox(("Do you really want to stop process?"),MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON2) == IDYES)
{
::TerminateThread(load_file->m_hThread,0);
load_file=NULL;
}
else
::ResumeThread(load_file->m_hThread);
}
}
please help me for this
thanks in advance.
To accomplish great things, we must not only act, but also dream;
not only plan, but also believe.
|
|
|
|
|
Don't use the SuspendThread , TerminateThread and ResumeThread functions for the type of thread management you're doing here. From the documentation on SuspendThread[^]:
This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread. To avoid this situation, a thread within an application that is not a debugger should signal the other thread to suspend itself. The target thread must be designed to watch for this signal and respond appropriately.
There are many other explanations of the kind of problems these functions can cause if used incorrectly and the reasons why, such as the following: Why you should never suspend a thread[^]
Steve
|
|
|
|
|
As already mentioned earlier[^], the TeminateThread, SuspendThread and ResumeThread should be avoided. There are much better mechanism to handle this type of situation (events for instance).
I think people already mentioned it to you, but you should really have a look at this article[^] and understand it. Threads are not an easy thing for beginner, so instead of trying things by your own, it would be much more efficient to read a good article about it.
|
|
|
|
|
These functions do not work the way you think they do. After the thread is suspended, no more code from the thread will be executed, and it won't be scheduled any processor time until it is resumed (A thread can suspend itself, but unfortunately cannot resume itself). If your thread is suspended, some other thread has to wake it up so that it can become "schedulable" again.
What you've designed is never going to work as it is fundamentally flawed. Please read up on threading and understand how it works. There are plenty of articles at CP and you should probably read a good book if you're serious about writing multi-threaded code. After all that, there are gotchas involved in threading that you'll only experience and learn by writing code.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|