为什么我不能通过 pinvoke 使 GetPrivateProfileString 工作? [英] Why can't I get GetPrivateProfileString to work via pinvoke?

查看:31
本文介绍了为什么我不能通过 pinvoke 使 GetPrivateProfileString 工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行我在 64 位 Vista 上编写的 c# 控制台应用程序.代码如下:

Running a c# console app I wrote on 64 bit Vista. Here's the code:

class Class1
{
    static void Main(string[] args)
    {
        Debug.Assert(File.Exists(@"c:	est.ini"));
        StringBuilder sb = new StringBuilder(500);
        uint res = GetPrivateProfileString("AppName", "KeyName", "", sb, sb.Capacity, @"c:	est.ini");
        Console.WriteLine(sb.ToString());
    }
    [DllImport("kernel32.dll")]
    static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
}

我敢肯定我会听到一个大大的DUH!"寻求答案,但我不明白为什么这不起作用.除了 Debug.Assert,此代码是从 此页面

I'm sure I'll get a big "DUH!" for an answer, but I'm not seeing why this fails to work. Other than the Debug.Assert, this code was cut from the c# sample at this page

推荐答案

这个问题也困扰了我一整天.我想我找到了一个解决方法,我真的不想处理它:在 .ini 文件的第一个部分标题之前插入一个空行.现在运行您的应用,看看您是否开始看到您期望的值.

This one has been busting my chops all day, too. I think I found a work-around, which I really don't want to have to deal with: insert a blank line before the first section header in your .ini file. Now run your app and see if you don't start seeing the values you were expecting.

考虑到这个错误显然已经存在多年,我很惊讶 MS 到现在还没有修复它.但是,.ini 文件应该在几年前就消失了.这当然很有趣,因为 MS 在很多地方使用 .ini 文件(例如,desktop.ini).但我认为 MS 知道这个错误,因为我注意到我的 desktop.ini 文件包含一个前导空行.嗯……

Considering this bug has apparently been around for years, I'm surprised MS hasn't fixed it by now. But then, .ini files were supposed to have gone away years ago. Which of course is funny, because there are a lot of places MS uses .ini file (e.g, desktop.ini). But I think MS is aware of the bug, because I notice my desktop.ini files include a leading blank line. Hmmm...

这篇关于为什么我不能通过 pinvoke 使 GetPrivateProfileString 工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆