C++ 能否使用SetupAPI检索.inf文件中某行的键?
我正在使用Setup API中的函数SetupGetLineText()从inf文件的一个部分读取一行 行的格式为:C++ 能否使用SetupAPI检索.inf文件中某行的键?,c++,windows,setupapi,C++,Windows,Setupapi,我正在使用Setup API中的函数SetupGetLineText()从inf文件的一个部分读取一行 行的格式为: key=value SetupGetLineText将显示以返回零件的值。这很好,但我也想知道我正在阅读的当前上下文的关键是什么。设置API中似乎没有用于读取密钥的函数 如果您能帮助我们找到钥匙,我们将不胜感激。希望渺茫: BOOL SetupGetStringField( __in PINFCONTEXT Context, __in DWORD Fie
key=value
SetupGetLineText将显示以返回零件的值。这很好,但我也想知道我正在阅读的当前上下文的关键是什么。设置API中似乎没有用于读取密钥的函数
如果您能帮助我们找到钥匙,我们将不胜感激。希望渺茫:
BOOL SetupGetStringField(
__in PINFCONTEXT Context,
__in DWORD FieldIndex,
__inout PTSTR ReturnBuffer,
__in DWORD ReturnBufferSize,
__out PDWORD RequiredSize
);
与
字段索引[in]基于1的索引 指定行内的字段 字符串应该从中删除 找回了使用0到0的字段索引 检索字符串键(如果存在) 远射:
BOOL SetupGetStringField(
__in PINFCONTEXT Context,
__in DWORD FieldIndex,
__inout PTSTR ReturnBuffer,
__in DWORD ReturnBufferSize,
__out PDWORD RequiredSize
);
与
字段索引[in]基于1的索引 指定行内的字段 字符串应该从中删除 找回了使用0到0的字段索引 检索字符串键(如果存在)
下面是一个使用SetupAPI处理inf文件的类
#ifndef SETUP_INF_PARSER_H_INC_
#define SETUP_INF_PARSER_H_INC_
#include <string>
#include <vector>
namespace win32
{
namespace inf
{
class line
{
public:
line() { ZeroMemory( &_ctx, sizeof( _ctx ) ); }
line( const INFCONTEXT& ctx ) : _ctx( ctx ) {}
bool operator==( const line& op2 ) const
{
return memcmp( &_ctx, &op2._ctx, sizeof( _ctx ) ) == 0;
}
bool operator!=( const line& op2 ) const
{
return memcmp( &_ctx, &op2._ctx, sizeof( _ctx ) ) != 0;
}
bool operator<( const line& op2 ) const
{
std::wstring str0, str1;
if( string_at( 0, str0 ) && op2.string_at( 0, str1 ) )
return str0 < str1;
return memcmp( &_ctx, &op2._ctx, sizeof( _ctx ) ) < 0;
}
bool isValid() const
{
return *this != line();
}
bool string_at( unsigned int idx, std::wstring& str ) const
{
wchar_t buf[4096];
DWORD size_needed = 0;
if( SetupGetStringField( (PINFCONTEXT) &_ctx, idx, buf, sizeof( buf ), &size_needed ) )
{
str = buf;
return true;
}
return false;
}
std::vector<std::wstring> contents()
{
std::vector<std::wstring> lst;
for( unsigned int idx = 0; true; ++idx )
{
std::wstring str;
if( !string_at( idx, str ) )
{
break;
}
lst.push_back( str );
}
return lst;
}
operator INFCONTEXT*() { return &_ctx; }
private:
INFCONTEXT _ctx;
};
class section
{
public:
section()
{
ZeroMemory( &_ctx, sizeof( _ctx ) );
}
section( HINF hInf, const std::wstring& section_name )
: _Name( section_name )
{
ZeroMemory( &_ctx, sizeof( _ctx ) );
SetupFindFirstLine( hInf, section_name.c_str(), 0, &_ctx );
}
~section()
{
}
class iterator : public std::iterator<std::forward_iterator_tag,line>
{
friend section;
public:
iterator()
{
ZeroMemory( &_ctx, sizeof( _ctx ) );
}
iterator& operator++()
{
INFCONTEXT tmpCtx;
if( SetupFindNextLine( _ctx, &tmpCtx ) )
{
_ctx = tmpCtx;
}
else
{
_ctx = line();
}
return *this;
}
line operator*()
{
return _ctx;
}
line* operator->()
{
return &_ctx;
}
bool operator==( const iterator& op2 ) const
{
return _ctx == op2._ctx;
}
bool operator!=( const iterator& op2 ) const
{
return _ctx != op2._ctx;
}
private:
iterator( INFCONTEXT& ctx )
: _ctx( ctx )
{}
line _ctx;
};
bool operator<( const section& op2 ) const
{
return _Name < op2._Name;
}
iterator begin()
{
return iterator( _ctx );
}
iterator end()
{
return iterator();
}
private:
std::wstring _Name;
INFCONTEXT _ctx;
};
class inf_file
{
public:
inf_file( const std::wstring& str )
{
UINT err_line = 0;
_inf = SetupOpenInfFile( str.c_str(), 0, INF_STYLE_WIN4, &err_line );
if( _inf == INVALID_HANDLE_VALUE )
{
DWORD err = GetLastError();
// do something ..
throw std::invalid_argument( "failed to open inf file" );
}
}
~inf_file()
{
SetupCloseInfFile( _inf );
}
section get_section( const std::wstring& section_name )
{
return section( _inf, section_name );
}
class iterator : public std::iterator<std::forward_iterator_tag,section>
{
friend inf_file;
public:
iterator() : _hInf( 0 ), _idx( 0 ) {}
iterator& operator++()
{
if( _idx < 0 )
_idx = 0;
else
++_idx;
init_at( _idx );
return *this;
}
const section& operator*() const
{
return _Section;
}
section* operator->()
{
return &_Section;
}
bool operator==( const iterator& op2 ) const
{
return _idx == op2._idx;
}
bool operator!=( const iterator& op2 ) const
{
return !(*this == op2);
}
private:
void init_at( int idx )
{
if( _hInf != 0 && _hInf != INVALID_HANDLE_VALUE )
{
wchar_t buf[128] = { 0 };
UINT sizeNeeded = 0;
if( SetupEnumInfSectionsW( _hInf, _idx, buf, ARRAYSIZE( buf ), &sizeNeeded ) )
{
_Section = section( _hInf, buf );
return;
}
}
_Section = section();
}
iterator( HINF& ctx )
: _idx( 0 ), _hInf( ctx )
{}
section _Section;
HINF _hInf;
int _idx;
};
iterator begin()
{
return iterator( _inf );
}
iterator end()
{
return iterator();
}
struct VersionInfo
{
std::wstring signature;
std::wstring class_name;
GUID class_guid;
std::wstring provider;
std::wstring date;
std::wstring version;
};
bool parseVersionInfo( VersionInfo& info )
{
section s( _inf, L"Version" );
for( section::iterator i = s.begin(); i != s.end(); ++i )
{
std::vector<std::wstring> str_list = i->contents();
if( str_list.size() > 1 )
{
std::wstring& entry = str_list[0];
std::wstring& entry1 = str_list[1];
if( entry == L"Signature" )
{
info.signature = entry1;
}
else if( entry == L"Class" )
{
info.class_name = entry1;
}
else if( entry == L"ClassGUID" )
{
IIDFromString( const_cast<wchar_t*>( entry1.c_str() ), &info.class_guid );
}
else if( entry == L"Provider" )
{
info.provider = entry1;
}
else if( entry == L"DriverVer" )
{
info.date = entry1;
info.version = str_list[2];
}
}
}
return true;
}
private:
inf_file( const inf_file& );
inf_file& operator=( const inf_file& );
HINF _inf;
};
};
};
#endif // SETUP_INF_PARSER_H_INC_
\ifndef安装程序\u INF\u解析器\u H\u公司_
#定义安装程序\u INF\u解析器\u H\u INC_
#包括
#包括
名称空间win32
{
命名空间inf
{
班级线
{
公众:
line(){ZeroMemory(&_ctx,sizeof(_ctx));}
行(const INFCONTEXT&ctx):\u ctx(ctx){}
布尔运算符==(常量行和op2)常量
{
返回memcmp(&_ctx,&op2._ctx,sizeof(_ctx))==0;
}
布尔运算符!=(常量行和op2)常量
{
返回memcmp(&_ctx,&op2._ctx,sizeof(_ctx))!=0;
}
布尔运算符()
{
返回&u ctx;
}
布尔运算符==(常量迭代器和op2)常量
{
返回_ctx==op2._ctx;
}
布尔运算符!=(常量迭代器和op2)常量
{
返回_ctx!=op2._ctx;
}
私人:
迭代器(INFCONTEXT和ctx)
:ctx(ctx)
{}
线路ctx;
};
布尔运算符()
{
返回和返回部分;
}
布尔运算符==(常量迭代器和op2)常量
{
返回_idx==op2._idx;
}
布尔运算符!=(常量迭代器和op2)常量
{
返回!(*this==op2);
}
私人:
无效初始值(整数idx)
{
if(\u hInf!=0&&u hInf!=无效的\u句柄\u值)
{
wchar_t buf[128]={0};
UINT SIZENEDED=0;
if(SetupEnumInfSectionsW(_hInf、_idx、buf、ARRAYSIZE(buf)和sizeNeeded))
{
_截面=截面(_hInf,buf);
返回;
}
}
_节=节();
}
迭代器(HINF和ctx)
:_idx(0),_hInf(ctx)
{}
第(u)节;;
欣欣(HINF);
int_idx;
};
迭代器begin()
{
返回迭代器(_inf);
}
迭代器结束()
{
返回迭代器();
}
结构版本信息
{
std::wstring签名;
std::wstring类名称;
GUID类\u GUID;
std::wstring提供者;
std::wstring日期;
std::wstring版本;
};
bool parseVersionInfo(VersionInfo&info)
{
第s节(“版本”);
对于(节::迭代器i=s.begin();i!=s.end();++i)
{
std::vector str_list=i->contents();
如果(str_list.size()>1)
{
std::wstring&entry=str_list[0];
std::wstring&entry1=str_列表[1];
如果(条目==L“签名”)
{
info.signature=entry1;
}
else if(条目==L“类”)
{
info.class_name=entry1;
}
else if(条目==L“ClassGUID”)
{
IIDFromString(const_cast(entry1.c_str()),&info.class_guid);
}
else if(条目==L“提供者”)
{
info.provider=entry1;
}
else if(条目==L“驱动服务器”)
{
info.date=entry1;
info.version=str_list[2];
}
}
}
返回true;
}
私人:
inf_文件(const inf_文件&);
inf_文件和运算符=(const inf_文件&);
HINF_inf;
};
};
};
#endif//SETUP\u INF\u PARSER\u H\u INC_
这里有一个类,我使用SetupAPI处理inf文件
#ifndef SETUP_INF_PARSER_H_INC_
#define SETUP_INF_PARSER_H_INC_
#include <string>
#include <vector>
namespace win32
{
namespace inf
{
class line
{
public:
line() { ZeroMemory( &_ctx, sizeof( _ctx ) ); }
line( const INFCONTEXT& ctx ) : _ctx( ctx ) {}
bool operator==( const line& op2 ) const
{
return memcmp( &_ctx, &op2._ctx, sizeof( _ctx ) ) == 0;
}
bool operator!=( const line& op2 ) const
{
return memcmp( &_ctx, &op2._ctx, sizeof( _ctx ) ) != 0;
}
bool operator<( const line& op2 ) const
{
std::wstring str0, str1;
if( string_at( 0, str0 ) && op2.string_at( 0, str1 ) )
return str0 < str1;
return memcmp( &_ctx, &op2._ctx, sizeof( _ctx ) ) < 0;
}
bool isValid() const
{
return *this != line();
}
bool string_at( unsigned int idx, std::wstring& str ) const
{
wchar_t buf[4096];
DWORD size_needed = 0;
if( SetupGetStringField( (PINFCONTEXT) &_ctx, idx, buf, sizeof( buf ), &size_needed ) )
{
str = buf;
return true;
}
return false;
}
std::vector<std::wstring> contents()
{
std::vector<std::wstring> lst;
for( unsigned int idx = 0; true; ++idx )
{
std::wstring str;
if( !string_at( idx, str ) )
{
break;
}
lst.push_back( str );
}
return lst;
}
operator INFCONTEXT*() { return &_ctx; }
private:
INFCONTEXT _ctx;
};
class section
{
public:
section()
{
ZeroMemory( &_ctx, sizeof( _ctx ) );
}
section( HINF hInf, const std::wstring& section_name )
: _Name( section_name )
{
ZeroMemory( &_ctx, sizeof( _ctx ) );
SetupFindFirstLine( hInf, section_name.c_str(), 0, &_ctx );
}
~section()
{
}
class iterator : public std::iterator<std::forward_iterator_tag,line>
{
friend section;
public:
iterator()
{
ZeroMemory( &_ctx, sizeof( _ctx ) );
}
iterator& operator++()
{
INFCONTEXT tmpCtx;
if( SetupFindNextLine( _ctx, &tmpCtx ) )
{
_ctx = tmpCtx;
}
else
{
_ctx = line();
}
return *this;
}
line operator*()
{
return _ctx;
}
line* operator->()
{
return &_ctx;
}
bool operator==( const iterator& op2 ) const
{
return _ctx == op2._ctx;
}
bool operator!=( const iterator& op2 ) const
{
return _ctx != op2._ctx;
}
private:
iterator( INFCONTEXT& ctx )
: _ctx( ctx )
{}
line _ctx;
};
bool operator<( const section& op2 ) const
{
return _Name < op2._Name;
}
iterator begin()
{
return iterator( _ctx );
}
iterator end()
{
return iterator();
}
private:
std::wstring _Name;
INFCONTEXT _ctx;
};
class inf_file
{
public:
inf_file( const std::wstring& str )
{
UINT err_line = 0;
_inf = SetupOpenInfFile( str.c_str(), 0, INF_STYLE_WIN4, &err_line );
if( _inf == INVALID_HANDLE_VALUE )
{
DWORD err = GetLastError();
// do something ..
throw std::invalid_argument( "failed to open inf file" );
}
}
~inf_file()
{
SetupCloseInfFile( _inf );
}
section get_section( const std::wstring& section_name )
{
return section( _inf, section_name );
}
class iterator : public std::iterator<std::forward_iterator_tag,section>
{
friend inf_file;
public:
iterator() : _hInf( 0 ), _idx( 0 ) {}
iterator& operator++()
{
if( _idx < 0 )
_idx = 0;
else
++_idx;
init_at( _idx );
return *this;
}
const section& operator*() const
{
return _Section;
}
section* operator->()
{
return &_Section;
}
bool operator==( const iterator& op2 ) const
{
return _idx == op2._idx;
}
bool operator!=( const iterator& op2 ) const
{
return !(*this == op2);
}
private:
void init_at( int idx )
{
if( _hInf != 0 && _hInf != INVALID_HANDLE_VALUE )
{
wchar_t buf[128] = { 0 };
UINT sizeNeeded = 0;
if( SetupEnumInfSectionsW( _hInf, _idx, buf, ARRAYSIZE( buf ), &sizeNeeded ) )
{
_Section = section( _hInf, buf );
return;
}
}
_Section = section();
}
iterator( HINF& ctx )
: _idx( 0 ), _hInf( ctx )
{}
section _Section;
HINF _hInf;
int _idx;
};
iterator begin()
{
return iterator( _inf );
}
iterator end()
{
return iterator();
}
struct VersionInfo
{
std::wstring signature;
std::wstring class_name;
GUID class_guid;
std::wstring provider;
std::wstring date;
std::wstring version;
};
bool parseVersionInfo( VersionInfo& info )
{
section s( _inf, L"Version" );
for( section::iterator i = s.begin(); i != s.end(); ++i )
{
std::vector<std::wstring> str_list = i->contents();
if( str_list.size() > 1 )
{
std::wstring& entry = str_list[0];
std::wstring& entry1 = str_list[1];
if( entry == L"Signature" )
{
info.signature = entry1;
}
else if( entry == L"Class" )
{
info.class_name = entry1;
}
else if( entry == L"ClassGUID" )
{
IIDFromString( const_cast<wchar_t*>( entry1.c_str() ), &info.class_guid );
}
else if( entry == L"Provider" )
{
info.provider = entry1;
}
else if( entry == L"DriverVer" )
{
info.date = entry1;
info.version = str_list[2];
}
}
}
return true;
}
private:
inf_file( const inf_file& );
inf_file& operator=( const inf_file& );
HINF _inf;
};
};
};
#endif // SETUP_INF_PARSER_H_INC_
\ifndef安装程序\u INF\u解析器\u H\u公司_
#定义安装程序\u INF\u解析器\u H\u INC_
#包括
#包括
名称空间win32
{
命名空间inf
{
班级线
{
公众:
line(){ZeroMemory(&_ctx,sizeof(_ctx));}
行(const INFCONTEXT&ctx):\u ctx(ctx){}
布尔运算符==(常量行和op2)常量
{
返回memcmp(&_ctx,&op2._ctx,sizeof(_ctx))==0;
}
布尔运算符!=(常量行和op2)常量
{
返回memcmp(&_ctx,&op2._ctx,sizeof(_ctx))!=0;
}
布尔运算符()
{
返回&u ctx;
}
布尔运算符==(常量迭代器和op2)常量
{
返回_ctx==op2._ctx;
}
布尔运算符!=(常量迭代器和op2