|
TopCoder23 wrote: File access must be for numbers of bytes that are integer multiples of the volume's sector size.
This is not true, a write can be for any number of bytes; see here[^] for details.
It's time for a new signature.
|
|
|
|
|
He opened the file with FILE_FLAG_NO_BUFFERING. That requires specific alignment and write size restrictions.
So given the following code and assuming 4096 is a correct size, if the write size is set to 16, error 87 will be returned.
void* pBuffer = _aligned_malloc(4096, 4096);
lstrcpyA((LPSTR)pBuffer, "Hello");
DWORD bytesWritten;
HANDLE hFile = CreateFile(L"C:\\Temp\\~tmp.tmp", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_FLAG_NO_BUFFERING, NULL);
if (!WriteFile(hFile, pBuffer, 4096, &bytesWritten, NULL))
{
printf("Write Error: %u\n", GetLastError());
}
CloseHandle(hFile);
_aligned_free(pBuffer);
|
|
|
|
|
Joe Woodbury wrote: He opened the file with FILE_FLAG_NO_BUFFERING.
Yeah, I missed that; thanks.
It's time for a new signature.
|
|
|
|
|
DWORD DownloadThread(LPVOID lpdwThreadParam)
{
CDownload* Download = (CDownload*)lpdwThreadParam;
WSADATA WS2Info;
if (!WSAStartup(MAKEWORD(2,2), &WS2Info))
{
SOCKET IPv4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (IPv4 != INVALID_SOCKET)
{
hostent *ResolveHost;
char *HostName = new char[wcslen(Download->HostName)+1];
wcstombs(HostName, Download->HostName, wcslen(Download->HostName)+1);
HostName[wcslen(Download->HostName)] = '\0';
ResolveHost = gethostbyname(HostName);
if (ResolveHost)
{
SOCKADDR_IN HostService;
HostService.sin_family = AF_INET;
HostService.sin_addr.s_addr=*((unsigned long*)ResolveHost->h_addr);
HostService.sin_port = htons(Download->Port);
if (connect(IPv4, (SOCKADDR*) &HostService, sizeof(HostService)) != SOCKET_ERROR)
{
char *HTTPrequest = new char[DEFAULT_BUFLEN];
char *RequestURI = new char[wcslen(Download->HostFileLocation)+1];
wcstombs(RequestURI, Download->HostFileLocation, wcslen(Download->HostFileLocation)+1);
RequestURI[wcslen(Download->HostFileLocation)] = '\0';
char *Host = new char[wcslen(Download->HostName)+1];
wcstombs(Host, Download->HostName, wcslen(Download->HostName)+1);
Host[wcslen(Download->HostName)] = '\0';
char *UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.9 Safari/533.2";
sprintf(HTTPrequest,"GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nAccept: */*\r\nAccept-Encoding:\r\nRange: bytes=%lld-\r\n\r\n",
RequestURI, Host, UserAgent, Download->BytesDownloaded);
send(IPv4,HTTPrequest,strlen(HTTPrequest),0);
delete[] HTTPrequest;
delete[] RequestURI, Host, UserAgent;
char *HTTPresponse = new char[DEFAULT_BUFLEN];
SecureZeroMemory(HTTPresponse, sizeof(HTTPresponse));
recv(IPv4, HTTPresponse, sizeof(HTTPresponse), 0);
********
******** HTTP parsing code
********
int PFDB = 0;
for (int n = 0; n < DEFAULT_BUFLEN; n++)
{
if (HTTPresponse[n] == '\r' && HTTPresponse[n-2] == '\r')
if (HTTPresponse[n-1] == '\n' && HTTPresponse[n+1] == '\n')
PFDB = n+2;
}
DWORD BytesReceived = sizeof(HTTPresponse)-PFDB;
int SPEED = 512;
char *Data = new char[SPEED];
SecureZeroMemory(Data, sizeof(Data));
for (int n1 = 0, n2 = PFDB; n2 < DEFAULT_BUFLEN; n1++, n2++)
Data[n1] = HTTPresponse[n2];
Data[DEFAULT_BUFLEN-PFDB] = '\0';
delete[] HTTPresponse;
int DestinationLength = (wcslen(Download->FileLocation)+wcslen(Download->FileName));
wchar_t *LocalPathPFile = new wchar_t[DestinationLength+1];
wcscpy(LocalPathPFile, Download->FileLocation);
wcscat(LocalPathPFile, Download->FileName);
LocalPathPFile[DestinationLength] = '\0';
HANDLE File;
if (Download->BytesDownloaded == 0)
{
File = CreateFile(LocalPathPFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_FLAG_NO_BUFFERING, NULL)
if (File == INVALID_HANDLE_VALUE)
Tu(L"File Error!");
}
else
{
File = CreateFile(LocalPathPFile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_FLAG_NO_BUFFERING, NULL);
if (File == INVALID_HANDLE_VALUE)
Tu(L"File Error!");
SetFilePointer(File, 0, NULL, FILE_END);
}
OVERLAPPED *Struc;
do
{
Download->BytesDownloaded += BytesReceived;
TotalBytesDownloaded += BytesReceived;
if (!WriteFile(File, Data, BytesReceived, NULL, NULL))
{
wchar_t Error[100];
swprintf(Error, L"Write Error: %d", GetLastError());
Tu(Error);
}
BytesReceived = recv(IPv4, Data, sizeof(Data), 0);
}
while (Download->DownloadState == Download->ACTIVE
&& Download->BytesDownloaded < Download->BytesToDownload);
delete[] Data;
shutdown(IPv4, SD_BOTH);
CloseHandle(File);
if (Download->BytesToDownload == Download->BytesDownloaded)
{
TotalBytesDownloaded -= Download->BytesToDownload;
TotalBytesToDownload -= Download->BytesToDownload;
Download->DownloadState = Download->FINISHED;
}
else
{
TotalBytesDownloaded -= Download->BytesDownloaded;
TotalBytesToDownload -= Download->BytesToDownload;
Download->DownloadState = Download->PAUSED;
}
}
}
}
else
closesocket(IPv4);
}
WSACleanup();
ExitThread(0);
}
|
|
|
|
|
There are two errors:
First, the fourth parameter of WriteFile must be a pointer to a DWORD (unless you use overlapped IO.)
Second, FILE_FLAG_NO_BUFFERING has strict rules on how the buffers must be aligned. See http://msdn.microsoft.com/en-us/library/cc644950%28v=VS.85%29.aspx[^]. If you violate those rules, one of the errors returned is 87 (for the misaligned data buffer.) If you want to minimize buffering without having to follow the alignment rules, use FILE_FLAG_WRITE_THROUGH. A short discussion of the two can be found at: http://support.microsoft.com/kb/99794[^] (This is a little misleading; the hard drive itself has an onboard cache and this setting has no effect on it.)
|
|
|
|
|
Hi,
I have lots of class and I have a problem. This is,
"An unhandled exception of type 'System.AccessViolationException' occurred in TrafficSim.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
I have classes and their functions.
class FollowAgent
class CreateAgent
{
FollowAgent* dFollowAgent
}
class DriveAgent
{
dCreateAgent = new CreateAgent(...);
...........
dCreateAgent->dFollowAgent= new FollowAgent(...);
}
I think the error is about usage of "dCreateAgent->dFollowAgent". if I use as "dFollowAgent= new FollowAgent(...);" it works normally, but is not my aim.
How can I solve this problem?
Have a good work!
|
|
|
|
|
I think we don't have enough information to really help you. Where does the exception get raised? I mean, what does your program do when it dies? You are (probably) reading from or writing to memory that does not belong to your process, maybe you are using an object that has been freed earlier or using one that hasn't been created yet.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
Hi,
Program running without any error since the line:
dCreateAgent->dFollowAgent= new FollowAgent(...);
I think the problem is about usage of "dCreateAgent->dFollowAgent". I have 3 couples of files.
1. followagent.cpp and followagent.h consist of FollowAgent class.
2. createagent.cpp and createagent.h consist of CreateAgent class.
3. driveagent.cpp and driveagent.h consist of DriveAgent class.
class FollowAgent
class CreateAgent
{
FollowAgent* dFollowAgent
}
class DriveAgent
{
dCreateAgent = new CreateAgent(...);
...........
dCreateAgent->dFollowAgent= new FollowAgent(...);
}
When my program dies, an error window occur. It consist of a message as "An unhandled exception of type 'System.AccessViolationException' occurred in TrafficSim.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Thanks your reply and I wait another
|
|
|
|
|
Well, you mostly only repeated what you sent in your first post so that doesn't bring us any further...anyways, i supose you access members thorough that dCreateAgent->dFollowAgent pointer, which points at some random memoy location or NULL if you initialized it, until you do dCreateAgent->dFollowAgent= new FollowAgent(...); , from which point it will point at a valid object and you can use it. Does this help?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
I'm installing that product on my vista SP1 with VS2008 SP1.
There is Missing Pre-requisite window appear.
Miscrosoft Platform SDK not found
Microsoft Platform SDK build 1830 or higher is required
to install Intel(R) C++ compiler for applications
running on IA-32, version 11.0.061.
Select one of the following options.
- Do no install Intel(R) C++ Compiler
- Continue installing and use Microsoft Platform SDK folder entered below
c:\program files\Microsoft SDKs\Windows\v6.0\
What does it mean?
Why platform SDK is needed for Intel Compiler?
Intel Compiler will be installed into different directory than SDK?
Чесноков
|
|
|
|
|
I would install a more recent (isn't yours the one that came with Visual Studio 6.0 ?) Windows SDK (it is available for free from Microsoft ) before installing the compiler.
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 articles]
|
|
|
|
|
The platform SDK contains the runtimes required to link object code on Windows systems into executables. Intel obviously designed their compiler to be compatible with the Windows libraries rather than try to write their own. You can get the latest SDK from Microsoft; see here[^] for more information.
It's time for a new signature.
|
|
|
|
|
Currenty Windows SDK is for 7 and .NET Framework 3.5.
I read they will ship a newer SDK after Visual Studio 2010, to support .NET 4.0 with Windows 7.
|
|
|
|
|
Sorry, but what has this to do with the my last message?
It's time for a new signature.
|
|
|
|
|
Please provide me step by step how to create a number of dialogs in a Console application
I also need to respond to the events in the dialogs as in MFC.
|
|
|
|
|
I don't get it. If you need dialogs, why don't you create a win32 application instead ?
|
|
|
|
|
I already have a console application in which I want to create my dialog. If I tried to create a dialog resource and then add a class to the dialog through class wizard as in MFC it gives me errors
due to #define "StdAfx.H" which is included in the new dialog class created through wizard. And also its not able to recogonize the MFC classes.
|
|
|
|
|
I believe he wants to mimic the logic of old DOS GUI.
|
|
|
|
|
Just as you normally would. What's the specific problem?
Steve
|
|
|
|
|
If I tried to create a dialog resource and then add a class to the dialog through class wizard as in MFC it gives me errors due to #define "StdAfx.H" which is included in the new dialog class created through wizard. And also its not able to recogonize the MFC classes and gives errors.
|
|
|
|
|
|
Which is the opposite of what the OP wanted, yes?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
|
|
I don't think Visual Studio is set up to do this. If you want to use MFC then you should either create an MFC Windows app that contains the dialogs, or an MFC dialog app with tabs or some similar mechanism. If you want to add dialogs to a console app then you will need to do it manually, by adding all the required Win32/MFC framework components.
It's time for a new signature.
|
|
|
|
|