关于在 Delphi 中使用DLL 中的字符资源(100分)

  • 主题发起人 question
  • 开始时间
Q

question

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在 Delphi 中创建纯字符资源的 DLL 以及如何在 Delphi 中调用
它?请以 Delphi 3 为例回答。谢谢
 
H

hfade

Unregistered / Unconfirmed
GUEST, unregistred user!
delphi 没有很好的资源编辑器,
ImageEditor 好象不能产生string,
你只好用其它专门的资源编辑器了。
做好DLL后,可用LoadLibrary和LoadString函数调
DLL中的字符串。

 
Q

question

Unregistered / Unconfirmed
GUEST, unregistred user!
请详细一些,最好有例子!
 
H

huizhang

Unregistered / Unconfirmed
GUEST, unregistred user!
Hi question,

To produce pure string resources in Delphi32 is quite simple. If you
can openup ../Source/vcl/Consts.pas have a look. You will see something
like below:

unit Consts;

interface
resourcestring
SOpenFileTitle = '打开';
SAssignError='不能赋值%s给%s';
SFCreateError='不能创建文件%s';
...
implementation
end.

The reserved word "resourcestring" will tell delphi compiler to complile
the following string consts as resource string, and bind them to the
resource part of a application or a dll.

We can use these resource strings in our code just as string consts.
Delphi will handle the resource IDs for us.

Try it now!
 
H

hfade

Unregistered / Unconfirmed
GUEST, unregistred user!
resourcestring 用起来比较方便,
但要把字符串编译到一个DLL然后在其它
模块中调用还是要用WINDOWS的API
例如:
var
hDll:HMODULE;
aChar:array[0..100] of char;
err:DWORD;
begin
hDll:=LoadLibrary('string.dll');
LoadString(hDll, 61488, aChar, 100);
freeLibrary(hdll);
end;
61488 是字符串的ID号,100是长度.

 
Q

question

Unregistered / Unconfirmed
GUEST, unregistred user!
那要如何建立一个字符资源的 DLL 呢?可用其它软件
最好有详细的例子,调用的方法我也会,可如何建立一个这样的 DLL 才是关键。
谢谢各位的回答!
 
H

hfade

Unregistered / Unconfirmed
GUEST, unregistred user!
用VC 内建的资源编辑器
或用BC 的resource workshop。
 
H

hfade

Unregistered / Unconfirmed
GUEST, unregistred user!
假如你用某个工具编好资源文件后
例如是:string.res,再用DELPHI产生
一个动态库工程,在工程文件中加上
如下一句话:
{$R string.res}

例如:
library hfade;
uses
SysUtils,Classes;
const
{$R string.res}
begin
end.
 
L

lanny

Unregistered / Unconfirmed
GUEST, unregistred user!
太简单了,你可以按下面这个步骤做:
一、使用一个纯文本编辑器,编辑 abcd.rc
在里面这样定义:
/********
abcd.rc
*********/

#define IDS_STRING1 1
#define IDS_STRING2 2
#define IDS_STRING3 3
STRINGTABLE
{
IDS_STRING1, "你需要写的内容"
IDS_STRING2, "ddddd"
IDS_STRING3, "ggghhhh"
}

然后用 Delphi 安装目录下Bin子目录的 Brcc32.exe 编译这个文件
假设abcd.rc 在当前目录下
C:/Delphi/Bin>brcc32 abcd

然后运行 Delphi
建立一个 Dll 的工程,你可以这样写:

library ddd;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
View-Project Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the DELPHIMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using DELPHIMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
SysUtils,
Classes;
{$R abcd.res}
begin
end.

直接编译这个工程后可以得到 ddd.DLL 文件

二、在 Delphi 中调用
你可以在一个方法中使用下面这段代码

procedure TForm1.Button1Click(Sender: TObject);
var
hDll:HMODULE;
cChar:array[0..254] of char;
begin
hDll:=LoadLibrary('ddd.dll')
//载入 DLL 文件
LoadString(hDll, 1, cChar, 254)
//读取字符资源,1表示资源文件中定义的1
freeLibrary(hdll)
//释放 DLL
showmessage(aChar)
//显示你需要的数据
end;

不知道这样的回答你是否满意?
 
顶部