如何将C/C++程序转译成Delphi(十三)

7. Linking

There are two ways to link a DLL and import a function. Static linking is very easy and the recommended way if the DLL is certain to be available on the client's machine. If the DLL is optional it is better to use dynamic linking (runtime-linking) to make sure that the application does not fail just because of a missing (possibly unimportant) DLL.

Static linking encodes the calling of the library DLL into the code as a direct call with no checking for inability to connect to the library. Dynamic linking calls the library DLL during the application's run-time and has the capability to handle problems with connection to the library. Dynamic linking can occur at startup, or during the program run-time as the library is needed by the user.

Borland usually uses static linking, but a few API translations use dynamic linking. One example is the translation of SMAPI (MAPI.pas).

Let's look at the two ways of linking a DLL and importing a function.


Back to contents

7.1. Static Linking

If using static linking, simply declare the function prototype in the interface section and the implementation section the following way:

INTERFACE

{Function|Procedure} FunctionName [FunctionDeclaration;]

IMPLEMENTATION

{Function|Procedure} FunctionName external DLLName [name 'FunctionExportName'];

The translation of a function prototype was in the previous section. What about importing the function? It's not obligatory to include the parameter part of the function in the implementation section, but you can if you want. As an example to explain how to import a function, let's use the OpenEvent function from the kernel32.dll.

There are two implementations of the function, one with unicode-support (16bit-WideChar) and one with ansi-character (8bit char) support.

The C-declaration is

WINBASEAPI

HANDLE

WINAPI

OpenEventA(
    DWORD dwDesiredAccess,
    BOOL bInheritHandle,
    LPCSTR lpName
    );
WINBASEAPI
HANDLE
WINAPI
OpenEventW(
   DWORD dwDesiredAccess,
    BOOL bInheritHandle,
    LPCWSTR lpName
    );

#ifdef UNICODE
#define OpenEvent  OpenEventW
#else
#define OpenEvent  OpenEventA
#endif // !UNICODE

Three function names are declared: OpenEventA for the ansi-character version of the function, OpenEventW for the wide-character (unicode) version, and OpenEvent. OpenEvent uses the widechar version of the function (OpenEventW ) if the UNICODE symbol is declared or the ansichar-version OpenEventA if the UNICODE symbol is not declared.

INTERFACE
{...}

function OpenEventA (dwDesiredAccess: DWORD; 
                    bInheritHandle: BOOL; 
                     lpName: PAnsiChar): THandle; stdcall;

function OpenEventW (dwDesiredAccess: DWORD; 
                    bInheritHandle: BOOL; 
                     lpName: PWideChar): THandle; stdcall;

{$IFDEF UNICODE}

function OpenEvent (dwDesiredAccess: DWORD; 
                      bInheritHandle: BOOL; 
                     lpName: PWideChar): THandle; stdcall;


{$ELSE}
 
function OpenEvent (dwDesiredAccess: DWORD; 
                    bInheritHandle: BOOL; 
                    lpName: PChar): THandle; stdcall;

{$ENDIF}

{...}

IMPLEMENTATION

Const
  kernel32 = 'kernel32.dll';

{...}

function OpenEventA; external kernel32 name 'OpenEventA';

function OpenEventW; external kernel32 name 'OpenEventW';

{$IFDEF UNICODE}


function OpenEvent; external kernel32 name 'OpenEventW';
{$ELSE}
function OpenEvent; external kernel32 name 'OpenEventA';
{$ENDIF}
{...}


Back to contents

### 回答1: 好的,以下是使用C语言实现将数字换成字母的代码示例: ```c #include <stdio.h> int main() { int num = 65; // 将数字65换成字母'A' char letter = (char)num; // 强制类型换 printf("The letter is %c\n", letter); // 输出字母'A' return 0; } ``` 在这个示例中,我们将数字65强制换为char类型,然后将其赋值给变量letter。最后,我们使用printf函数将字母'A'输出到控制台。 请注意,这只适用于将数字换为其ASCII码等价物的情况。如果要将数字换为其他字母系统中的字母,需要使用不同的换方法。 ### 回答2: 在C语言中,可以使用字符数组和字符串的特性来实现将数字译成字符的功能。 首先,可以定义一个字符数组来存储对应的字符: ```c char digits[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; ``` 这个数组中,每个元素表示对应的数字字符。 接下来,可以编写一个函数来实现将数字译成字符的操作: ```c void convertToChar(int number, char *result) { int count = 0; int temp = number; // 计算数字的位数 while (temp != 0) { temp /= 10; count++; } // 将数字从低位到高位译成字符 for (int i = count-1; i >= 0; i--) { // 从最高位开始 result[i] = digits[number % 10]; // 取余数作为索引,从digits数组中取对应的字符 number /= 10; // 去掉最低位 } result[count] = '\0'; // 字符串末尾添加结束符 } ``` 这个函数接受一个整数和一个字符指针作为参数,将对应的字符存储在字符指针指向的数组中。 可以在主函数中调用这个函数进行测试: ```c int main() { int num = 12345; char charArray[6]; convertToChar(num, charArray); printf("%s\n", charArray); return 0; } ``` 运行程序后,输出将会是: ``` 12345 ``` 这样就实现了将数字译成字符的功能。 ### 回答3: 在C语言中,我们可以使用类型换来将数字译成字符。 首先,我们需要了解ASCII码。ASCII码是一种常用的字符编码标准,用于将字符映射到数字。在ASCII码表中,每个字符都对应着一个唯一的数字。 为了将数字译成字符,我们可以使用类型换操作符`(char)`。这个操作符可以将一个整数类型的值换为字符类型。 下面是一个简单的示例代码,演示了如何将一个数字译成字符: ```c #include <stdio.h> int main() { int number = 65; // 假设我们要将数字65译成字符,对应ASCII码中的字母'A' char character = (char)number; // 使用类型换将数字换为字符 printf("译后的字符为: %c\n", character); return 0; } ``` 运行上述代码,我们将会得到输出结果为: ``` 译后的字符为: A ``` 在这个示例中,我们将数字65译成了字符'A'。使用`(char)`操作符将整数类型的数字换成字符类型,并通过`printf`函数将译后的字符输出到控制台。 需要注意的是,当数字超出ASCII码表所能表示的范围时,译结果可能会是不可打印的字符或者乱码。因此,在实际应用中,我们需要确保将数字译成字符的操作是在ASCII码表所能包含的范围内。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值