如何在取得程序运行时所在的路径

5 篇文章 0 订阅
2 篇文章 0 订阅
 在网上看到了一篇文章,大意是这样的:

TCHAR exeFullPath[_MAX_PATH];

char szDrive[_MAX_DRIVE]={0}, szDir[_MAX_DIR]={0}, szFNAME[_MAX_FNAME]={0}, szExt[_MAX_EXT]={0};

GetModuleFileName(AfxGetInstanceHandle(), exeFullPath, sizeof(exeFullPath));

_splitpath (exeFullPath, szDrive, szDir, NULL, NULL);

m_strWorkDIR.Format("%s%s", szDrive, szDir);

GetModuleFileName可以取得程序运行时的路径和文件名,具体的帮助请查看MSDN.但是在WinCE(PocketPC,SmartPhone)上没有找到_makepath,_splitpath ,着两个函数在Windows SDK的stdlib.h中.没办法,自己写了一个函数,取得程序的运行是的路径,代码如下,仅功参考:

/*
Parameters
    hModule :
        [in] Handle to the module whose executable file name is being requested.
        If this parameter is NULL, GetModuleFileName returns the path for the file used to create the calling process.

    lpFilename :
        [out] Pointer to a buffer that is filled in with the path and file name of the module.
    nSize :
        [in] Specifies the length, in characters, of the lpFilename buffer.
        If the length of the path and file name exceeds this limit, the string is truncated.

Return Values
    The length, in characters, of the string copied to the buffer indicates success.

    Zero indicates failure.

    To get extended error information, call GetLastError.
*/
DWORD GetAppPath(
            HMODULE hModule,
            LPWSTR lpFilename,
            DWORD nSize
            )
{
    DWORD result;
    result = GetModuleFileName( NULL,lpFilename,nSize);
    if (0==result){
        return result;
    }

    LPWSTR pc;
    pc = lpFilename + wcslen(lpFilename);
    while('//'!=*pc){
        *pc=0;
        pc--;
    }
}

转载请注明出处(http://blog.csdn.net/enjoyo)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值