20140629:简单的打印函数执行日志的函数代码WriteLog函数(适用于c++builder等,未加载lib文件)

94 篇文章 0 订阅
37 篇文章 0 订阅
#include<windows.h>
#include<FSTREAM>
#include<stdio.h>
#include<tchar.h>
#include<process.h>
using namespace std;
char pLogPath[1000];
char pCurrentDirectory[1000];

int WriteLog(const char *format, ... );
bool MakeGlobalPath();
bool execwait(char *cmdline,char *cmd,int timeout);

 int WriteLog(const char *format, ... )  //if succeeds,return 0.else return 1.
{    
    MakeGlobalPath();
    strcat(pLogPath,"SQLSelect.txt");      
    FILE    *fStream = fopen(pLogPath,"a+");    
    if (fStream != NULL)    
    {       
        try{
            va_list  ap;    
            TCHAR    strTemp[5120];    
            va_start(ap,format);    
            vsprintf(strTemp,format,ap);    
            fprintf(fStream,"%s\n",strTemp);
            va_end(ap);    
        }    
        catch (...)    
        {    
        }    
        fclose(fStream);    
        fStream = NULL;    
        return 0;    
    }    
    else    
    {    
        return 1;    
    }    
    
}    

bool MakeGlobalPath()
{      
    char    lpModuleFilePath[1000];      
    DWORD   dwModuleFilePath = 0;
    DWORD   iIndex = 0;      
    DWORD   iPosition = 0;      
    

    memset(lpModuleFilePath,0,sizeof(char)*1000);      
    memset(pCurrentDirectory,0,sizeof(char)*1000);      
    
    
    dwModuleFilePath = GetModuleFileName(NULL,lpModuleFilePath,1000);  //get the exe program's full path name    

    
    for (;iIndex<dwModuleFilePath;iIndex++)      
    {      
        if (lpModuleFilePath[iIndex]=='\\')      
        {      
            iPosition = iIndex;                                         //get the last blackslash    
        }       
    }
    strncpy(pCurrentDirectory,lpModuleFilePath,iPosition+1);      
    pCurrentDirectory[iPosition+1] = '\0';                              //null    
    strcpy(pLogPath,pCurrentDirectory);      
    
    
    //cout<<"pCurrentDirectory"<<pCurrentDirectory<<endl<<"pLogPath"<<pLogPath<<endl;
    
    return true;      
}

 
bool execwait(char *cmdline,char *cmd,int timeout)  
{  
    PROCESS_INFORMATION   pi;  
    STARTUPINFO si;  
    ZeroMemory( &pi, sizeof(pi) );  
    ZeroMemory( &si, sizeof(si) );  
  
    si.cb = sizeof(si);  
    si.dwFlags   =   STARTF_USESHOWWINDOW;   
    si.wShowWindow   =   SW_HIDE;  
    CreateProcess(cmdline,cmd,NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);  
    WaitForSingleObject(pi.hProcess ,timeout);  
    //Edit1->Text = "ddd";  
    CloseHandle( pi.hProcess );  
    CloseHandle( pi.hThread );  
    return true;  
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值