Cody Jason
Cody Jason

Reputation: 19

Error: must declare a body because it is not marked abstract, extern, or partial

The code produces the following errors:

Error: Ini.IniFile.WritePrivateProfileString(string, string, string, string)' must declare a body because it is not marked abstract, extern, or partial

Error2: Ini.IniFile.GetPrivateProfileString(string, string, string, System.Text.StringBuilder, int, string)' must declare a body because it is not marked abstract, extern, or partial

using System.Runtime.InteropServices;
using System.Text;

namespace Ini
{
  public class IniFile
  {
    public string path;

    public IniFile(string INIPath)
    {
      this.path = INIPath;
    }

    [DllImport("kernel32")]
    private static long WritePrivateProfileString(string section, string key, string val, string filePath);

    [DllImport("kernel32")]
    private static int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

    public void IniWriteValue(string Section, string Key, string Value)
    {
      IniFile.WritePrivateProfileString(Section, Key, Value, this.path);
    }

    public string IniReadValue(string Section, string Key)
    {
      StringBuilder retVal = new StringBuilder((int) byte.MaxValue);
      IniFile.GetPrivateProfileString(Section, Key, "", retVal, (int) byte.MaxValue, this.path);
      return ((object) retVal).ToString();
    }
  }
}

Upvotes: 1

Views: 3984

Answers (1)

Inisheer
Inisheer

Reputation: 20794

When using PInvoke to call native methods, you need to include extern. For example:

static extern bool WritePrivateProfileString(string lpAppName,
   string lpKeyName, string lpString, string lpFileName);

http://www.pinvoke.net/default.aspx/kernel32.writeprivateprofilestring

Upvotes: 1

Related Questions