c++常用自定义宏

#pragma once

//help macro
#ifndef SAFE_DELETE
#define SAFE_DELETE(p) if(p){ delete p; p = NULL; }
#endif
//
#ifndef SAFE_DELETE_ARY
#define SAFE_DELETE_ARY(p) if(p){ delete []p; p = NULL; }
#endif

#ifndef CLR_ARY
#define CLR_ARY( t, s, n ) memset( &n, 0, sizeof(t)*s );
#endif
#ifndef CLR_PTR
#define CLR_PTR( t, s, p ) memset( p, 0, s*sizeof(t) );
#endif

#ifndef DEC_CLR_ARY
#define DEC_CLR_ARY(t, s, n ) t n[s]; CLR_ARY( t, s, n );
#endif

#ifndef DEC_CLR_CHR_ARY
#define DEC_CLR_CHR_ARY( n, size ) DEC_CLR_ARY( TCHAR, size, n )
#endif

#ifndef CLR_CHR_ARY
#define CLR_CHR_ARY( n, s ) CLR_ARY( TCHAR, n, s )
#endif

#ifndef DEC_CLR_PTR_ARY
#define DEC_CLR_PTR_ARY( t, size, n ) \
t* n = new t[size]; \
if(n){ CLR_PTR( t, size, n ) }
#endif

#ifndef DEC_CLR_CHR_PTR_ARY
#define DEC_CLR_CHR_PTR_ARY( n, s ) DEC_CLR_PTR_ARY( TCHAR, s, n )
#endif

#ifndef CLR_SCT
#define CLR_SCT( t, n ) memset( &n, 0, sizeof(t) );
#endif

#ifndef CLR_SCT_PTR
#define CLR_SCT_PTR( t, p ) memset( p, 0, sizeof(t) );
#endif

#ifndef DEC_CLR_SCT
#define DEC_CLR_SCT( t, n ) t n; CLR_SCT( t, n);
#endif

#ifndef GET_NEW_CHRARY_SIZE
#define GET_NEW_CHRARY_SIZE( n ) (_tcslen(n)+1)*sizeof(TCHAR)
#endif

#ifdef _AFXDLL
//
// function:GetAppExePath
// desc: get exe path
// return: CString
inline CString GetAppExePath(void)
{
DEC_CLR_CHR_ARY( cp, MAX_PATH );
GetModuleFileName( NULL, cp, MAX_PATH );

return CString(cp);
}
//

//
// function GetCurExePath
// desc: get cur execute file path
// return: CString
inline CString GetCurExePath(void)
{
DEC_CLR_CHR_ARY( cp, MAX_PATH );
GetModuleFileName( NULL, cp, MAX_PATH );

while( cp[ _tcslen( cp )-1 ] != TEXT('\\') )
{
   cp[ _tcslen( cp ) - 1 ] = TEXT('\0');
}

return CString( cp );
}
#endif

inline BOOL GetAppExePath( LPTSTR p)
{
if(!GetModuleFileName( NULL, p, MAX_PATH ) )
{
   return FALSE;
}else{
   return TRUE;
}
}
//
inline BOOL GetCurAppPath( LPTSTR p)
{
if(!GetModuleFileName( NULL, p, MAX_PATH ) )
{
   return FALSE;
}else{
   while( p[_tcslen(p)-1] != TEXT('\\') )
   {
    p[_tcslen(p)-1] = TEXT('\0');
   }
   return TRUE;
}
}
//
inline BOOL RunExtPath( LPCTSTR p, bool showwindow = true, bool bInner = true, bool useParam = false, bool fpath = true)
{
if( NULL == p ){ return FALSE; }

STARTUPINFO si;
PROCESS_INFORMATION pi;
BOOL bOK = FALSE;

CLR_SCT( STARTUPINFO, si );

si.cb   = sizeof( STARTUPINFO );
si.dwFlags   = STARTF_USESHOWWINDOW;
si.wShowWindow = showwindow?SW_SHOW:SW_HIDE;
if( !showwindow )
{
   si.hStdError = stderr;
   si.hStdInput = stdin;
   si.hStdOutput = stdout;
}
DEC_CLR_CHR_ARY( np, MAX_PATH );
if( !fpath )
{
   if( GetCurAppPath( np ) )
   {
    _tcscat_s( np, MAX_PATH, p );
   }
}else{
   _tcscpy_s( np, MAX_PATH, p );
}
bOK = ::CreateProcess(
   (useParam )?NULL:(np), //NULL,
   (useParam )?(np):NULL, //(np),
   NULL,
   NULL,
   FALSE,
   0,
   NULL,
   NULL,
   &si,
   &pi);

CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );

return bOK;
}
//
inline bool GetExeAppName( LPTSTR s )
{
bool bOK = true;
DEC_CLR_CHR_ARY( nn, MAX_PATH );

if( GetAppExePath( nn ) ){
   while( nn[_tcslen(nn)-1] != _T('\\') )
   {
    s[_tcslen(s)] = nn[_tcslen(nn)-1];
    nn[_tcslen(nn)-1] = _T('\0');
  
   }
   _tcsrev(s);
}else{
   bOK = false;
}
return bOK;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值