Delphi-Dll function with return Boolean throw AccessViolationException

Answered Delphi-Dll function with return Boolean throw AccessViolationException

  • Tuesday, April 03, 2012 9:52 AM
     
      Has Code

    Hello,

    i use a Delphi-Win32-Dll in C# (.Net 4.0, VS 2010).

    All functions works fine, but only the function with returns Delphi-Boolean throws AccessviolationException.

    here is a litle bit of Code:

    Delphi function-Typedef:

    DbOpen = function: Boolean; stdcall;

    C# function Typedef:

    [return: MarshalAs(UnmanagedType.Bool)]
    protected delegate Boolean TMatriKSDbOpen();
    protected TMatriKSDbOpen matriKSDbOpen;

    C#-Dynamic-Dll loading

    ...LoadLibraryEx...
            public Delegate CreateDynamicDllDelphi(string functionName, Type delegateType)
            {
                IntPtr funcAddr = GetProcAddress(dllHandle, functionName);
                if (funcAddr == IntPtr.Zero)
                    throw new EntryPointNotFoundException("Function: " + functionName);
                return Marshal.GetDelegateForFunctionPointer(funcAddr, delegateType);
            }
    matriKSDbOpen = (TMatriKSDbOpen)CreateDynamicDllDelphi("DbOpen", typeof(TMatriKSDbOpen));

    c# Using:

    bool openOk = matriKSDbOpen(); // throw AccessViolationException

    i have also test

    [return: MarshalAs(UnmanagedType.I1)]
    I'm now out of order ;-((

    • Edited by Mario3211 Tuesday, April 03, 2012 9:54 AM
    • Moved by Leo Liu - MSFT Thursday, April 05, 2012 9:22 AM Moved for better support. (From:Visual C# General)
    •  

All Replies

  • Tuesday, April 03, 2012 11:25 AM
     
     
    I don't think the problem is the return value. It could cause an unbalanced stack, not an access violation. Are you sure the function does not have parameters?
  • Wednesday, April 04, 2012 8:34 AM
     
     Answered

    Yes this function have non parameters!

    All other function (with and without params, returns PChar=>String and Integer=>Int32) works fine.

    We have make many tests an we give up.

    Now we are change the Type from Boolean to PChar(C# String) '0' and '1' this works.