Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PInvoke code usage in C#

I have the following C# code that uses DLLImport.

using System;

namespace LvFpga {
  class RegTest 
  {

    [DllImport("kernel32")]    
    public extern static int LoadLibrary(string lpLibFileName);
    [DllImport("kernel32")]    
    public extern static bool FreeLibrary(int hLibModule); 

    public static bool IsDllRegistered(string DllName)    
    {    
      int libId = LoadLibrary(DllName);
      if (libId>0) FreeLibrary(libId);
      return (libId>0);    
    }
    public static void Main(string[] args)
    {
        Console.WriteLn(IsDllRegistered("msdia100.dll"));
    }
  }
}

When I simply run csc CSCODE.cs I got the errors.

regtest.cs(7,6): error CS0246: The type or namespace name 'DllImport' could not be found (are you
        missing a using directive or an assembly reference?)
regtest.cs(7,6): error CS0246: The type or namespace name 'DllImportAttribute' could not be found
        (are you missing a using directive or an assembly reference?)
regtest.cs(9,6): error CS0246: The type or namespace name 'DllImport' could not be found (are you
        missing a using directive or an assembly reference?)
regtest.cs(9,6): error CS0246: The type or namespace name 'DllImportAttribute' could not be found
        (are you missing a using directive or an assembly reference?)

What's wrong? What might be added in options?

like image 821
prosseek Avatar asked Jun 14 '26 19:06

prosseek


2 Answers

You have to have

using System.Runtime.InteropServices;

Moreover, there is no function "Console.WriteLn". You need

Console.WriteLine(IsDllRegistered("msdia100.dll"));
like image 198
irritate Avatar answered Jun 16 '26 09:06

irritate


You are missing a using System.Runtime.InteropServices; at the beginning of the code.

like image 34
Residuum Avatar answered Jun 16 '26 09:06

Residuum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!