|
Thanks, it works very well
Also, for anyone who could be interested: we could use a simple function to check if sound card exists and then switch between using Beep() or PlaySound()
BOOL m_bIsSoundCardExist = (waveOutGetNumDevs() > 0);
|
|
|
|
|
You are welcome. Thanks for the tip.
-Saurabh
|
|
|
|
|
|
Using C++ and MFC - is it possible to display an MDI Child window outside of it's MDI Parent - sort of like a modeless dialog?
I know it's goofy - but it is the quick solution to a requirement for a legacy application...
|
|
|
|
|
Have you tried setting the child frame's owner and/or parent to the desktop instead?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I've tried putting the following code in the MainFrame, the MDIChildWnd and the View with no luck:
CWnd* prnt = (CWnd*)::GetDesktopWindow();
if(prnt)
this->SetParent(prnt);
The actual create code is buried in the MFC DocView architecture somewhere - I don't know what method can be overriden to try to pass in the desktop window as the parent.
Maybe someone can help with that?
|
|
|
|
|
This is just a guess, but usually a parent window will clip any child window. Possibly you need to override the PreCreate of the MDI Parent and change it's attributes so as not to clip children.
|
|
|
|
|
Hi everybody.
I have a problem with Serial Communication in MFC. My program could not open serial port and the error is INVALID_HANDLE_VALUE. But if I open the Hyper Terminal program before opening my program, my code is running and I had succeed open port and read port operations. I can't understand this situation.
Thanks in advance...
Port settings are like these (These are parameters)
Baud Rate : 9600
Parity : Even
Stop Bits : 1
Data Bits : 7
And the scale is connected COM1 port to the computer.
My simple code is below as:
////////////////////////////
dcb.DCBlength=sizeof(DCB);
dcb.BaudRate=(unsigned long)atol(m_sBaudRate);
if ( m_sParity.Compare("None") == 0 )
dcb.Parity = NOPARITY;
else
if ( m_sParity.Compare("Even") == 0 )
dcb.Parity = EVENPARITY;
else
if ( m_sParity.Compare("Odd") == 0 )
dcb.Parity = ODDPARITY;
else
dcb.Parity = NOPARITY;
.........
.........
hComm = CreateFile(m_sCOM /*COM1*/,
GENERIC_READ | GENERIC_WRITE,
0,
/* comm devices must be opened w/exclusive-access */ NULL,
/* no security attrs */ OPEN_EXISTING,
/* comm devices must use OPEN_EXISTING */ 0,
/* not overlapped I/O */ NULL /* hTemplate must be NULL for comm devices */ );
if (hComm == INVALID_HANDLE_VALUE)
{
AfxMessageBox("Port not opened!");
}
..............................
..............................
ReadData(m_sResults);
///////////////////////////////////////////////////////////
|
|
|
|
|
Why are you setting DCB values before actually open the communication port?
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
|
|
|
|
|
I tried to set DCB properties after calling CreateFile(..) method but it didn't work.
|
|
|
|
|
sahanaskin wrote: if (hComm == INVALID_HANDLE_VALUE)
{
AfxMessageBox("Port not opened!");
}
Did it ever occur to you to call GetLastError() here?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
|
sahanaskin wrote: Yes, getlasterror returns 1,
Which equates to ERROR_INVALID_FUNCTION .
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I'm sorry; I had wrote returning value incorrect, GetLastError() returns 0 in my program.
|
|
|
|
|
Well, if CreateFile() is returning INVALID_HANDLE_VALUE and GetLastError() is returning 0 , I don't know what to tell you.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Yes, exactly same as you said
Thanks anyway.
|
|
|
|
|
Has suggested by David Crow the two conditions should be mutually exclusive. Please, clean the code and try again.
(Try also the classical MSDN sample code [^]).
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
|
|
|
|
|
Other than the first argument (he doesn't indicate what value m_sCOM has, only what the comment shows), his call to CreateFile() looks identical.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I changed some part of my code. Now, I wrote GetLastError section like belove and the getlasterror result was 6:
hComm_ = CreateFile("COM1", // Specify port device: default "COM1"
GENERIC_READ | GENERIC_WRITE, // Specify mode that open device.
0, // the devide isn't shared.
NULL, // the object gets a default security.
OPEN_EXISTING, // Specify which action to take on file.
0, // default.
NULL); // default.
// Get current configuration of serial communication port.
if (GetCommState(hComm,&dcb) == 0)
{
char sMsg[512];
DWORD dwError = GetLastError();
sprintf(sMsg, "Error=%d ",
dwError);
AfxMessageBox(sMsg); // prints to screen 6
return FALSE;
}
|
|
|
|
|
And dcb variable has these attributes (I set them in constructor):
dcb.ByteSize = 7;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = EVENPARITY;
dcb.BaudRate = CBR_9600;
|
|
|
|
|
sahanaskin wrote: ...the getlasterror result was 6:
Which equates to ERROR_INVALID_HANDLE .
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Am I wrong to call CreateFile(..) method code section or what? When does The ERROR_INVALID_HANDLE error happen? please help me I'm confused?
hComm_ = CreateFile("COM1", // Specify port device: default "COM1"
GENERIC_READ | GENERIC_WRITE, // Specify mode that open device.
0, // the devide isn't shared.
NULL, // the object gets a default security.
OPEN_EXISTING, // Specify which action to take on file.
FILE_FLAG_OVERLAPPED, // default.
NULL); // default.
// Get current configuration of serial communication port.
if (GetCommState(hComm, &dcb) == 0)
......
|
|
|
|
|
sahanaskin wrote: When does The ERROR_INVALID_HANDLE error happen?
This happens when CreateFile() fails. Your CreateFile is probably returning INVALID_HANDLE_VALUE.
As has been previously stated, you need to call GetLastError() to see why this is occuring. Modify your code as follows:
hComm_ = CreateFile("COM1",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if(INVALID_HANDLE_VALUE == hComm_)
{
DWORD dwErr = GetLastError();
LPTSTR lpMsgBuf = NULL;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,dwErr,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf,0,NULL);
TRACE(_T("ERROR: %s\n"),lpMsgBuf);
LocalFree(lpMsgBuf);
}
|
|
|
|
|
Yes, I suggest him to use the MSDN sample as it stands, without preliminary stuff.
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
|
|
|
|
|
Hi,
MSDN tells us this:
--COM ports
--The CreateFile function can create a handle to a COM port. By setting the dwCreationDisposition
--parameter to OPEN_EXISTING, read-only, write-only, or read/write access can be specified
But I did not successed to open port. If I open the hyper terminal program only one time, and then i closed hyper terminal ; this code works and serial_handle is not equal invalid.. :
if (serial_handle!=INVALID_HANDLE_VALUE)
CloseHandle(serial_handle);
serial_handle = INVALID_HANDLE_VALUE;
serial_handle = CreateFile("COM1",GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING ,NULL,NULL);
if ( serial_handle == INVALID_HANDLE_VALUE )
{
GetLastError(); // it returns 0
I couldn't understand this situation? Please tell me where am I wrong?
|
|
|
|