blog:unmanagedcodeinvokation

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";
[DllImport(AVR309DLL)]
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);
 
 
[DllImport("kernel32.dll")]
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;
}

x

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