#include "stdafx.h"
#include "afxinet.h"
#include "HeaderCode.h"
//--------------------------------------------------------------------------------------------------
// 以下函数为整个工程公用的函数,禁止增加其他不相关或者定制函数
//--------------------------------------------------------------------------------------------------
CString g_AppPath =L"";
CString GetMyAppPath()
{
if (g_AppPath.IsEmpty())
{
// Get the path.
CString apppath;
TCHAR szPath[_MAX_PATH*2];
::GetModuleFileName(AfxGetInstanceHandle(), szPath, MAX_PATH*2);
DWORD dwLen = GetLongPathName(szPath, NULL, 0);
GetLongPathName(szPath, apppath.GetBuffer(dwLen + 1), dwLen + 1);
apppath.ReleaseBuffer();
int iPos = apppath.ReverseFind(_T('\\'));
if (-1 != iPos)
{
apppath = apppath.Left(iPos + 1);
}
g_AppPath = apppath;
}
return g_AppPath;
}
std::string UnicodeToAnsi(const CString& strWide)
{
std::string strANSI = "";
int iLen = ::WideCharToMultiByte(CP_ACP, 0, strWide, -1, NULL, 0, NULL, NULL);
if (iLen > 1)
{
char *pBuf = new char[iLen+1];
if (pBuf)
{
::WideCharToMultiByte(CP_ACP, 0, strWide, -1, pBuf, iLen, NULL, NULL);
strANSI = pBuf;
delete []pBuf;
}
}
return strANSI;
}
std::string UnicodeToUTF8(const CString& strWide)
{
std::string strANSI = "";
int iLen = ::WideCharToMultiByte(CP_UTF8, 0, strWide, -1, NULL, 0, NULL, NULL);
if (iLen > 1)
{
char *pBuf = new char[iLen+1];
if (pBuf)
{
::WideCharToMultiByte(CP_UTF8, 0, strWide, -1, pBuf, iLen, NULL, NULL);
strANSI = pBuf;
delete []pBuf;
}
}
return strANSI;
}
//----------------------------------------------------------------------------
int CountDirectory(CString path)
{
int count = 0;
CFileFind finder;
BOOL working = finder.FindFile(path + "\\*.*");
while (working)
{
working = finder.FindNextFile();
if (finder.IsDots())
continue;
if (!finder.IsDirectory())
count++;
}
return count;
}
int DeleteFiles(CString path)
{
int count = 0;
CFileFind finder;
BOOL working = finder.FindFile(path + "\\*.*");
while (working)
{
working = finder.FindNextFile();
if (finder.IsDots())
continue;
if (!finder.IsDirectory())
{
CString strDir = finder.GetFilePath();
DeleteFile(finder.GetFilePath());
}
}
return count;
}
//----------------------------------------------------------
// 以下是监控软件函数
//----------------------------------------------------------
void KillMonitor(CString sAppName)
{
HWND hWnd = ::FindWindow(NULL,sAppName);//_T("PMonitor"));
DWORD nProcessID = 0;
::GetWindowThreadProcessId( hWnd, &nProcessID );
HANDLE nHandle = ::OpenProcess( PROCESS_ALL_ACCESS|PROCESS_TERMINATE, FALSE,nProcessID );
::PostMessage(hWnd,WM_CLOSE,0,0);
if (nHandle)
{
::TerminateProcess(nHandle,1);
}
return ;
}
void StartMonitor(CString sAppName)
{
CString tmpfile = GetMyAppPath() + sAppName;
#if 0
#ifdef _DEBUG
return;
tmpfile +=_T("PMonitord.exe");
#else
tmpfile += _T("PMonitor.exe");
#endif
#endif
SHELLEXECUTEINFO sei;
ZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.lpFile = tmpfile;
sei.nShow = SW_HIDE;
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb = _T("open");
if (!ShellExecuteEx(&sei))//执行成功
{
Sleep(300);
ShellExecuteEx(&sei);
}
return;
}
//----------------------------------------------------------
void MakeFileData(CString &sHeadData,CString &sPreData,CString &sPostData)//发送请求包
{
CString sBoundary = _T("-----46as7d----");
CString sType = _T("Content-Type: multipart/form-data; boundary=%s\r\n");
sHeadData.Format(sType, sBoundary);
CString strFormat;
CString strData;
strFormat += _T("--%s");
strFormat += _T("\r\n");
strFormat += _T("Content-Disposition: form-data; name=\"trackdata\"; filename=\"%s\"");//文件地址信息
strFormat += _T("\r\n");
strFormat += _T("Content-Type: application/x-dwg");
strFormat += _T("\r\n\r\n");
strData.Format(strFormat, sBoundary, L"Mx_Dwg");//
sPreData = strData;
strFormat = _T("\r\n");
strFormat += _T("--%s");
strFormat += _T("\r\n");
strFormat += _T("Content-Disposition: form-data; name=\"submitted\"");
strFormat += _T("\r\n\r\n");
strFormat += _T("hello");
strFormat += _T("\r\n");
strFormat += _T("--%s--");
strFormat += _T("\r\n");
strData.Format(strFormat, sBoundary, sBoundary);
sPostData = strData;
return ;
}
#define MAX_MEM_SIZE 4096
// 使用方式
//
//{
// UploadDwgFile(L"http://51.14.64.2:88/DynaMineGZ/Home/SaveDwg?filename=10001_admin_E5C1F97F-A103-4795-AF92-6ED9E7AED7EA_20190304153128853.dwg&filetype=1",L"d:\\12a.dwg");
//}
BOOL UploadDwgFile(CString szRemoteUrl,CString filepath,CString &strRespose)
{
CFile file;
CInternetSession session;
CHttpConnection* conn = NULL;
CHttpFile* pHttpFile;
BOOL bRet = FALSE;
if(!file.Open(filepath, CFile::modeRead | CFile::shareDenyWrite))
{
strRespose=L"打开文件失败";
//AfxMessageBox(_T("文件打开失败!"));
return FALSE;
}
try
{
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,3000);
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES,1);
DWORD dwType = 0;
CString sServer,sParam;
INTERNET_PORT wPort = 0;
if(!AfxParseURL(szRemoteUrl, dwType, sServer, sParam, wPort))
return FALSE;
CString sPreData,sPostData,sHeadData;
MakeFileData(sHeadData,sPreData,sPostData);
DWORD totallength = file.GetLength()+sPostData.GetLength()+sPreData.GetLength();
conn = session.GetHttpConnection(sServer,wPort);
pHttpFile=conn->OpenRequest(CHttpConnection::HTTP_VERB_POST,sParam);
pHttpFile->AddRequestHeaders(sHeadData);
pHttpFile->SendRequestEx(totallength, HSR_SYNC | HSR_INITIATE);
std::string tmpstr1 = UnicodeToAnsi(sPreData);
pHttpFile->Write(tmpstr1.c_str(), tmpstr1.size());
char *pBuf=new char[MAX_MEM_SIZE+1];
if (pBuf)
{
DWORD nLen = 0;
do
{
nLen=file.Read(pBuf,MAX_MEM_SIZE);
if(nLen > 0)
{
pHttpFile->Write(pBuf,nLen);
}
}while (nLen > 0);
delete pBuf;
}
tmpstr1 = UnicodeToAnsi(sPostData);
pHttpFile->Write(tmpstr1.c_str(), tmpstr1.size());
pHttpFile->EndRequest(HSR_SYNC);
DWORD dwStateCode = 0;
pHttpFile->QueryInfoStatusCode(dwStateCode);
if(dwStateCode== HTTP_STATUS_CREATED || dwStateCode == HTTP_STATUS_OK)
{
bRet= TRUE;
}
}
catch (CException* e)
{
TCHAR strerror[MAX_PATH]=L"";
e->GetErrorMessage(strerror, MAX_PATH);
e->Delete();
strRespose = strerror;
}
if (pHttpFile)
{
pHttpFile->Close();
delete pHttpFile;
}
session.Close();
file.Close();
return bRet;
}