0
点赞
收藏
分享

微信扫一扫

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;
 }



举报

相关推荐

0 条评论