title: Calling c dll in C#
body: <p><strong>Introduction to the problem:</strong></p>
  
  <p>I've got to control a certain device through API provided with a DLL file, LIB file, and c header files whose functions are declared as <strong>dllimport</strong>. </p>
  
  <p>When I use the API in a C++ project everything worked just fine -  I included the headers, lib , dll, and called the functions as declared in the header files. </p>
  
  <p>The problem begins when trying to call those functions from a C#.NET project, using [DllImport] attribute: The functions were declared with the exact name and parameters, and running the code did not throw any exception. And yet the device did not respond at all, like the functions had never been actually called.</p>
  
  <p>How it is declared in the C header: </p>
  
  <pre><code>int __declspec(dllimport) Init_Card_RTx(unsigned short device_num, unsigned short enabled_channels, unsigned short xmt_channels);
  </code></pre>
  
  <p>How it is declared in C#: </p>
  
  <pre><code>[DllImport(Path, CallingConvention = CallingConvention.Cdecl, EntryPoint = "Init_Card_RTx")]
  public static extern int Init_Card_RTx(UInt16 device_num, UInt16 enabled_channels, UInt16 xmt_channels);
  </code></pre>
  
  <p><strong>The questions:</strong></p>
  
  <ul>
  <li>Is that because the functions in the headers are declared dllimport? </li>
  <li>In that case, do I have to wrap the DLL with C++ functions declared as dllexport?</li>
  <li>What are the steps required for a C DLL to be accessible from C#?</li>
  <li>In the C# project, do I have to include the LIB file as well? not just the DLL file itself?</li>
  </ul>