使用DLL编译在Delphi 7中的C# [英] Use DLL compiled in Delphi 7 in C#

查看:139
本文介绍了使用DLL编译在Delphi 7中的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的C#应用​​程序中使用Delphi 7中的一个DLL(硬件ID提取器)。



此DLL导出的函数是:



导出功能:

  // CPU 
函数GetCPUSpeed:Double;
功能CPUFamily:ShortString; {从Windows注册表获取cpu标识符}
函数GetCpuTheoreticSpeed:Integer; {Get cpu speed(in MHz)}
function IsCPUIDAvailable:Boolean;寄存器;
函数GetCPUID(CpuCore:byte):ShortString;
函数GetCPUVendor:ShortString;

// RAM
函数MemoryStatus(MemType:Integer):cardinal; {in Bytes}
function MemoryStatus_MB(MemType:Integer):ShortString; {in MB}

// HDD
函数GetPartitionID(分区:PChar):ShortString; {获取指定的标记的ID。参数示例:'C:'}
函数GetIDESerialNumber(DriveNumber:Byte):PChar; {DriveNr从0到4}






我知道(显然)Delphi中的字符串不是空终止的,是byte(ASCII)。但是我不知道如何将这些Delphi字符串映射到C#。



谢谢。

解决方案

以下是C#中如何声明GetCPUSpeed函数的示例:

  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);
}
}

还有其他声明可以尝试: / p>

  [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);


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.

解决方案

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);

这篇关于使用DLL编译在Delphi 7中的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆