我正在尝试使用 DllImport 将 dll 导入到我的 C# 项目,如下所示:
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key,string val,string filePath);
另外,我添加了命名空间 System.Runtime.InteropServices:
using System.Runtime.InteropServices;
尽管如此,我还是收到了一个错误:
“名称‘DllImport’在当前上下文中不存在”
类中导入 dll 的位置是否有限制?
您的语句中可能还得到了错误的返回类型。尝试用bool:
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(string section, string key,string val,string filePath);
参考:http://msdn.microsoft.com/en-us/library/ms725501(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/ms725501(v=vs.85).aspx
EDIT:
DllImports 必须放置在类的主体中。不在方法或构造函数内部。
public class Class1
{
//DllImport goes here:
[DllImport("kernel32")]
private static extern ...
public Class1()
{
...
}
/* snip */
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)