以编程方式修改/创建vcproj文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了以编程方式修改/创建vcproj文件相关的知识,希望对你有一定的参考价值。

我有一个visual c++项目文件(vcproj),我想以编程方式修改它,以添加其他包含目录或链接库。

一种解决方案可能是将vcproj解析为XML文件并进行修改。有没有其他API可以让这更容易?

答案

下面是我编写的一些代码,用于从Visual Studio解决方案(.sln)中获取它包含的所有项目,并从每个项目中获取它包含的所有文件(包括完整路径)。

static strList parseSolution( const char * solName  )
{
  strList result;
  static char drive[_MAX_DRIVE];
  static char somepath[_MAX_PATH];
  static char buffer[_MAX_PATH];
  static char path[_MAX_PATH];
  static char ext[_MAX_EXT];

  _splitpath( solName, drive, somepath, buffer, ext );

  FILE * f = fopen( solName, "r" );
  if( NULL == f )
  {
    printf("ERROR: Solution %s is missing or unavailable.
", solName );
    exit(1);
  }

  while( !feof(f) )
  {
    char * res = fgets( buffer, sizeof(buffer), f );
    if( NULL == res )
      continue;

    if( NULL != strstr(buffer, "Project(") )
    {
      char * ptrName = strchr( buffer, '=' );
      char * ptrFile = strchr( ptrName, ',' );
      *ptrFile++ = 0;
      char * ptrEnd  = strchr( ptrFile, ',' );
      *ptrEnd++  = 0;

      while( ('=' == *ptrName) 
           ||(' ' == *ptrName)
           ||('"' == *ptrName) ) ptrName++;
      if( '"' == ptrName[strlen(ptrName)-1] )
        ptrName[strlen(ptrName)-1] = 0;

      while( (' ' == *ptrFile)
           ||('"' == *ptrFile) ) ptrFile++;
      if( '"' == ptrFile[strlen(ptrFile)-1] )
        ptrFile[strlen(ptrFile)-1] = 0;

      _makepath( path, drive, somepath, ptrFile, NULL );

      result.push_back( std::string(path) );
    }
  }

  fclose(f);

  return result;
}

/**
 * Parse project and extract fullpath source filename from project.
 */
static strList parseProject( const char * projName  )
{
  strList result;
  static char drive[_MAX_DRIVE];
  static char somepath[_MAX_PATH];
  static char buffer[_MAX_PATH];
  static char path[_MAX_PATH];
  static char ext[_MAX_EXT];

  _splitpath( projName, drive, somepath, buffer, ext );

  FILE * f = fopen( projName, "r" );
  if( NULL == f )
  {
    printf("ERROR: Project %s is missing or unavailable.
", projName );
    exit(1);
  }

  while( !feof(f) )
  {
    char * res = fgets( buffer, sizeof(buffer), f );
    if( NULL == res )
      continue;

    if( (NULL != strstr(buffer, "<ClInclude Include="))
      ||(NULL != strstr(buffer, "<ClCompile Include=")) )
    {
      char * ptrName = strchr( buffer, '=' );
      char * ptrName1 = strstr( buffer, "/>" );
      if( NULL != ptrName1 ) *ptrName1 = 0;

      while( ('=' == *ptrName) 
           ||(' ' == *ptrName)
           ||('"' == *ptrName) ) ptrName++;
      while( ('"' == ptrName[strlen(ptrName)-1])
           ||(' ' == ptrName[strlen(ptrName)-1])
           ||('
' == ptrName[strlen(ptrName)-1]))
        ptrName[strlen(ptrName)-1] = 0;

      _makepath( path, drive, somepath, ptrName, NULL );
      result.push_back( std::string(path) );
    }
  }

  fclose(f);

  return result;
}

/**
 * Recoding source file.
 */

使用这些函数,您可以处理每个文件,或者自己处理项目。

  strList projectList = parseSolution( argv[1] );
  strList::iterator itProj = projectList.begin();
  while( itProj != projectList.end() )
  {
    printf("Project: %s
", itProj->c_str());
    strList fileName = parseProject( itProj->c_str() );
    strList::iterator itFile = fileName.begin();
    while( itFile != fileName.end() )
    {
      printf("  File %s
", itFile->c_str());
      // do something with the project file
      itFile++;
    }
    fileName.clear();
    itProj++;
  }

以上是关于以编程方式修改/创建vcproj文件的主要内容,如果未能解决你的问题,请参考以下文章

html 将以编程方式附加外部脚本文件的javascript代码片段,并按顺序排列。用于响应式网站,其中ma

以编程方式创建 MapView 并添加标记导致片段中出现空指针异常

如何从 nmake 文件创建 vcproj(VSC++ 项目文件)?

以编程方式替换片段

以编程方式将按钮添加到片段

如何在android中以编程方式在片段之间导航?