Unmanaged code invokation

When making an unmanaged call if the imported DLL does not exist you program will crash. Its much better to first check to see if the DLL is there. Here is a snippet in C#.

public const string AVR309DLL = "AVR309.DLL";
public static extern int DoGetInfraCode(ref byte[] TimeCodeDiagram, ref int DiagramLength);

How to we make sure the AVR309DLL library exists?

[DllImport ("kernel32.dll")]
private extern static IntPtr LoadLibrary(string fileName);
private extern static IntPtr FreeLibrary(IntPtr hModule);
public static bool isDLLAvailable()
  IntPtr hModule = LoadLibrary(AVR309DLL);
  bool result = hModule.ToInt32() > 32;
  if(result) FreeLibrary(hModule);
  return result;


  • blog/unmanagedcodeinvokation.txt
  • Last modified: 2009/11/27 17:54
  • by