简体   繁体   中英

cannot find pinvoke dll 'kernel32'

i want to use ini file in my program using windows CE device. and i got the exception "cannot find pinvoke dll 'kernel32'" what should I do to solve this? please help me!

string Path = "c:\\Settings.ini";
        [DllImport("kernel32")]
         private static extern long WritePrivateProfileString(string section, 
string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal,                                                int size, string filePath);

    public string Read(string Section,string Key,string Value)
    {
        StringBuilder sb = new StringBuilder();
        GetPrivateProfileString(Section, Key, Value, sb, 32, Path).ToString();           // Error occured!!
        return sb.ToString();
    }

it works at vs2017 but not vs2008. please. give me your answer!

Firstly

Your version of Visual Studio will not normally make one difference to the compatibility of a Windows API call.

Secondly

You are calling an Windows API that is not supported in Windows CE

Thirdly

INI files are an old technology and replaced largely by config files, this is the way the world has moved

Lastly

If you really have the need to read and write INI files, just download a Nuget that supports it out of the box (which doesn't rely on the Windows APIs) and save your self a lot of trouble

Michael Randall makes some very valid points in his answer, though in case you are editing files from older programs, an ini handler can still be useful, and for simple human-readable config files, ini does remain handy, in my opinion; I find xml to be awful to actually open in a text editor to change settings, and registry settings are the exact opposite of portable.

So, if all you need is an ini handler, I wrote a small free library for that a couple of years ago, to handle the files of an old game that used ini format for all its settings and missions.

You can download it here:
http://nyerguds.arsaneus-design.com/project_stuff/2013/nyerguds.ini/

Mirror:
http://www.mediafire.com/file/ldebjkppf59j13r/nyerguds.ini.zip

Note that unlike most ini implementations, which just search the line and return the value, this loads the entire file into memory and translates all contents to objects, and keeps track of what is modified, so while it is useful for small configuration files, it is not advised to use it for larger data handling. A while back, some people used it in an online play system to scan the contents of a whole bunch of game maps that were in an ini-based format, and that turned out to be pretty slow.

Also note that it is text-encoding aware , and defaults to UTF-8, so if you are actually reading some old program's setting, you may need to set it to load as Windows-1252, or, in case of DOS programs, DOS-437. Since I was working with DOS files when making this, there is a static preset shortcut to DOS-437 called ENCODING_DOS_US offered by the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM