I'm trying to import a dll to my C# project using DllImport as follows:
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key,string val,string filePath);
Also, I have added the namespace System.Runtime.InteropServices:
using System.Runtime.InteropServices;
Still, I'm getting an error: "The name 'DllImport' does not exist in the current context"
Is there a limitation on where in a class you can import a dll?
In Windows, a dynamic-link library (DLL) is a kind of executable file that acts as a shared library of functions and resources. Dynamic linking is an operating system capability. It enables an executable to call functions or use resources stored in a separate file.
DllImport Attribute is a declarative tag used in C# to mark a class method as being defined in an external dynamic-link library (DLL) rather than in any . NET assembly.
You've probably also got the wrong return type in your statement. Try with bool:
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(string section, string key,string val,string filePath);
References: http://msdn.microsoft.com/en-us/library/ms725501(v=vs.85).aspx
EDIT:
DllImports have to be placed inside the body of your class. Not inside methods or the constructor.
public class Class1
{
//DllImport goes here:
[DllImport("kernel32")]
private static extern ...
public Class1()
{
...
}
/* snip */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With