Use DLL compiled in Delphi 7 in C#

c#, delphi, dll

Solution

Here's an example of how you could declare the GetCPUSpeed function in C#:

class Program
{
    [DllImport("the_name_of_the_delphi_library.dll")]
    public static extern double GetCPUSpeed();

    static void Main(string[] args)
    {
        double cpuSpeed = GetCPUSpeed();
        Console.WriteLine("CPU speed: {0}", cpuSpeed);
    }
}

And there are the other declarations you could try:

[DllImport("the_name_of_the_delphi_library.dll")]
public static extern string CPUFamily();

[DllImport("the_name_of_the_delphi_library.dll")]
public static extern int GetCpuTheoreticSpeed();

[DllImport("the_name_of_the_delphi_library.dll")]
public static extern bool IsCPUIDAvailable();

[DllImport("the_name_of_the_delphi_library.dll")]
public static extern string GetCPUID(byte cpuCore);

[DllImport("the_name_of_the_delphi_library.dll")]
public static extern string GetCPUVendor();

[DllImport("the_name_of_the_delphi_library.dll")]
public static extern uint MemoryStatus(int memType);

[DllImport("the_name_of_the_delphi_library.dll")]
public static extern string MemoryStatus_MB(int memType);

[DllImport("the_name_of_the_delphi_library.dll")]
public static extern string GetPartitionID(char partition);

[DllImport("the_name_of_the_delphi_library.dll")]
public static extern string GetIDESerialNumber(byte driveNumber);

Problem

I need to use a DLL (Hardware ID Extractor) made in Delphi 7 in my C# application. The functions exported by this DLL are: Exported functions: ``` // CPU function GetCPUSpeed: Double; function CPUFamily: ShortString; { Get cpu identifier from the windows registry } function GetCpuTheoreticSpeed: Integer; { Get cpu speed (in MHz) } function IsCPUIDAvailable: Boolean; Register; function GetCPUID (CpuCore: byte): ShortString; Function GetCPUVendor: ShortString; // RAM function MemoryStatus (MemType: Integer): cardinal; { in Bytes } function MemoryStatus_MB (MemType: Integer): ShortString; { in MB } // HDD function GetPartitionID (Partition : PChar): ShortString; { Get the ID of the specified patition. Example of parameter: 'C:' } function GetIDESerialNumber(DriveNumber: Byte ): PChar; { DriveNr is from 0 to 4 } ``` I know (obviously) that string in Delphi are not null terminated and are byte (ASCII). But I have no clue on how to map these Delphi string to C#. Thanks.

Original source