|
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?
|
|
|
|
|
Why do you preventively close the handle? Why don't you copy the MSDN sample as it stands (possibly changing the port string) and try?
BTW Could you please post the routine complete 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
|
|
|
|
|
My method is this:
/************************************************************************************************/
void CDlgComPortConnectTest::OpenPort()
{
//if (serial_handle!=INVALID_HANDLE_VALUE)
// CloseHandle(serial_handle);
//serial_handle = INVALID_HANDLE_VALUE;
serial_handle = 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 */
NULL, /* not overlapped I/O */
NULL /* hTemplate must be NULL for comm devices */ );
if (GetCommState(serial_handle,&dcb) == 0)
{
char sMsg[512];
DWORD dwError = GetLastError();
sprintf(sMsg, "Error=%d ",dwError);
AfxMessageBox(sMsg);
AfxMessageBox(GetLastError());
AfxMessageBox("Get configuration port has problem.");
return;
}
FillMemory(&dcb, sizeof(dcb), 0);
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate=(unsigned long)atol(m_sBaudRate);//(unsigned long)(9600);
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;
if ( m_sStopBits.Compare("1") == 0 )
dcb.StopBits = ONESTOPBIT;
else
if ( m_sStopBits.Compare("1.5") == 0 )
dcb.StopBits = ONE5STOPBITS;
else
if ( m_sStopBits.Compare("2") == 0 )
dcb.StopBits = TWOSTOPBITS;
else
dcb.StopBits = ONESTOPBIT;
if ( m_sDataBits.Compare("7") == 0 )
dcb.ByteSize = 7;
else
if( m_sDataBits.Compare("8") == 0 )
dcb.ByteSize = 8;
else
dcb.ByteSize = 8;
dcb.fAbortOnError = TRUE;
if (serial_handle == INVALID_HANDLE_VALUE)
{
CString sDC;
sDC.Format("B_commopen = FALSE;\n%d, %d, %d, %d", dcb.XonChar, dcb.XoffChar, dcb.XonLim, dcb.XoffLim);
AfxMessageBox(sDC);
B_commopen = FALSE;
char sMsg[512];
DWORD dwError = GetLastError();
sprintf(sMsg, "Error=%d ",
dwError);
AfxMessageBox(sMsg);
ASSERT(FALSE);
// error opening port; abort
}
else
{
B_PortReady = SetupComm(serial_handle, 128, 128); // set buffer sizes
B_commopen = TRUE;
}
Read(m_sResults);
}
|
|
|
|
|
You're testing the handle value in the wrong place. You should test it immediately after the CreateFile call (failure), otherwise you'll not get meaningful info by GetLastError (other function calls reset last error). Moreover it doesn't make any sense calling GetCommState with an invalid handle as argument.
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
|
|
|
|
|
Thanks for your advice. I wrote the function as you said and GetLastError function returned 5 (ACCESS DENIED), how I solve this error?
:
/************************************************************************************************/
void CDlgComPortConnectTest::OpenPort()
{
//if (serial_handle!=INVALID_HANDLE_VALUE)
// CloseHandle(serial_handle);
//serial_handle = INVALID_HANDLE_VALUE;
serial_handle = 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 */
NULL, /* not overlapped I/O */
NULL /* hTemplate must be NULL for comm devices */ );
if (serial_handle == INVALID_HANDLE_VALUE)
{
B_commopen = FALSE;
char sMsg[512];
DWORD dwError = GetLastError();
sprintf(sMsg, "Error=%d ",
dwError);
AfxMessageBox(sMsg);
ASSERT(FALSE);
// error opening port; abort
}
if (GetCommState(serial_handle,&dcb) == 0)
{
char sMsg[512];
DWORD dwError = GetLastError();
sprintf(sMsg, "Error=%d ",dwError);
AfxMessageBox(sMsg);
AfxMessageBox(GetLastError());
AfxMessageBox("Get configuration port has problem.");
return;
}
FillMemory(&dcb, sizeof(dcb), 0);
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate=(unsigned long)atol(m_sBaudRate);//(unsigned long)(9600);
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;
if ( m_sStopBits.Compare("1") == 0 )
dcb.StopBits = ONESTOPBIT;
else
if ( m_sStopBits.Compare("1.5") == 0 )
dcb.StopBits = ONE5STOPBITS;
else
if ( m_sStopBits.Compare("2") == 0 )
dcb.StopBits = TWOSTOPBITS;
else
dcb.StopBits = ONESTOPBIT;
if ( m_sDataBits.Compare("7") == 0 )
dcb.ByteSize = 7;
else
if( m_sDataBits.Compare("8") == 0 )
dcb.ByteSize = 8;
else
dcb.ByteSize = 8;
dcb.fAbortOnError = TRUE;
B_PortReady = SetupComm(serial_handle, 128, 128); // set buffer sizes
B_commopen = TRUE;
}
Read(m_sResults);
}
|
|
|
|
|
That means the serial port is currently owned by another application.
Have you closed the hyperterminal?
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
|
|
|
|
|
No, I didn't open hyperterminal. Is there any reason for this error?
|
|
|
|
|
As I suggested before, probably there is another application that owns the serial port; the latter cannot be shared and hence the error. You may try to open indeed the hyperterminal on the same port and see if it succeeds.
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
|
|
|
|
|
You're right. I tried to open and connect COM1 port with Hyper Terminal program but I got "Another program is using.." warning. I restarted computer and I will try again. Thank you for your all advices and helps.
|
|
|
|
|
You're welcome.
BTW: remember to call CloseHandle when you've done with the serial 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
|
|
|
|
|
See does this[^] helpful for you?
|
|
|
|
|
Hi,
Quick question; im a developer currently working on a win32 project in C/C++/ASM that utilizes a lot of debugging theory in order to work. However, at this point, i cannot seem to find a way to list all process memory maps running in a win32 process. I can extract a full list of modules, and mappings, but i cannot find anything that isn't loaded as an executable module (e.g. Stacks, Heaps, other Maps).
What i really need is a way to extract a list of all process memory maps in a similar fashion to how ollydbg displays them, so i can use the list of maps for internal program calculations.
Any help would be appreciated as i've spent hours scouring the web for an answer.
~Jason
|
|
|
|
|
|
Perhaps something like this is what you're after:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
SYSTEM_INFO si;
GetSystemInfo(&si);
char *pMin = (char*)si.lpMinimumApplicationAddress;
char *pMax = (char*)si.lpMaximumApplicationAddress;
for (char* pAddress = pMin; pAddress<pMax; )
{
MEMORY_BASIC_INFORMATION mbi;
DWORD res = VirtualQuery(pAddress, &mbi, sizeof(mbi));
if (res != sizeof(mbi))
{
cerr << "VirtualQuery failed!" << endl;
return -1;
}
if (mbi.State == MEM_COMMIT)
{
DWORD base = (DWORD)mbi.BaseAddress;
cout << hex << showbase << base << " - " << base + mbi.RegionSize-1 << endl;
}
pAddress += mbi.RegionSize;
}
return 0;
}
Steve
|
|
|
|
|
Yes, that is almost exactly what i need to do, except not from within the process itself. I need to break into the application remotely using the OpenProcess/Thread functions and utilizing the debug libraries, and enumerate address space from there. Looks like i can just use VirtualQueryEx and enumerate that way. I'm gonna go give it a try, thanks for the insight.
~jason
|
|
|
|
|
how to compress/decompress image using lzw algorithm???
is there anyone know?
please send me a sample code , related links , some demo program ,
and many other that can help my project
thanks a lot...
|
|
|
|
|