Problems linking to Windows APIs in VS 2012 Express for Desktop in C#
c#, dllimport, pinvoke, windows
Solution
Make sure you're placing this declaration within a class definition, not outside it.
Typically, you'd keep P/Invokes within a static class called `NativeMethods`, which you then invoke using a call like `NativeMethods.SetLayeredWindowedAttributes(...)`. For example:
internal static class NativeMethods
{
[DllImport("user32.dll")]
public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
}
If you want to call it without a type reference, then you need to put it in the same class as you're calling it in, but unless you're sure you won't use this P/Invoke anywhere else, I wouldn't recommend it.
Problem
I'm new to Windows desktop programming (or at least I haven't done it since, um, Windows 3.0). I've got VS 2012 Express for Desktop installed. I have a default forms-based project created and running. Now I'd like to add in a Windows API with the following lines per pinvoke.net: ``` [DllImport("user32.dll")] static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags); ``` I'm getting two errors for this code: - The modifier 'extern' is not valid for this item (on the closing square bracket of the attribute) - Expected class, delegate, enum, interface, or struct (on `bool`) What am I doing wrong?