in Education by
When I'm calling a Dll method it sometimes throws an exception, and sometimes doesn't. I'm calling it like this: public class DllTest { [DllImport(@"MyDll.dll")] public extern static string MyMethod(string someStringParam); } class Program { static void Main(string[] args) { DllTest.MyMethod("SomeString"); } } And the exception I get sometimes is this: AccessViolationException Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Does anyone have any idea why I only get this exception sometimes? Why does it run smoothly sometimes? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You clearly have a mismatch between the p/invoke code and the Delphi code. You have not shown the Delphi code but the C# code is enough to know what the Delphi code should look like. Your DllImport attribute uses default values for calling convention and character set. This means that the calling convention is stdcall and the character set is ANSI. You have not specified any marshalling attributes so default marshalling must be used. Therefore your Delphi code must look like this: function MyMethod(someStringParam: PChar): PChar; stdcall; begin Result := ??; end; And now here's the problem. The p/invoke marshaller treats a string return value in a very special way. It assumes that it is the responsibility of the p/invoke marshaller to deallocate the return value's memory. And it has to use the same allocator as the native code. The assumption made by the marshaller is that the shared COM allocator will be used. So the rule is that the native code must allocate the memory with the COM allocator by calling CoTaskMemAlloc. My bet is that your code does not do that and that will certainly result in errors. Here is an example of how you could make a native Delphi function that works with the C# signature in your code. function MyMethod(someStringParam: PChar): PChar; stdcall; var Size: Integer; begin Size := SizeOf(Char)*(StrLen(someStringParam)+1);//+1 for zero-terminator Result := CoTaskMemAlloc(Size); Move(someStringParam^, Result^, Size); end; Whilst you could adopt this approach I recommend an alternative. Marshal all your strings as BSTR on the C# side and WideString on the Delphi side. These are matching types which are also allocated by the COM allocator. Both parties know exactly what to do with these types and will make your life easier. Unfortunately, you cannot return a WideString from a Delphi function across an interop boundary because Delphi uses a different ABI for function return values. More details of this issue can be found in my question Why can a WideString not be used as a function return value for interop? So to work around that, we can declare the return type from the Delphi code to be TBStr. Your code would then look like this: C# [DllImport(@"MyDll.dll")] [return: MarshalAs(UnmanagedType.BStr)] private static extern string MyMethod( [MarshalAs(UnmanagedType.BStr)] string someStringParam ); Delphi function MyMethod(someStringParam: WideString): TBStr; stdcall; begin Result := SysAllocString(POleStr(someStringParam)); end;

Related questions

0 votes
    The question is: after am raising an exception, can I stop it to propagate from it's own constructor ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    The question is: after am raising an exception, can I stop it to propagate from it's own constructor ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I am trying to do a telephone directory using arrays (I have to use arrays). I am trying to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Which of the following keywords is used for throwing exception manually? (a) finally (b) try (c) throw ( ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    I want to raise an exception in Python and in future want to detect it using an except block, Can someone tell me how to do it? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Which of the following keywords is used for throwing exception manually? (a) finally (b) try (c) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    I just got Delphi 2009 and have previously read some articles about modifications that might be necessary because ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 18, 2022 in Education by JackTerrance
0 votes
    I just got Delphi 2009 and have previously read some articles about modifications that might be necessary because ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 18, 2022 in Education by JackTerrance
0 votes
    I just got Delphi 2009 and have previously read some articles about modifications that might be necessary because ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    Anyone know of an example of using Amazon SES with Delphi, or failing that, some tips to get me ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    I've got a problem with exposing a DTO class through SOAP WebService. My class looks like TKontrahent = ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I've got a problem with exposing a DTO class through SOAP WebService. My class looks like TKontrahent = ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 28, 2022 in Education by JackTerrance
0 votes
    __________ is a powerful RAT build using the language Delphi 7. (a) Stuxnet (b) T-Bomb (c) Beast (d) ... ?,Need-for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Nov 1, 2021 in Education by JackTerrance
...