|
|
Below is my code tried
class CVersion
void CVersion::ReadValue(BYTE *Buffer, int n_Length, int n_Source)
{
EnterCriticalSection(&m_sReadCritical);
CString tempBuffer((BYTE*)Buffer);
m_svalue = tempBuffer;
m_cData->Data(this);
LeaveCriticalSection(&m_sReadCritical);
}
class CClassVersion
void CClassVersion::Data(CVersion *pcVers)
{
CString m_Value1 = pcVers->m_sValue;
this->SetDlgItemTextA(IDC_EDIT_VAL,m_Value1);
}
//Declared in CVersion header file
protected:
CClassVersion* m_cData;
An Runtime error occurs"Unhandled exception at 0x782ac7da in .exe:0xC0000005:Access violation reading location 0x00000020."
after SetDlgItemText stmt.
Please guide me for the same.
modified 2-Mar-15 1:23am.
|
|
|
|
|
You should step through the code with your debugger to see which variable is invalid. There is nothing obvious in the above code, although it is not easy to understand what it is supposed to be doing.
|
|
|
|
|
Can i use callback function over der?How??????please guide me for the same.Thankyou
|
|
|
|
|
Member 11438021 wrote: Can i use callback function For what reason? You first need to diagnose what is happening in your code to cause the error. When you enter the critical section what are you trying to synchronise with?
[edit]
void CVersion::ReadValue(BYTE *Buffer, int n_Length, int n_Source)
{
EnterCriticalSection(&m_sReadCritical);
CString tempBuffer((BYTE*)Buffer);
m_svalue = tempBuffer;
m_cData->Data(this);
LeaveCriticalSection(&m_sReadCritical);
}
You are setting m_svalue to point to tempBuffer , but as soon as you exit this function that buffer will get released so the pointer is no longer valid. Looking at this code again I cannot see what useful purpose it serves.
[/edit]
|
|
|
|
|
I want to update the text(string) on u/i page.I am sending the message on button click and want to receive the same string on Edit controlBox.
|
|
|
|
|
Then you need to manage the lifetime of the string. If you pass it from one thread to another then you must ensure it is not deleted from its source until the destination has taken a secure copy. In your sample code your destination thread is taking a copy, but only into a temporary location which immeditely gets disposed. Assuming that m_svalue is a string pointer then you just need to allocate a buffer of the required size to it, and copy the message string across. You can then delete it at some later point.
|
|
|
|
|
Can you just give the sample code for the same.Because i tried ,but i am doing a small mistake somewhere.How will i store the string in a buffer so that it can be accessible from another class and use for updating the text.
|
|
|
|
|
Sample code for what? Allocate a memory buffer, copy the string into it, and pass it to the receiver. The receiver can then keep that buffer until it is no longer needed.
|
|
|
|
|
I assume this is a threading problem. The code can probably be called from different threads (that's why you use a Critical Section), but the control text can only be set from the GUI thread.
The easiest way to achieve this is using a timer. So in the ReadValue() fucntion you only put the data into m_svalue. And in the OnTimer() function (which runs in GUI thread) you call Data(). This would of cource mean that you need to store the most recently used CClassVersion object somewhere.
The good thing about pessimism is, that you are always either right or pleasently surprised.
|
|
|
|
|
Can i use Handler or callback function,if yes.How???
Guide for the same.Ty.
|
|
|
|
|
How is m_cData initialized?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
|
|
|
|
|
m_cData is declared as pointer
|
|
|
|
|
I understand that, but what value are you assigning to it?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
|
|
|
|
|
I wrote a DLL with VC6.0. I want to pass a LPCSTR type parameter (file path) to a thread. Here is the code:
DWORD WINAPI ThreadFunc(LPVOID lpParam)
{
char* fileLoct = (char*)lpParam;
MessageBox(NULL, fileLoct, "Message", MB_OK |
MB_ICONINFORMATION);
return 0;
}
void __stdcall StartThread(LPCSTR flt)
{
HANDLE hThread;
hThread = CreateThread(NULL, 0, ThreadFunc,
(void *)flt, 0, NULL);
CloseHandle(hThread);
}
I tested it with MessageBox and found the path was not correct at all. How to correctly pass a LPCSTR type parameter to a thread in DLL?
Thank you.
|
|
|
|
|
What was "not correct" about it?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
MessageBox display is not file path, instead it showed something "s" or "c", "7". this is what I don't understand.
|
|
|
|
|
Are you calling the thread from a Unicode function? If you're passing a Unicode string to a function that expects an ANSI string, this is what you'll see.
See: Converting Unicode and ANSI Strings[^]
and: WideCharToMultiByte function[^]
The difficult we do right away...
...the impossible takes slightly longer.
modified 1-Mar-15 20:36pm.
|
|
|
|
|
No, my function is not a Unicode function. my file path is C:\Downloads\HornSound.wav.
I found if I added the following " MessageBox(NULL, loct, "Message1", NULL); " before "CloseHandle(hThread);".
Both MessageBoxes (both in the function and in the thread) can correctly display the file path.
But if I removed "MessageBox" in the function, then the MessageBox in the thread cannot correctly display the file path. What is the problem?
|
|
|
|
|
What is thae value of flt at the point that you call the thread, where does it come from?
|
|
|
|
|
"flt" is the file path from VBA. it's "C:\Downloads\HornSound.wav."
Sorry, it's my mistake. the "loct" in MessageBox should be replaced with "flt".
I spent some time try to figure it out.
So as I said, I added " MessageBox(NULL, flt, "Message1", NULL); " before "CloseHandle(hThread);". both messagebox can correctly display my file
path "C:\Downloads\HornSound.wav."
I also found if I replaced "MessageBox" with "Sleep(3000)" before "CloseHandle(hThread);" MessageBox in thread can correctly display my file
path "C:\Downloads\HornSound.wav."
if I add nothing and just removed "CloseHandle(hThread);", MessageBox in thread can also correctly display my file path "C:\Downloads\HornSound.wav."
It seems I cannot close handle right after CreateThread. but that can cause memory leak?
|
|
|
|
|
The fact that it works when you add a delay suggests that the file path buffer is getting destroyed before the thread process has constructed the message box. You should ensure the buffer is preserved until the thread has finished using it, by some form of synchronisation. Or better still don't use threads unless they are serving some necessary function.
|
|
|
|
|
What doesn't work? compiler error? What's the error? Runtime error? Describe it.
Give people something to go on!
Steve
|
|
|
|
|
no compiler error. when I calling the DLL from VBA, the function just cannot pass a file path to the thread. File path in thread displayed as "s", "c", etc.
|
|
|
|
|
You are using an automatic variable sent from VB call. That variable, and the string contente, will be destroyed after the function that create the thread returns. The thread instead will be running and looking for the string at the address that you passed in the thread creation, but that address holds only garbage by then...
To make it work create a local string in your code and define it 'static', copy the passed string there, then create the thread.
void __stdcall StartThread(LPCSTR flt)
{
HANDLE hThread;
static char *szStaticString[MAX_PATH];
strncpy(szStaticString, flt, MAX_PATH-1);
hThread = CreateThread(NULL, 0, ThreadFunc,
(void *)szStaticString, 0, NULL);
CloseHandle(hThread);
}
|
|
|
|