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

问题描述 投票:1回答:1

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

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

c++ visual-studio-2010
1个回答
0
投票

下面是我编写的一些代码,用于从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.\n", 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.\n", 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])
           ||('\n' == 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\n", itProj->c_str());
    strList fileName = parseProject( itProj->c_str() );
    strList::iterator itFile = fileName.begin();
    while( itFile != fileName.end() )
    {
      printf("  File %s\n", itFile->c_str());
      // do something with the project file
      itFile++;
    }
    fileName.clear();
    itProj++;
  }
© www.soinside.com 2019 - 2024. All rights reserved.