C++ 设置和获取当前工作路径的实现代码
时间:2020-11-14 11:50:27|栏目:C代码|点击: 次
通常,你在服务程序中调用DLL,而DLL又会加载许多配置和文件,一般会出现DLL加载不到配置和文件,原因是你的服务程序被加载后,路径并不是你程序的所在目录,故DLL也不是,因此加载不了。解决办法,是在DLL的路径或服务程序中设计当前的工作路径。
主要函数为:SetCurrentDirectory;
设置当前工作路径实例如下:
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
char buf[1000];
int i=1000;
GetCurrentDirectory(1000,buf); //得到当前工作路径
cout<<buf<<endl;
char strModule[256];
GetModuleFileName(NULL,strModule, 256); //得到当前模块路径
cout<<strModule<<endl;
string a;
a.assign(buf);
cout<<a<<endl;
a.append("//..//"); //设置为当前工作路径为当时的上一级
//a=a+"..//";
SetCurrentDirectory(a.c_str()); //设置
GetCurrentDirectory(1000,buf);
cout<<buf<<endl;
return 0;
}
上一篇:MFC实现字幕滚动效果
栏 目:C代码
下一篇:关于C++的强制类型转换浅析
本文标题:C++ 设置和获取当前工作路径的实现代码
本文地址:http://www.codeinn.net/misctech/22411.html






