|
Hi Gurus,
Does using RegFlushKey automatically close the handle to a new registry key created using RegCreateKeyEx function. Or we have to use RegCloseKey if the handle needs to be closed?
Thanks
C++beginer
|
|
|
|
|
All RegFlushKey() does is write your changes to the registry rather than letting the OS's lazy flusher do the job. It does not close your handle, you have to do that yourself.
|
|
|
|
|
Ok. Thanks for clearing the doubt.
C++beginer
|
|
|
|
|
Hi,
I wanted to create a threaded dialog app however I couldn't find a simple example I could build on. I have now figured out a way to do it mainly based on this article (Unfortunaly you have to download windows speech to get it to run). So I am thinking of using it to create my first article here on code project.
My dialog consists of a start and stop button and an edit control.
The code for the start and stop buttons:
void CSimpleThreadDlg::OnBnClickedOk()<br />
{<br />
int i = 3;<br />
theApp.m_stop = false;<br />
theApp.m_pThread =AfxBeginThread(simThread,(LPVOID)i);<br />
m_start.EnableWindow(0);<br />
}<br />
<br />
void CSimpleThreadDlg::OnBnClickedStop()<br />
{<br />
theApp.m_stop = true;<br />
m_start.EnableWindow(1);<br />
}
And the code for my simple thread:
#include "stdafx.h"<br />
#include "SimpleThread.h"<br />
#include "thread.h"<br />
<br />
unsigned int simThread(LPVOID pParam)<br />
{<br />
unsigned int i = (unsigned int)pParam;<br />
CWnd* pEditControl=theApp.m_pMainWnd ->GetDlgItem (IDC_EDIT1);<br />
CString message;<br />
while(!theApp.m_stop)<br />
{<br />
pEditControl->GetWindowText(message);<br />
message.AppendFormat(_T("\r\nHello World #%i"),i);<br />
pEditControl->SetWindowText(message);<br />
Sleep(500);<br />
i++;<br />
if(i>10)<br />
{<br />
i = 0;<br />
<br />
pEditControl->SetWindowText("");<br />
}<br />
}<br />
PostQuitMessage(0);<br />
return 0;<br />
}
As you can see I disabled the Start button until the Stop button is pressed. If I don't do that it is possible to create multiple theads (Which is a bad thing since I believe it would overwrite the pointer - still leaving the existing thread running. The multiple threads also fight for the dialog.). When the dialog is closed in this situation the debugger reports memory leaks.
Now for my real app I want to kill the thread when the dialog closes...
CSimpleThreadDlg dlg;<br />
m_pMainWnd = &dlg;<br />
INT_PTR nResponse = dlg.DoModal();<br />
if (nResponse == IDOK)<br />
{<br />
}<br />
else if (nResponse == IDCANCEL)<br />
{<br />
}<br />
MSG msg;<br />
theApp.m_stop = true;
return FALSE;<br />
}
However this also has a memory leak, since the application closes before the second thread has a chance to execute...
So what is the correct way around this one? A simple Sleep(10); does the trick since it gives the other thread a chance to run and read m_stop.
Or I figured I could look at PeekMessage to see if the thread had sent the WM_QUIT message. However when debugging I looked at what peak message returned before setting m_stop and it equalled WM_QUIT. (I assumme it was the quit message for the dialog?) So the following loop to check if the thread has finished is not very useful.
<br />
do{ <br />
Sleep(10);<br />
PeekMessage(&msg, dlg, 0, 0, PM_NOREMOVE);<br />
}<br />
while(msg.message != WM_QUIT);
Another question - is the thread a UI thread or a worker thread?
Thanks for your help
Nick
|
|
|
|
|
Use CreateThread or _beginthreadex.
Then wait on the thread handle.
e.g.
theApp.m_pThread = _beginthreadex(...);
...
theApp.m_stop = true;
switch( WaitForSingleObject(theApp.m_pThread, 10000) ) {
case WAIT_TIMEOUT : TerminateThread(); break;
case WAIT_FAILED : ...; break;
case WAIT_OBJECT_0 : ...; break;
}
[EDIT]
From MSDN:
The system maintains a single system message queue and one thread-specific message queue for each graphical user interface (GUI) thread. To avoid the overhead of creating a message queue for non–GUI threads, all threads are created initially without a message queue. The system creates a thread-specific message queue only when the thread makes its first call to one of the User or Windows Graphics Device Interface (GDI) functions.
[/EDIT]
...cmk
Save the whales - collect the whole set ca
|
|
|
|
|
Hi,
Thanks for your help. You got me on the right track. Note AfxBeginThread calls CreateThread. Also that WaitForSingleOptject requires the handle of the thread as below....
switch( WaitForSingleObject(theApp.m_pThread->m_hThread, 10000) ) {<br />
case WAIT_TIMEOUT : TerminateThread(theApp.m_pThread->m_hThread,0); break;
Cheers
Nick
|
|
|
|
|
Hello all
I am writing a Multilingual app, which has to run on
ENG, GERMAN, FRENCH and JAPNEESE os. Now what I have
done is , in one app i made 4 resources and change the
resource accordingly.
But I have one doubt? I heard this can be even achived
by writing 4 resource dll ! I am bit confused now
Which is the better way? I found 4 string table in
one app is the simplest way.
pleas put on your sugg and ideas. thanks
|
|
|
|
|
It really depends on the size of the resources and you future intentions. With resource dll's it is possible to load only the required dll cutting back on memory usage, also if there are any translation problems you only need to redistribute a simgle dll rather than the whole exe. I also find it makes coding a lot easier. When the app starts all it needs to do load the correct dll and store the HINSTANCE, all the resource id's are the same so there's no need mess about with the locale.
If you enumerate the dll's on startup, you can also add further translations without adding code to the main exe.
On the downside, each time you want to change a resource, you have to rebuild them all.
|
|
|
|
|
if you want to change only the strings on the GUI interface for example, no DLL is needed, even though you can use Resource DLLs...
basically, you put all your strings in the Resource String Table. then, you set the language of this string table (in its properties). after that, you copy the string table (and tell VC++ its new target language), and translate the strings into the proper language of the table.
in your application, you have to load the strings (by using CString::Format() or CString::LoadString() for example), and nothing more !
the system will be recognized at the program start, and the correct string table will be used.
|
|
|
|
|
What is the difference between a Device Independent Bitmap and a normal BITMAP? Any specific answers?
---
Hakuna-Matada
It means no worries for the rest of your days...
It's our problem free, Philosophy
<marquee behavior="alternate" scrollamount="5" scrolldelay="50">
|
|
|
|
|
With a DIB, the format of the pixels is independent of a display device. .BMP files are DIBs.
By contrast, a device dependent bitmap is specific to a video device. In theory you could do a bitwise copy to the video buffer and have the bitmap display correction. (In theory; in reality, video devices often use complex layer that precludes actually doing this.)
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
DIBs have enough information for it to be displayed on any device (printer, screen, etc). That is information such as palettes, RGB/CMYK values, gamma values, etc. A device dependent bitmap *may* be contain enough information to be displayed on another device, but is not required to. The best example of a dependent bitmap is one that uses a palette. Without the palette, the bitmap is at best an array of random bytes for any other use...
--
Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!
|
|
|
|
|
hey all,
In my code, the data gets read via USB in a buffer.
i need to save this buffer to a file/ location in my PC (eg., C:/Documents/file1) everytime new data is received.
The file name has to be different everytime new data is read.
how do i do it?
can anyone help with some code snippet or the logic or the functions to be used to achieve this.
Thanks in advance.
|
|
|
|
|
Inorder to make the file name different add curent date and time as suffix for a phrase
say file1_mmddyyyhhmmss
|
|
|
|
|
thanks for the suggestion.
But i need to know how to store the buffer in a file?
|
|
|
|
|
wat is the real problem ?
to read USB or to save the read buffer ?
|
|
|
|
|
i can read usb--no problem in that.
But how do I save the read buffer in to a file of desired location.?
|
|
|
|
|
sorry 4 delay
pBuff = USBReadBuff; // where pBuff and USBReadbuff are char*
CFile TestFile;
CString csFileName = C:\TestFile.txt; // csFileName is the path and name where u want to create the file
TestFile.Open( csFileName, CFile::modeCreate | CFile::modeWrite ))
TestFile.Write( &pBuff, no of bytes )
Is this ok for u
RinuRaj
|
|
|
|
|
thanks very much. I will try now in my program.
Can u also suggest how to have a different file name each time new set of data is received from USB.?
|
|
|
|
|
This is one way. It's based on Rinu_Raj's suggestion (file1_mmddyyyhhmmss)
CString csFileName;
CTime ct = CTime::GetCurrentTime();
csFileName = "file1_" + ct.Format("%m%d%Y%H%M%S");
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
thanks. When I tried the code in my program, it fails giving
:Debug assertion failed in filecore.cpp line238.
which is the CFile::Write() function.
the line is:
ASSERT(m_hFile != (UINT)hFileNull);
how to solve?
|
|
|
|
|
Did you acually open/create the file before trying to write the data?
See this suggestion[^]
Another thing, it's always wise to close a file manually, even if the CFile destructor does it for you....
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
i did exactly this:
CFile TestFile;<br />
CString csFileName = "C:/Documents/TestFile.txt"; TestFile.Open(csFileName,CFile::modeCreate | CFile::modeWrite);<br />
TestFile.Write(&Rx_Buf,3000);
what needs to be done?
|
|
|
|
|
Check the return value from the TestFile.Open call, such as:
<br />
CFile TestFile;<br />
CString csFileName = "C:/Documents/TestFile.txt"; <br />
if(TestFile.Open(csFileName,CFile::modeCreate | CFile::modeWrite)) {<br />
TestFile.Write(&Rx_Buf,3000);<br />
TestFile.Close();<br />
}<br />
else {<br />
}<br />
My guess is that there are no C:/Documents folder.
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
your guess is absolutely correct.
There is no C:/Documents..
I am storing the data values (numbers) in the file. but when I open the file TestFile.txt...i cant read the numbers. its all ASCII i guess.
In what format should I be storing the file as?
|
|
|
|