如何从德尔福做了一个DLL导入功能? [英] How to import a function from a DLL made in Delphi?

查看:165
本文介绍了如何从德尔福做了一个DLL导入功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以告诉我怎么做我用下面的功能在我的C程序。

Can you tell me how do i use the following functions in my C program.

Delphi的DLL - 导出功能:

Delphi DLL - Exported functions :

function GetCPUID (CpuCore: byte): ShortString; stdcall;
function GetPartitionID(Partition : PChar): ShortString; stdcall;

我没有源$ C ​​$ C为DLL,所以我必须适应我的C程序的DLL,而不是周围的其他方法。

I don't have the source code for that DLL so I must adapt my C program to that DLL and not the other way around.

我做以下,并得到误差

typedef char* (_stdcall *GETCPUID)(BYTE);
typedef char* (_stdcall *GETPID)(PCHAR);
GETCPUID pGetSerial;
GETPID pGetPID

HMODULE hWtsLib = LoadLibrary("HardwareIDExtractor.dll");
if (hWtsLib){
pGetSerial = (GETCPUID)GetProcAddress(hWtsLib, "GetCPUID");
char *str = (char*) malloc(1024);
str = pGetSerial((BYTE)"1");

pGetPID= (GETPID )GetProcAddress(hWtsLib, "GetPartitionID");
char *str1 = (char*) malloc(1024);
str1 = pGetPID("C:");
}

感谢

推荐答案

既然你没有源到DLL,你需要得到一个小创意对事物的C面。即使ShortString短被列为函数的结果,它实际上是有责任在来电者以提供在其中放置结果的位置。因为这是一个STDCALL函数,参数由右至左传递,从而意味着该ShortString短结果的地址在过去的传递。要获得此排队,它将需要列出的第一个参数。我会做的第一API,GetCPUID。在C中,它可能是这个样子:

Since you don't have the source to the DLL, you'll need to get a little creative on the C side of things. Even though the ShortString is listed as the function result, it is actually the responsibility of the caller to provide a location in which to place the result. Because this is a stdcall function, parameters are passed in from right to left, so that means that the address of the ShortString result is passed in last. To get this to line up, it will need to the first parameter listed. I'll do the first API, GetCPUID. In C, it might look something like this:

typedef struct ShortString {
  char len;
  char data[255];
};
typedef void (_stdcall *GETCPUID)(struct ShortString *result, BYTE cpuCore);

GETCPUID pGetSerial;

HMODULE hWtsLib = LoadLibrary("HardwareIDExtractor.dll");
if (hWtsLib) {
  ShortString serial;
  pGetSerial = (GETCPUID)GetProcAddress(hWtsLib, "GetCPUID");
  pGetSerial(&serial, '1');
  char *str = malloc(serial.len + 1); // include space for the trailing \0
  strlcpy(str, serial.data, serial.len);
  str[serial.len] = '\0'; // drop in the trailing null
}

我将离开GetPartitionID作为练习读者: - )

I'll leave the GetPartitionID as an exercise for the reader :-).

这篇关于如何从德尔福做了一个DLL导入功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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