Multiple function calls from C # to C ++ unmanaged code raise an AccessViolationException

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 //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; ... ... } 

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?

+6
source share
2 answers

I know this may be unsatisfactory, but as soon as I removed the output redirection that I used for debugging from my C / C ++ DLL, the problem stopped. Everything is working now, so I assume that this is essentially equivalent to answering my own question. Thanks everyone for the answers.

0
source

I suggest trying the following:

 [DllImport("C:\\c_keycode.dll", EntryPoint = "generateKeyCode", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] static extern IntPtr generateKeyCode(string serial, string option, char c_type); 

Note the new CharSet field of the CharSet attribute.

The next idea is to use the MarshalAs attribute:

 [DllImport("C:\\c_keycode.dll", EntryPoint = "generateKeyCode", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)] static extern IntPtr generateKeyCode([MarshalAs(UnmanagedType.LPTStr)] string serial, [MarshalAs(UnmanagedType.LPTStr)] string option, char c_type); 
+2
source

Source: https://habr.com/ru/post/905139/


All Articles