none
Use a Pascal library in C# RRS feed

  • Question

  • Hi,

    I have a verry little dll writen with Pascal as a test to see how to use it with c#.

    the dll:

    library Dll_test;
    
    {$mode objfpc}{$H+}
    
    uses
      Classes
      { you can add units after this };
    
    function Proc_Totaal :pchar; stdcall; export;
    begin
      result := PChar('Test');
    end;
    
    exports
      Proc_Totaal;
    
    {$R *.res}
    
    begin
    end.  

    In C# i have:

    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace DllTest
    {
        public partial class Form1 : Form
        {
            //const string SimpLibName = "Dll_test.dll";
    
            public Form1()
            {
                InitializeComponent();
            }
    
            [DllImport("Dll_test.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
            public static extern string Proc_Totaal();
    
            private void button1_Click(object sender, EventArgs e)
            {
                textBox1.Text = Proc_Totaal();
            }
        }
    }

    When i click the button i get an error. This is:

    Attempt to load a program with an incorrect format. (Exception of HRESULT: 0x8007000B)

    What is wrong? Or where can i find a tutorial for loading pascal dll's?

    Friday, February 8, 2019 9:10 PM

All replies

  • The error itself indicates you are mixing formats. Did you compile your Pascal DLL as 32 or 64-bit? The C# program needs to match this. 

    You could also run into an issue with the DLL itself. What Pascal compiler are you using? Does it generate a Windows DLL directly or does it compile down to P-Code which then needs to be translated? Given that it seems to support export syntax it seems like it is a Windows DLL. You could try calling the DLL from C++ to eliminate any issues with the DLL itself.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, February 8, 2019 10:01 PM
    Moderator
  • Hi HansvB69,

    Thank you for posting here.

    For your question, you want to Use a Pascal library in C#.

    You could refer to the following link.

    http://wiki.freepascal.org/Using_Pascal_Libraries_with_.NET_and_Mono#A_simple_C.23_app

    Note: This response contains a reference to a third-party World Wide Web site. Microsoft is providing this information as a convenience to you.

    Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Hope my advice could be helpful.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 13, 2019 2:35 AM
    Moderator
  • Hi 

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find a solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, February 19, 2019 8:08 AM
    Moderator