【问题标题】:Unable to use WriteProfileInt() in a C++ windows application无法在 C++ Windows 应用程序中使用 WriteProfileInt()
【发布时间】:2014-05-22 09:05:24
【问题描述】:

我正在编写一个 C++ 应用程序,我想将一个整数值写入 INI 文件,以便每次运行应用程序时都可以保留它。

但我无法在我的程序中使用 WriteProfileInt() API,每当我使用它时,它都会给我以下错误:- “WriteProfileInt”:找不到标识符

但是 GetProfileInt() 工作正常,谁能建议我该怎么做? 我正在使用 Visual Studio 2013。

这是我正在使用的示例代码:-

public ref class main_form : public System::Windows::Forms::Form

{
public:

   int i2c_addr;
   int temp;

   main_form(void)
   {

      InitializeComponent();
   }

   void InitializeComponent(void)
   {
          WriteProfileInt(_T("device"),_T("i2c_address"),8);
          temp = GetProfileInt(_T("device"),_T("i2c_address"),0);
          this->i2c_addr = temp;
   }
}

但它给了我 WriteProfileInt(_T("device"),_T("i2c_address"),8); 的错误;

请帮助我进一步处理。

【问题讨论】:

  • 请出示您的代码。
  • public ref class main_form : public System::Windows::Forms::Form { public: int i2c_addr;国际温度;公共:线程^我的线程; main_form(void) { InitializeComponent(); } void InitializeComponent(void) { WriteProfileInt(_T("device"),_T("i2c_address"),5); temp = GetProfileInt(_T("device"),_T("i2c_address"),0);
  • 您可以使用代码更新您的原始帖子。
  • 使用edit 链接添加代码。另外,您错误地回答了这个问题。您不是在编写 C++ 应用程序,而是在使用 .NET Framework。
  • 另外不要使用GetProfileStringWriteProfileString函数!永远!提供它们只是为了与旧的 16 位 Windows 应用程序兼容。他们写入 WIN.INI 文件,任何应用程序都不应修改该文件。请改用GetPrivateProfileStringWritePrivateProfileString

标签: .net winforms winapi c++-cli


【解决方案1】:

在 API 级别上,您需要执行以下操作:

char bf[50];
sprintf(bf,"%d", my_int_value);
WriteProfileString("MyApp", "MySection", bf);  // will write to the registry
WritePrivateProfileString("C:\\myprog\\myprog.ini", "MySection", bf); // will write to the ini file

希望对你有帮助

【讨论】:

  • 不,你不会。除非你是一个时间旅行者。 WriteProfileString 自 Windows 3 以来就没有使用过。它写入 WIN.INI 文件,应用程序没有业务修改。您正在寻找的功能是WritePrivateProfileString。而且您也不会使用窄字符串。十多年来,Windows 已经完全采用 Unicode,是时候使用该程序了。公平地说,WritePrivateProfileString 会将编码与文件匹配,但您仍应调用宽变体。
  • 此函数不会写入任何配置文件 .ini,如果这是有意的,它会写入注册表。如果没有,并且用户想要写入文件,那么所有这些 API 函数都带有“私有”版本。它是作为一种便利功能提供的,以简化注册表的写入和读取。
【解决方案2】:

您的代码正试图将时钟拨回 21 年。回到 Windows 3.11,最后一个 Windows 版本仍然具有 Win.ini 配置文件。

那再也行不通了。 winapi 仍然支持 GetProfileInt() 来模拟 Win.ini 文件中的一些常见条目并保持遗留代码运行。但是 WriteProfileInt() 已被完全删除,没有希望假装 .ini 文件仍然存在并且可能正常运行。向后兼容性在 Windows 中非常具有传奇色彩,但在某处停止了降压。你找到了。

System.ini 和 Win.ini 在功能上已被注册表替换。您可以改用它,也许RegistryKey class 提供对注册表的访问。在 .NET 中,应用程序设置是仍然保持基于配置文件的推荐替代方法。但是,从 C++/CLI 不方便,IDE 没有其他语言那样的设置设计器。将配置保存在 XML 文件中通常是一个不错的选择,请使用 System.Xml 命名空间中的类。

【讨论】:

    猜你喜欢
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 2011-01-29
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    相关资源
    最近更新 更多