|
Richard MacCutchan wrote: You should use the OnInitDialog method of CDialog Class | Microsoft Docs[^] to set the focus.
I stated that the focus edit is being set from the OnInitialUpdate override of the CFormView derived class, I should have clarified that this has been called from the CDialog OnInitDialog method via a call to
SendMessageToDescendants(WM_INITIALUPDATE, 0, 0, TRUE, TRUE);
Also, the OnInitDialog method returns FALSE to indicate that I have set the focus to a control.
.
|
|
|
|
|
Sorry, I cannot help further as I don't have MFC.
|
|
|
|
|
Is it possible to somehow check during compile time if an enum exists, i.e. something like this:
typedef enum {
MY_ENUM_VALUE = 0
} myEnum_e;
#define DOES_ENUM_EXIT(__ARG__) ??? What goes here???
if (DOES_ENUM_EXIT(myEnum)) {
} I know that I could achieve something similar easily be using
#define MY_ENUM_VALUE (0)
instead, but then my code wouldn't be type safe.
|
|
|
|
|
|
No, because the #define statement is handled by the preprocessor, and the enum value is not known until it is fed into the compiler, which comes later.
|
|
|
|
|
|
|
I have written a little piece of code to read few (first 512) bytes from USB drive. here is the code:
HANDLE hVolume = ::CreateFile(sVolume, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
TRACE("Volume handle: %p\n", hVolume);
LONG lDistLow = 0;
PLONG plDistHigh = 0;
if (INVALID_SET_FILE_POINTER == SetFilePointer(hVolume, lDistLow, plDistHigh, FILE_BEGIN) &&
NO_ERROR != GetLastError())
{
TRACE("Error: %d\n", GetLastError());
return FALSE;
}
else
{
TRACE("%d\t%d\n", lDistLow, plDistHigh);
}
DWORD dwLen = 512;
DWORD dwNum = 0;
void* pBuffer = NULL;
ReadFile(hVolume, pBuffer, dwLen, &dwNum, NULL);
TRACE("%p\t%d\t%d\n", pBuffer, dwLen, dwNum);
but the result is:
Volume handle: 00000438
0 0
00000000 512 0
this pBuffer NULL tell me that I am not able to read that sectors ... why ? The program is running as admin and sVolume is feed by _T("\\\\.\\F:") , first TRACE macro printed is a prove that is OK that part. SetFilePointer is used to position the reading part, and I intend to setup offset to the beginning, right ?
|
|
|
|
|
What does ReadFile return? TRUE or FALSE?
|
|
|
|
|
The ReadFile function returned FALSE, which tell me that reading has failed.
|
|
|
|
|
In this case you have to call GetLastError to obtain the exact reason of the failure!
|
|
|
|
|
I didn't feed correctly the buffer. After I setup correctly that parameter, ReadFile return TRUE.
|
|
|
|
|
Your pBuffer value is NULL, so you are trying to read data into the memory address 0 - you have to allocate some space to read the data into and use that for your pBuffer value. For 512 bytes a stack-based array should do the trick.
|
|
|
|
|
_Flaviu wrote: pBuffer NULL tell me that I am not able to read that sectors No, pBuffer = NULL; tells you that you have not allocated any space to pBuffer. And don't use void* unless you are trying to allocate nothing.
You can allocate space either of the following ways.
unsigned char* pBuffer = new unsigned char[dwLen]; unsigned char buffer[dwLen]; ReadFile(hVolume, buffer, dwLen, &dwNum, NULL);
|
|
|
|
|
Is not accepted:
unsigned char buffer[dwLen];
Of course, putting 512 is ok, but I need to allocate that length of buffer through a variable.
I prefer this option, on stack, as far as I know, is faster than heap.
|
|
|
|
|
You can fix that easily by doing it this way:
#define SECTOR_SIZE 512
DWORD dwLen = SECTOR_SIZE;
DWORD dwNum = 0;
char buffer[SECTOR_SIZE];
ReadFile(hVolume, buffer, dwLen, &dwNum, NULL);
If you are using C++ then instead of #define you can use this:
const int SECTOR_SIZE = 512
|
|
|
|
|
Good idea. I have tried in this way:
CByteArray arrByte;
arrByte.SetSize(512);
BOOL bRet = ReadFile(hVolume, arrByte.GetData(), dwLen, &dwNum, NULL);
and seem to go well.
|
|
|
|
|
If you are writing MFC code then you should be using CFile to handle all your I/O.
|
|
|
|
|
|
|
|
I should know that ... thank you.
|
|
|
|
|
Good idea. I have few troubles with accessing USB drive with CFile, but once I'll solve it, the code will be simple.
Here is my trial, none on them has worked:
file.Open(_T("\\\\.\\F:"), CFile::modeRead | CFile::osSequentialScan);
file.Open(_T("F:"), CFile::modeRead | CFile::osSequentialScan);
file.Open(_T("F:\\"), CFile::modeRead | CFile::osSequentialScan);
HANDLE hVolume = ::CreateFile(sVolume, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
CFile file(hVolume);
CFileStatus status;
file.GetStatus(status);
|
|
|
|
|
When using CFile::Open you should pass the CFileException* parameter to get the failure cause if CFile::Open fails.
See the example in CFile::Open
|
|
|
|
|
It is just the same as OpenFile behind the scenes so nothing to do with CFile . Your call probably fails because you are not including the share flags in your call to CFile::Open(), which are required when trying to access a device.
|
|
|
|