l_Result = 1;}
}
else//文件不存在
{
l_Result = 1;
}
return l_Result;
}
设置系统语言 int SetLanguageThrd()
{
BOOL bXPLaterOS=FALSE;//是否为xp以后版本的os
OSVERSIONINFO ovs;
ovs.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (::GetVersionEx(&ovs))
{
bXPLaterOS = (ovs.dwPlatformId == VER_PLATFORM_WIN32_NT
&& ovs.dwMajorVersion >= 6
&& ovs.dwMinorVersion >= 0);//vista or later
}
else
{
return FALSE;
}
int lang = 0;
//char l_Language[10] = {0};
char *l_Language = ReadXmlFile(\"System\
if(stricmp(l_Language, \"en\") == 0)
lang = 1;
if(lang == 1)
{
if(bXPLaterOS)
SetThreadUILanguage(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US));
else
SetThreadLocale(MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_
DEFAULT));
}
else if(lang == 0)
{
if(bXPLaterOS)
SetThreadUILanguage(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED));
else
SetThreadLocale(MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIE
D),SORT_DEFAULT));
}
else
{
return FALSE;
}
return TRUE;
}
读写xml文件
char *ReadXmlFile(char *v_FatherNodeName, char *v_NodeName, char *v_Default)
{
char l_ComName[20] = {0};
char l_TempStr[20] = {0};
char l_XmlPath[256] = {0};
char l_ExePath[256] = {0};
GetModuleFileName(NULL, l_ExePath, 256);
int l_ExePathLen = strlen(l_ExePath);
for(int i = l_ExePathLen - 1; i >= 0; i--)
{
if(l_ExePath[i] == '\\\\')
{
memcpy(l_XmlPath, l_ExePath, i);
}
}
strcat(l_XmlPath, g_XmlFileName);
CTinyXMLLib l_TinyXml;
strcpy(l_TinyXml.m_XmlPath, l_XmlPath);
int l_Result = l_TinyXml.LoadXml();
if(l_Result == 0)
{
l_TinyXml.ReadXmlNode(v_FatherNodeName, v_NodeName, v_Default,
l_TempStr);
}
else
{
strcpy(l_TempStr, v_Default);
}
return l_TempStr;
}
char *ReadXmlFile(char *v_XmlFileName, char *v_FatherNodeName, char *v_NodeName, char *v_Default)
{
char l_XmlPath[256] = {0};
char l_TempStr[20] = {0};
char l_ExePath[256] = {0};
GetModuleFileName(NULL, l_ExePath, 256);
int l_ExePathLen = strlen(l_ExePath);
for(int i = l_ExePathLen - 1; i >= 0; i--)
{
if(l_ExePath[i] == '\\\\')
{
memcpy(l_XmlPath, l_ExePath, i);
}
}
strcat(l_XmlPath, \"\\\\\");
strcat(l_XmlPath, v_XmlFileName);
CTinyXMLLib l_TinyXml;
strcpy(l_TinyXml.m_XmlPath, l_XmlPath);
int l_Result = l_TinyXml.LoadXml();
if(l_Result == 0)
{
l_TinyXml.ReadXmlNode(v_FatherNodeName, v_NodeName, v_Default,
l_TempStr);
}
else
{
strcpy(l_TempStr, v_Default);
}
return l_TempStr;
}
int WriteXmlFile(char *v_XmlFileName, char *v_FatherNodeName, char *v_NodeName, char *v_Value)
{
char l_ComName[20] = {0};
char l_XmlPath[256] = {0};
char l_ExePath[256] = {0};
GetModuleFileName(NULL, l_ExePath, 256);
int l_ExePathLen = strlen(l_ExePath);
for(int i = l_ExePathLen - 1; i >= 0; i--)
{
if(l_ExePath[i] == '\\\\')
{
memcpy(l_XmlPath, l_ExePath, i);
}
}
strcat(l_XmlPath, \"\\\\\");
strcat(l_XmlPath, v_XmlFileName);
CTinyXMLLib l_TinyXml;
strcpy(l_TinyXml.m_XmlPath, l_XmlPath);
l_TinyXml.LoadXml();
l_TinyXml.WriteXmlNode(v_FatherNodeName, v_NodeName, v_Value);
return 0;
}
读ini文件
int CCommServerDlg::ReadIniFile()
{
char l_TempStr[2048] = {0};
char l_CurrentDir[256] = {0};
GetPrivateProfileString(\"Application\
\"http://172.18.1.170:9131/Download/FileList.txt\
l_TempStr, 2048, g_config_name);
GetPrivateProfileString(\"Application\
\"http://172.18.1.170:9131/Download/\
l_TempStr, 2048, g_config_name);
GetCurrentDirectory(256, l_CurrentDir);
strcat(l_CurrentDir, \"\\\\Download\");
CheckFolderExist(l_CurrentDir);
GetPrivateProfileString(\"Application\
l_TempStr, 2048, g_config_name);
GetPrivateProfileString(\"Application\
l_TempStr, 2048, g_config_name);
return 0;
} 读文件
void Read(char * v_Path, char *v_Buffer, const int v_BufferLen)
{
HANDLE hFile = ::CreateFile(v_Path, GENERIC_READ, 0, NULL, OPEN_EXISTING, NULL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
//SetFilePointer(hFile, -1, NULL, FILE_END);
SetFilePointer(hFile, 81920, NULL, FILE_BEGIN);
char l_Buffer[5] = {0};
DWORD nNumberOfBytesRead;
BOOL bRet = ::ReadFile(hFile, l_Buffer, v_BufferLen, &nNumberOfBytesRead, NULL);
memcpy(v_Buffer, l_Buffer, v_BufferLen);
// TODO ...
CloseHandle(hFile);
}
} 写文件
void Write(char * v_Path, char *v_Content, const int v_ContentSize)
{
HANDLE hFile = ::CreateFile(v_Path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, NULL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
//SetFilePointer(hFile, -1, NULL, FILE_END);
SetFilePointer(hFile, 81920, NULL, FILE_BEGIN);
DWORD nNumberOfBytesWritten;
::WriteFile(hFile, v_Content, v_ContentSize, &nNumberOfBytesWritten, NULL);
// TODO ...
CloseHandle(hFile);
}
}
查找进程 BOOL FindProcess()
{
int i=0;
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32);
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
i+=0;
}
BOOL bMore = ::Process32First(hProcessSnap, &pe32);
while(bMore)
{
//printf(\" 进程名称:%s \\n\
if(stricmp(\"RWPEPROC.exe\
{
//printf(\"进程运行中\");
i+=1;
}
bMore = ::Process32Next(hProcessSnap, &pe32);
}
if(i>1){ //大于1,排除自身
return true;
}else{
return false;
}
}
路径操作
char l_ExePath[256] = {0};
char l_MainExePath[256] = {0};
//GetCurrentDirectory(256, l_ExePath);
GetModuleFileName(NULL, l_ExePath, 256);
int l_ExePathLen = strlen(l_ExePath);
for(int i = l_ExePathLen - 1; i >= 0; i--)
{
if(l_ExePath[i] == '\\\\')
{
memcpy(l_MainExePath, l_ExePath, i);
}
}
strcat(l_MainExePath, \"\\\\RWPEPROC.exe\");
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
char *l_Buffer;
l_Buffer = _getcwd(NULL, 0);
_getcwd(DllPath, MAXPATH);
HMODULE module = GetModuleHandle(0);
char szBuff[MAX_PATH] = {0};
GetModuleFileName(module, szBuff, sizeof(szBuff));
int l_Pause = 0;
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
查找指定进程 ///判断指定进程是否存在
int IsProcExist(char FileName[50],bool IsDelete)
{
char NameBuff[MAX_PATH];
char *pFileName; // pFileName转化为大写后的字符串指针
char *pNameBuff; // NameBuff转化为大写后的字符串指针
BOOL bRes; // 返回结果
HINSTANCE hInstance; // 动态库句柄
HANDLE hHandle; // 进程句柄
int i, j; // 循环变量
int len;
funEnumPorcess EnumProcesses;
funGetModuleFileNameEx GetModuleFileNameEx;
DWORD *buffer, maxNum, factNum;
maxNum = MAXPROCESS;
factNum = MAXPROCESS;
buffer = (DWORD *)malloc(maxNum);
hInstance = LoadLibrary(\"psapi.dll\"); // 装载psapi.dll
if (hInstance)
{
EnumProcesses \"EnumProcesses\");
= (funEnumPorcess)GetProcAddress(hInstance,
if (EnumProcesses)
{
bRes = EnumProcesses(buffer, maxNum, &factNum); // 枚举进程,得到进程名称和实际进程总大小
if (bRes)
{
factNum /= sizeof(DWORD); // 计算实际进程个数
GetModuleFileNameEx
(funGetModuleFileNameEx)GetProcAddress(hInstance, \"GetModuleFileNameExA\");
=
if (GetModuleFileNameEx)
{
for (i=0; i{hHandle buffer[i]); // 获取进程句柄
= OpenProcess(PROCESS_ALL_ACCESS, FALSE,
if (hHandle)
{
bRes = GetModuleFileNameEx(hHandle, NULL, NameBuff, MAX_PATH); // 获取进程名称
if (bRes)
{
pFileName = strupr(FileName); // 字符串转化为大写
pNameBuff = strupr(NameBuff);
if // 名字查找 { } } // CloseHandle(hHandle);(strstr(pNameBuff, pFileName)
if(!IsDelete)
return 1;
else
{
TerminateProcess(hHandle,0);
}
关闭句柄
!= NULL)
}
}
}
}
}
// 释放动态库
FreeLibrary(hInstance);
}
free(buffer);
return 0;
}
//
//判断进程是否存在
BOOL FindProcess(char *FileName, bool v_IsKill)
{
int i=0;
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32);
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
i+=0;
}
BOOL bMore = ::Process32First(hProcessSnap, &pe32);
while(bMore)
{
if(stricmp(FileName, pe32.szExeFile)==0)
{
i+=1;
int l_ProcNums = 0;
if(v_IsKill) l_ProcNums = 1;
else l_ProcNums = 2;
if(i >= l_ProcNums)
{
HANDLE hHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE,
pe32.th32ProcessID); // 获取进程句柄
TerminateProcess(hHandle,0);
CloseHandle(hHandle);
}
}
bMore = ::Process32Next(hProcessSnap, &pe32);
}
if(i>=1){ //大于1,排除自身
return true;
}else{
return false;
}
}
写日志,删除日志
///目录是否存在的检查,不存在建立:
bool CheckFolderExist(char *v_StrPath)
{
WIN32_FIND_DATA l_Wfd;
bool l_Result = false;
HANDLE hFind = FindFirstFile(v_StrPath, &l_Wfd);
if ((hFind != INVALID_HANDLE_VALUE) && (l_Wfd.dwFileAttributes FILE_ATTRIBUTE_DIRECTORY))
{
l_Result = true;
}
else
{
if(CreateDirectory(v_StrPath, NULL))
{
l_Result = false;
& }
}
FindClose(hFind);
return l_Result;
}
//遍历目录中所有的文件
BOOL DirectoryList(char *v_Path)
{
WIN32_FIND_DATA l_WFindData;
HANDLE hFind;
char l_FilePathName[128]={0};
// 构造路径
char l_FullPathName[128]={0};
bool l_SearchFinished=false;
strcpy(l_FilePathName, v_Path);
strcat(l_FilePathName, \"\\\\*.*\");
hFind = FindFirstFile(l_FilePathName, &l_WFindData);
if (hFind == INVALID_HANDLE_VALUE)
{
//printf(\"搜索失败!\");
return 0;
}
while(::FindNextFile(hFind, &l_WFindData))
{
sprintf(l_FullPathName, \"%s\\\\%s\ DeleteFile(l_FullPathName);
}
FindClose(hFind);
::RemoveDirectory(v_Path);
return 0;
}
int DeleteLogDir()
{
int l_Result=0;
time_t t=time(0);
for(int i = 5; i <= 13; i++)
{
time_t l_DeleteDate = t - i*24*60*60;//三天前
char l_DeleteDirName[64] = {0};
strftime(l_DeleteDirName, sizeof(l_DeleteDirName), \"%y%m%dServiceLogs\
localtime(&l_DeleteDate));
char l_DeleteDirPath[256] = {0};
strcpy(l_DeleteDirPath, logpath);
strcat(l_DeleteDirPath, \"Logs\\\\\");
strcat(l_DeleteDirPath, l_DeleteDirName);
DirectoryList(l_DeleteDirPath);
}
return l_Result;
}
bool LocalWriteLog(char * v_FileName , char * v_Module , const char * v_LogInfo){
time_t t = time(0);
char l_FileName[64] = {0};
char l_DirName[256] = {0};
strcpy(l_DirName, logpath);
strcat(l_DirName, \"Logs\");
char l_Temp[3];
CheckFolderExist(l_DirName);
char l_DirYMD[32] = {0};
strftime(l_DirYMD, sizeof(l_DirYMD), \"%y%m%dServiceLogs\
strcat(l_DirName, \"\\\\\");
strcat(l_DirName, l_DirYMD);
CheckFolderExist(l_DirName);
//strftime( l_FileName, sizeof(l_FileName), \"%Y-%m-%d-\
strcat(l_FileName , v_FileName);
if(v_FileName != NULL || v_Module != NULL || v_LogInfo != NULL){
fstream l_FileHandle;
strcat(l_DirName, \"\\\\\");
strcat(l_DirName, l_FileName);
l_FileHandle.open(l_DirName ,ios::app);
if(!l_FileHandle){
char l_Buffer[50] = {0};
memcpy(l_Buffer , l_FileName , strlen(l_FileName));
memcpy(l_Buffer + strlen(l_FileName) , \"Open Log Error\" , 20);
// MessageBox(NULL , l_Buffer , \"error\" , MB_OK);
}
time_t t = time(0);
char tmp[64];
strftime( tmp, sizeof(tmp), \"%d %X\
l_FileHandle << \"TIME :\"<v_LogInfo<l_FileHandle.close();return true;
}
return false;
}
启动程序(以system权限启动显示界面) //for win7
DWORD _stdcall LaunchAppIntoDifferentSession( LPTSTR lpCommand )
{
DWORD dwRet = 0;
PROCESS_INFORMATION pi ;
STARTUPINFO si ;
DWORD dwSessionId ;
HANDLE hUserToken = NULL;
HANDLE hUserTokenDup = NULL;
HANDLE hPToken = NULL;
HANDLE hProcess = NULL;
DWORD dwCreationFlags ;
HMODULE hInstKernel32 = NULL;
typedef DWORD (WINAPI * WTSGetActiveConsoleSessionIdPROC)();
WTSGetActiveConsoleSessionIdPROC WTSGetActiveConsoleSessionId = NULL;
hInstKernel32 = LoadLibrary (\"Kernel32.dll\" );
if (!hInstKernel32 )
{
return FALSE ;
}
OutputDebugString(\"LaunchAppIntoDifferentSession 1\\n\" );
WTSGetActiveConsoleSessionId =
(WTSGetActiveConsoleSessionIdPROC )GetProcAddress( hInstKernel32,\"WTSGetActiveConsoleSessionId\" );
// Log the client on to the local computer.
dwSessionId = WTSGetActiveConsoleSessionId ();
do
{
WTSQueryUserToken( dwSessionId ,&hUserToken );
dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE;
ZeroMemory( &si , sizeof( STARTUPINFO ) );
si.cb = sizeof( STARTUPINFO );
si.lpDesktop = \"winsta0\\\\default\" ;
ZeroMemory( &pi , sizeof( pi) );
TOKEN_PRIVILEGES tp ;
LUID luid ;
if( !::OpenProcessToken ( GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY
| TOKEN_ADJUST_SESSIONID
TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY |
| TOKEN_READ | TOKEN_WRITE , &hPToken ) )
{
dwRet = GetLastError ();
break;
}
else;
if ( !LookupPrivilegeValue ( NULL, SE_DEBUG_NAME, &luid ) )
{
dwRet = GetLastError ();
break;
}
else;
tp.PrivilegeCount =1;
tp.Privileges [0].Luid = luid;
tp.Privileges [0].Attributes = SE_PRIVILEGE_ENABLED;
if( !DuplicateTokenEx ( hPToken, MAXIMUM_ALLOWED, SecurityIdentification , TokenPrimary, & hUserTokenDup ) )
{
dwRet = GetLastError ();
break;
}
else;
NULL , //Adjust Token privilege
if( !SetTokenInformation ( hUserTokenDup,TokenSessionId ,(void*)& dwSessionId,sizeof (DWORD) ) )
{
dwRet = GetLastError ();
break;
}
else;
if( !AdjustTokenPrivileges ( hUserTokenDup, FALSE, &tp sizeof(TOKEN_PRIVILEGES ), (PTOKEN_PRIVILEGES) NULL, NULL ) )
{
dwRet = GetLastError ();
break;
}
else;
LPVOID pEnv =NULL;
if( CreateEnvironmentBlock ( &pEnv, hUserTokenDup, TRUE ) )
, {
dwCreationFlags|=CREATE_UNICODE_ENVIRONMENT ;
}
else pEnv =NULL;
// Launch the process in the client's logon session.
if( CreateProcessAsUser ( hUserTokenDup, // client's access token
NULL, // file to execute
lpCommand, // command line
NULL, // pointer to process SECURITY_ATTRIBUTES
NULL, // pointer to thread SECURITY_ATTRIBUTES
FALSE, // handles are not inheritable
dwCreationFlags,// creation flags
pEnv, // pointer to new environment block
NULL, // name of current directory
& si, // pointer to STARTUPINFO structure
& pi // receives information about new process
) )
{
}
else
{
dwRet = GetLastError ();
break;
}
}
while( 0 );
//Perform All the Close Handles task
if( NULL != hUserToken )
{
CloseHandle( hUserToken );
}
else;
if( NULL != hUserTokenDup)
{
CloseHandle( hUserTokenDup );
}
else;
if( NULL != hPToken )
{
CloseHandle( hPToken );
}
else;
return dwRet ;
}
建立取消磁盘映射连接
int __stdcall CreateConn(char *v_LocalName, char *v_RemoteName, char *v_PSW, char *v_UserName, char *v_ReturnBuffer)
{
int l_Result = 0;
try
{
char l_ReturnBuffer[256] = {0};
DWORD dwRetVal;
NETRESOURCE nr;
DWORD dwFlags;
memset(&nr, 0, sizeof (NETRESOURCE));
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = v_LocalName;
//nr.lpRemoteName = \"\\\\\\\\LYLTEST\\\\ShareFile\";
nr.lpRemoteName = v_RemoteName;
nr.lpProvider = NULL;
dwFlags = CONNECT_UPDATE_PROFILE;
dwRetVal = WNetAddConnection2(&nr, v_PSW, v_UserName, dwFlags); if (dwRetVal == NO_ERROR)
{
l_Result = 0;
strcpy(l_ReturnBuffer, \"Success\");
LocalWriteLog(LOG_RUNFILE, \"CreateConn||WNetAddConnection2\
l_ReturnBuffer);
//printf(\"Connection added to %s\\n\
}
else
{
l_Result = -1;
GetLastErrMsg(l_ReturnBuffer);
LocalWriteLog(LOG_ERRFILE, \"CreateConn||WNetAddConnection2\
l_ReturnBuffer);
}
strcpy(v_ReturnBuffer, l_ReturnBuffer);
}
catch(exception ex)
{
l_Result = -2;
strcpy(v_ReturnBuffer, ex.what());
}
return l_Result;
}
int __stdcall CancelConn(char *v_LocalName, char *v_ReturnBuffer)
{
int l_Result = 0;
try
{
char l_ReturnBuffer[256] = {0};
DWORD dwResult;
// Call the WNetCancelConnection2 function, specifying
// that the connection should no longer be a persistent one.
//
dwResult = WNetCancelConnection2(v_LocalName,
CONNECT_UPDATE_PROFILE, // remove connection from profile
FALSE); // fail if open files or jobs
// Process errors.
// The device is not a local redirected device.
//
if(dwResult == NO_ERROR)
{
l_Result = 0;
strcpy(l_ReturnBuffer, \"Success\");
LocalWriteLog(LOG_RUNFILE, \"CancelConn||WNetAddConnection2\
l_ReturnBuffer);
}
else
{
l_Result = -1;
GetLastErrMsg(l_ReturnBuffer);
LocalWriteLog(LOG_ERRFILE, \"CancelConn||WNetAddConnection2\
l_ReturnBuffer);
}
strcpy(v_ReturnBuffer, l_ReturnBuffer);
}
catch(exception ex)
{
l_Result = -2;
strcpy(v_ReturnBuffer, ex.what()); }
return l_Result;
}