title: Multiple function calls from C# to C++ unmanaged code causes AccessViolationException
body: <p>I have declared a DLL import in my C# program that looks like this:</p>
  
  <pre><code>[DllImport("C:\\c_keycode.dll", EntryPoint = "generateKeyCode", 
             CallingConvention = CallingConvention.Cdecl)]
  static extern IntPtr generateKeyCode(char[] serial, char[] option, char c_type);
  </code></pre>
  
  <p>It references the function <code>generateKeyCode()</code> inside of my DLL. </p>
  
  <p>Here is the code that is causing an error (used breakpoints):</p>
  
  <pre><code>const char* generateKeyCode(char serial[], 
                   char option[], 
                   char c_type)
  {
  returnBufferString = "";
  SHA1_CTX context;
  int optionLength = 0;
  #ifdef WIN32
  unsigned char buffer[16384] = {0};
  #else
  unsigned char buffer[256] = {0}; 
  
  #endif
  //char output[80];
  char keycode[OPTION_KEY_LENGTH+1]        = "";
  int digest_array_size = 10; //default value for digest array size
  unsigned char digest[20] = {0};
  char optx[24] = {0};
  char c_type_upper;
  // Combine serial # and Option or Version number
  
  char str1[30] = {0};
  int i;
  int size = 0;
  int pos = 0;
  
  
  ...
  ...
  }
  </code></pre>
  
  <p>Basically, I imported this DLL so I could pass the function parameters and it could do its algorithm and simply return me a result. I used this marshaler function...</p>
  
  <pre><code>public static string genKeyCode_marshal(string serial, string option, char type)
  {
      return Marshal.PtrToStringAnsi(generateKeyCode(serial.ToCharArray(), 
             option.ToCharArray(), type));
  }
  </code></pre>
  
  <p>...so I could make the call properly. Inside of my C++ header file, I have defined a string, as indicated is helpful in the answer to this <a href="http://discuss.fogcreek.com/dotnetquestions/default.asp?cmd=show&amp;ixPost=1108" rel="nofollow">question</a> (it is the <code>returnBufferString</code> variable present at the top of the C/C++ function).</p>
  
  <p>I make this function call several times as I use a <code>NumericUpDown</code> control to go from 1.0 to 9.9 in increments of 0.1 (each up or down accompanies another function call), and then back down again. However, every time I try to do this, the program hitches after a seemingly set number of function calls (stops at 1.9 on the way back down if I just go straight up and down, or earlier if I alternate up and down a bit). </p>
  
  <p>Please note that it works and gives me the value I want, there are no discrepancies there.</p>
  
  <p>I changed the buffer size to some smaller number (5012) and when I tried to run the program, on the first function call it threw the AccessViolationException. However, doubling the buffer size to twice (32768) the original had no effect in comparison to the original -- going straight up to 9.9 from 1.0 and down back again, it stops at 1.9 and throws the exception.</p>
  
  <p>EDIT: Default is ANSI, so it is ANSI. No problems there. Is this a memory allocation issue??</p>