soh*_*hil 4 .net c# dll dllimport
我正在尝试使用DllImport将dll导入到我的C#项目中,如下所示:
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key,string val,string filePath);
Run Code Online (Sandbox Code Playgroud)
另外,我添加了命名空间System.Runtime.InteropServices:
using System.Runtime.InteropServices;
Run Code Online (Sandbox Code Playgroud)
不过,我收到一个错误:"当前上下文中不存在名称'DllImport'"
是否可以在类中的哪个位置导入dll?
您的声明中可能还有错误的返回类型.尝试使用bool:
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(string section, string key,string val,string filePath);
Run Code Online (Sandbox Code Playgroud)
参考文献:http://msdn.microsoft.com/en-us/library/ms725501(v = vs.85).aspx
编辑:
DllImports必须放在你班级的体内.不在内部方法或构造函数中.
public class Class1
{
//DllImport goes here:
[DllImport("kernel32")]
private static extern ...
public Class1()
{
...
}
/* snip */
}
Run Code Online (Sandbox Code Playgroud)