title: Using a C/C++ DLL with FreePascal
body: <p>First of all I am using Mingw 4.8 as the compiler of the C++ DLL in Code:blocks 13.12 and Lazarus 1.4.2 for working with the pascal code.(windows 7)</p>
  
  <p>I need to generete a dll in c++ or c that can be called from a pascal program.</p>
  
  <p>The problem is that my knowlegde about pascal is null, It dosn't look really complicate to make a simple program but I can't find good information about how to import and use a C/C++ DLL.</p>
  
  <p>The only thing that moreless worked was this: <a href="http://www.drbob42.com/delphi/headconv.htm" rel="nofollow">http://www.drbob42.com/delphi/headconv.htm</a>
  My real code:</p>
  
  <p>Pascal:</p>
  
  <pre><code>funtion hello():Integer; external 'function' index 1;
  ...
  Label1.Caption:=IntToStr(hello()); 
  </code></pre>
  
  <p>C++ DLL header:</p>
  
  <pre><code>#ifndef function_H
  #define function_H
  #ifdef __cplusplus
  extern "C" {
  #endif
  #ifdef BUILDING_DLL
  #define FUNCTION_DLL __declspec(dllexport)
  #else
  #define FUNCTION_DLL __declspec(dllimport)
  #endif
   int __stdcall FUNCTION_DLL hello( );
  
  #ifdef __cplusplus
  }
  #endif
  #endif
  </code></pre>
  
  <p>C++ file:</p>
  
  <pre><code>    #include &lt;stdio.h&gt;
  
  #include "function.h"
    __stdcall int hello( )
  {
  return 8;
  }
  </code></pre>
  
  <p>But when try to pass any argument or do something complicated with the function,starts to give randoms numbers.</p>
  
  <p>This is the new code:
  Pascal:</p>
  
  <pre><code>function function1(t1:Integer):Integer; external 'function' index 1;  
  ...
  entero:=8;
  Label1.Caption:=IntToStr(function1(entero2));
  </code></pre>
  
  <p>Also I update the c++ code to this:</p>
  
  <p>C++:  </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #include "function.h"
    __stdcall int function1(int t1)
  {
  return t1*2;
  }
  </code></pre>
  
  <p>Header:</p>
  
  <pre><code>#ifndef funtion_H
  #define funtion_H
  #ifdef __cplusplus
  extern "C" {
  #endif
  #ifdef BUILDING_DLL
  #define FUNCTION_DLL __declspec(dllexport)
  #else
  #define FUNCTION_DLL __declspec(dllimport)
  #endif
   int __stdcall FUNCTION_DLL function1(int t1);
  
  #ifdef __cplusplus
  }
  #endif
  #endif
  </code></pre>
  
  <p>I aslo read this other info:<a href="http://www.jrsoftware.org/ishelp/index.php?topic=scriptdll" rel="nofollow">http://www.jrsoftware.org/ishelp/index.php?topic=scriptdll</a>. And tried to implement the dll call like this:</p>
  
  <p>Pascal:</p>
  
  <pre><code>function function1(t1: Integer): Integer; external 'function1@files:function.dll';
  </code></pre>
  
  <p>But I receive an error saying that: </p>
  
  <blockquote>
    <p>The procedure entry point function1 could not be located in the
    dynamic link library function.dll</p>
  </blockquote>
  
  <p>I'm looking for an example that works or a online tutorial or something to continue working because I am very stuck with this.
  Thank you in advance.</p>