<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Incorrect values returned from external C library</title><link href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/6ae5082dbd/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/CODESYS-V2/thread/6ae5082dbd/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/CODESYS-V2/thread/6ae5082dbd/</id><updated>2020-02-27T20:55:14.777000Z</updated><subtitle>Recent posts to Incorrect values returned from external C library</subtitle><entry><title>Incorrect values returned from external C library</title><link href="https://forge.codesys.com/forge/talk/Codesys%2520V2.3/thread/6ae5082dbd/?limit=25#46d9" rel="alternate"/><published>2020-02-27T20:55:14.777000Z</published><updated>2020-02-27T20:55:14.777000Z</updated><author><name>pfguenette</name><uri>https://forge.codesys.com/u/pfguenette/</uri></author><id>https://forge.codesys.com9cca2f31763f26ac9308c9f3bea965caf1171ac6</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I'm following the instructions in the attached document for creating an external C library, but I'm having issues.  I've created a library with only one function - testFunc, which takes one INT and returns an INT.&lt;/p&gt;
&lt;p&gt;Saving as an external library generates a header (.h) file and a source (.c) file.  After fixing the INT -&amp;gt; int16_t issues, I compile with MSVC / Visual Studio, which generates a .obj file.  I place that file and the .lib in the same directory, and import the .lib into my Codesys project.  I can then call the library function by calling testFunc(), either via ST or CFC.  That all works, and it compiles and uploads to the controller.  But the values I get back are just garbage.  For example, I wrote my function to be:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nt"&gt;FUNCTION&lt;/span&gt; &lt;span class="nt"&gt;testFunc&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;INT&lt;/span&gt;
&lt;span class="nt"&gt;VAR_INPUT&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;inVal&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;INT&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;END_VAR&lt;/span&gt;
&lt;span class="nt"&gt;VAR&lt;/span&gt;
&lt;span class="nt"&gt;END_VAR&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nv"&gt;int16_t&lt;/span&gt; &lt;span class="nv"&gt;testFunc&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;int16_t&lt;/span&gt; &lt;span class="nv"&gt;inVal&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt;{
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;inVal&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;I send the value 10, and get back the value -24569.  Sending different values doesn't change the return value, but calling it in a different place seems to.&lt;/p&gt;
&lt;p&gt;Does anyone have any idea why this would happen?&lt;/p&gt;
&lt;p&gt;EDIT:  I just realized that even if the .obj isn't in the folder, I get the same value.&lt;/p&gt;
&lt;p&gt;&lt;a class="" href="https://forum.codesys.com//download/file.php?id=3393" rel="nofollow"&gt;C_IN_32Bit_E.pdf &lt;span&gt;&lt;span&gt;[179.98 KiB]&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>