API declaration location advice

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi! In VB-6 we had the concept of modules where I frequently placed my API declarations. Where should I place API declarations and usage in C# winforms based applications? If I place it in one form, how can other forms use it? For instance, where do I place LockWindowUpdate() function that all forms in the application can benefit from ?

Any input is greatly appreciated,

--Mike
 
Put the "P/Invokes" into a separate class, and I would also recommend
putting them into a separate namespace and separate class library (aka dll
aka assembly).

using System;
using System.Runtime.InteropServices;

namespace YourCompany.Win32
{
public sealed class User32
{
private const string dllName = "user32.dll";

private User32()
{
}

[DllImport(dllName, SetLastError=true)]
public static extern bool LockWindowUpdate(IntPtr hWnd);
}
}

This way you can compile the assembly and reference it in not only your
current project but future projects as well. Whenever you have another Win32
API that you need to call just add it to this code file, compile it, and
then reference that inside your project. Of course, if it was a call from
Kernel32.dll, for example, you would want to add a "Kernel32" class and then
add the p/invoke to it. This allows reusability (OOP is good!) as this type
of stuff is not really specific to your project. Alternatively, you can
probably find something on the Internet similar to this as I'm sure someone
has started an assembly somewhere out there.

--
Tim Wilson
..Net Compact Framework MVP

Mike said:
Hi! In VB-6 we had the concept of modules where I frequently placed my API
declarations. Where should I place API declarations and usage in C# winforms
based applications? If I place it in one form, how can other forms use it?
For instance, where do I place LockWindowUpdate() function that all forms in
the application can benefit from ?
 
Back
Top