The following error message is returned:
System. accessviolationexception: attempts to read or write data to protected memory. This usually indicates that other memory is damaged.
I used Delphi to find the cause of this error. Use Delphi to write a dynamic library. The method is as follows:
function Hello(Name:pchar):pchar; export; var myStr :string; begin myStr:='hi!' + string(Name); result:=pchar(myStr); end;
Use C # To call
[DllImport("minmax.dll", CharSet = CharSet.Ansi)]public static extern string Hello(string pName);
Calling Hello ("Kevin") really shows the error I expected. So we thought we could solve the problem by adjusting the character processing.
function Hello1(Name:pchar):pchar; export; var Buffer:pchar; maxSize:integer; begin maxSize := sizeof(Name); GetMem(Buffer,maxSize); StrCopy(Buffer,Name); result:=Buffer; end;
In fact, the two methods have no problem with string operations. The problem is that the method definition is missing "stdcall" and the first line of code is called.
function Hello1(Name:pchar):pchar;stdcall; export;
There is no problem with calling both methods in C. At this point, I thought I had found a direction, but I accidentally found an interesting phenomenon. So I want to take a look at it. Because the function I encountered a problem also returns a long. To simplify the description, the method in Delphi is as follows, and-1 is returned directly.
function Hello2(pint:LongInt;row:integer;pname:pchar; pvalue:pchar):integer; stdcall; export; begin result:=-1; end;
C # defines this external function in this way.
[DllImport("minmax.dll", CharSet = CharSet.Ansi)]public static extern long Hello2(long pint, int row, string pname, string pvalue);
Calling hello2 in C # produces a large number! Not what I expected-1! I am not very familiar with Delphi. I used to return the type of long used to flip books. I remember that longint is actually a 32 number. Therefore, the return type of the C # Definition Statement is changed to int. Result-1 arrived on schedule! This laid the foundation for finding another memory corruption!
Because delphi can call the method that needs to be called at work, but C # has an error in calling the method, so we want to use Delphi as a dynamic library as a medium pool,
Function put2 (pint: longint; row: integer; pname: pchar; pvalue: pchar): integer; stdcall; export; var bufferval: pchar; maxsize: integer; begin maxsize: = sizeof (pvalue); getmem (bufferval, maxsize); strcopy (bufferval, pvalue); // call the Method Result of the target database: =-1; end;
C # defines the above external functions.
[DllImport("minmax.dll", CharSet = CharSet.Ansi)]public static extern int put2(long pint, long row, string pname, string pvalue);
Calling put2 in C # also prompts that the memory is damaged. Why? Is it because put2 has two long parameters more than hello1 ?! The head suddenly flashed a thought. Do C ++ and Delphi both have 32-bit long and C # are 64-bit? The two have different memory sizes allocated for long! Try to modify the external Function Definition of C #
[DllImport("minmax.dll", CharSet = CharSet.Ansi)]public static extern int put2(int pint, int row, string pname, string pvalue);
Solve the problem!