C++ sets and gets the implementation code for the current work path

  • 2020-05-30 20:47:24
  • OfStack

Usually, you call DLL in the service program, and DLL will load a lot of configuration and files. In general, DLL will not be able to load the configuration and files. The reason is that after your service program is loaded, the path is not the directory where your program is located, so DLL is not. The solution is to design the current work path in the DLL path or service program.

The main functions are: SetCurrentDirectory;

The example of setting the current working path is as follows:


#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
 char buf[1000];
 int i=1000;
 GetCurrentDirectory(1000,buf); // Get the current work path 
 cout<<buf<<endl;
 char strModule[256];
 GetModuleFileName(NULL,strModule, 256); // Gets the current module path 
 cout<<strModule<<endl;
 string a;
 a.assign(buf);
 cout<<a<<endl;
 a.append("//..//");   // Set the current work path to be on at that time 1 level 
 //a=a+"..//";
 SetCurrentDirectory(a.c_str()); // Set up the 
 GetCurrentDirectory(1000,buf);
 cout<<buf<<endl;
 return 0;
}

Related articles: