读书人

历程工作目录的继承

发布时间: 2012-11-03 10:57:44 作者: rapoo

进程工作目录的继承

关于进程工作目录的继承

尤其是进程需要读取资源文件的时候,

或者更特殊的时候,会用到工作目录的设置问题,

下面代码,仅仅帮助理解进程间工作目录继承关系,希望对大家有用

// tt.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include "afxinet.h"#include <Windows.h>#include <ShellAPI.h>using namespace std;#define BUFSIZE MAX_PATHint _tmain(int argc, _TCHAR* argv[]){///*************//关于进程工作目录的继承//**************/wstring strExePath=L"C:\\Program Files\\111.exe";wstring strParamete=L"W";ShellExecute(NULL, _T("open"), strExePath.c_str(), strParamete.c_str(), NULL, SW_SHOWNORMAL);TCHAR Buffer[BUFSIZE];DWORD dwRet;dwRet = GetCurrentDirectory(BUFSIZE, Buffer); SetCurrentDirectory(L"C:\\");dwRet = GetCurrentDirectory(BUFSIZE, Buffer);wstring strExePathC=L"C:\\Program Files\\111.exe";wstring strParameteC=L"G";ShellExecute(NULL, _T("open"), strExePathC.c_str(), strParameteC.c_str(), NULL, SW_SHOWNORMAL);if( dwRet == 0 )    //返回零表示得到文件的当前路径失败,所谓当前路径就是.dsw所在的文件夹{_tprintf(TEXT("GetCurrentDirectory failed (%d)n"),  GetLastError());return 0;}if(dwRet > BUFSIZE) //如果超过了最大字符数,则提示越界,MAX_PATH 是260{_tprintf(TEXT("GetCurrentDirectory failed (buffer too small,need %d chars)/n"), dwRet);return 0;} if( !SetCurrentDirectory(argv[1])){_tprintf(TEXT("SetCurrentDirectory failed (%d)n"), GetLastError());return 0;}_tprintf(TEXT("Set current directory to %sn"), argv[1]);if( !SetCurrentDirectory(Buffer) ){_tprintf(TEXT("SetCurrentDirectory failed (%d)n"), GetLastError());return 0;}_tprintf(TEXT("Restored previous directory (%s).n"), Buffer);return 1;     }


读书人网 >编程

热点推荐