I declared a DLL import in my C # program that looks like this:
[DllImport("C:\\c_keycode.dll", EntryPoint = "generateKeyCode", CallingConvention = CallingConvention.Cdecl)] static extern IntPtr generateKeyCode(char[] serial, char[] option, char c_type);
It refers to the generateKeyCode() function inside my DLL.
Here is the error code (breakpoints used):
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
Basically, I imported this DLL so that I could pass parameters to the function, and could execute its algorithm and just return the result to me. I used the marshaler function ...
public static string genKeyCode_marshal(string serial, string option, char type) { return Marshal.PtrToStringAnsi(generateKeyCode(serial.ToCharArray(), option.ToCharArray(), type)); }
... so that I can call correctly. Inside my C ++ header file, I defined a line as indicated in the answer to this question (this is the returnBufferString present at the top of the C / C ++ function).
I call this function several times when I use the NumericUpDown control to go from 1.0 to 9.9 in increments of 0.1 (each up or down accompanies another function call) and then returns again. However, every time I try to do this, the program is captured after an apparent set of function calls (it stops at 1.9 on the way back if I just go up and down, or earlier if I alternate a little up and down),
Please note that it works and gives me the value I want, there are no discrepancies.
I changed the buffer size to a slightly smaller number (5012), and when I tried to run the program, the first time I called the function, it threw an AccessViolationException. However, doubling the size of the buffer to twice (32768) the original had no effect compared to the original - up to 9.9 from 1.0 and vice versa, it stops at 1.9 and throws an exception.
EDIT: The default is ANSI, therefore ANSI. No problem. Is this a memory allocation problem?