このページはEtoJ逐語翻訳フィルタによって翻訳生成されました。

翻訳前ページへ


DLLs: Using them; 令状ing them- Dt3k HOME - - - - - - - TUTORIALS INDEX - - - - - - - - - - - - Other 構成要素 for programmers Delicious  Bookmark this on Delicious    Recommend to StumbleUpon

Delphi: Using or 令状ing DLLs

(This page 以前 had (警察などへの)密告,告訴(状) about using InpOut32.dll, which has been moved to the page the link will take you to.)

Introduction: What DLLs are; why we want them; why we don't; 代案/選択肢s.

"DLL" stands for "dynamic link library". When a programmer has something of general usefulness to other programmers, he or she may choose to "一括" the 構成要素 in a DLL. 使用者s must have both the DLL and the program that uses the things in it on their system, but, once they do, using the program which uses the things in the DLL 作品 just like using any other program. Only the programmers will be aware of the fact that 決まりきった仕事s the main program uses are 含む/封じ込めるd in the 外部の library.

I have a general disinclination for things 存在 more コンビナート/複合体 than necessary. In general, I like my 使用/適用s to 要求する just one とじ込み/提出する, the .exe とじ込み/提出する. There are times, however, when the 総計費 of other とじ込み/提出するs is 令状d. For example, help とじ込み/提出するs seem to be 価値(がある) the "複雑さ".

DLLs are 特に useful if you have something that many programs will use. I often work with 半導体素子s in the Dallas 1-Wire family. One of those 半導体素子s 変える's its 気温 to a 数字表示式の signal that a program can 過程. Is is 価値(がある) putting the read-the-半導体素子 code into every program that uses one the 半導体素子s? Probably not. A DLL could be created which would encapsulate all of the "messy bits", and all any programmer would need to know is how to call the 関連した 決まりきった仕事s within the DLL. Working with bits of code within DLLs is very like than working with 手続きs and 機能(する)/行事s within a monolithic program.


DLLs are also written when a 供給者 wants to keep 支配(する)/統制する of 面s of their work. If I 解放(する) the source code for a program reading the 気温 sensor 半導体素子s, then my 適切な時期s for selling that すぐに 縮む. I could put it into a DLL, and 制限する 使用者s' 接近 with 登録 重要なs.

DLLS are also useful when someone is selling some 金物類/武器類. Printer drivers are often DLLs.

If you have some Delphi code which is going to be useful in many Delphi programming 事業/計画(する)s, 代案/選択肢 ways of 再生利用するing your work (a good idea, for さまざまな 推論する/理由s) are...



The glory of DDLs is that they are standalone とじ込み/提出するs which you put on a machine, and other 使用/適用s, written in almost any language, can use things from inside the DLL. Even a spreadsheet or fancy wordprocessing 文書 can 利益 from things in DLLs. I have a separate tutorial for you which explains the 詳細(に述べる)s of 接近ing 機能(する)/行事s and subroutines in a DDL from OpenOffice's ooCalc spreadsheet, and some 付加 comments on the 支配する その上の 負かす/撃墜する this page..

Two parts: First: Using a DLL, then 令状ing a DLL.

The "令状ing DLLs" section was reviewed March 2011, using Delphi 4 on a Windows XP, SP3, machine.

Using a DLL

To 完全にする this tutorial, you will need to download a zip とじ込み/提出する. Within it, you will find DD50Demo.dll. Put a copy of that in the folder you want to work in. I would 示唆する calling the folder DD50.

Start an 使用/適用 in the usual way. Call the form DD50testF1, the 部隊 DD50testU1, and the 事業/計画(する) DD50test.

Put a button on the form. call it buTestSayHi. Make the button's OnClick event handler say....

手続き TDD50testF1.buTestSayHiClick(Sender: TObject);
begin
SayHi;
end;
... and... just after the uses line 近づく the start of the program, 追加する the に引き続いて...

手続き SayHi; stdcall 外部の 'DD50demo.dll';
... and try running the program! If you get 半端物 errors ("debugger kernel...", etc), be sure you have DD50demo.dll in the same folder as your program, and check for typos. When you click the button, you should get a message box on the 審査する with "Hi" in it. Inside the DLL, all SayHi does is "Showmessage('Hi');

(An unimportant aside: In Delphi 2, you could put the 宣言 of the call just before the "var" line, but in later 見解/翻訳/版s that 原因(となる)s problems.)

Look over your program: It has SayHi; in the button OnClick handler. Ordinarily, you'd get a 収集する error 説 that "SayHi" is not known to the compiler.

The line 説....
        手続き SayHi; stdcall 外部の 'DD50demo.dll';
.. tells the compiler: "If you 遭遇(する) "SayHi", then go to the DLL called DD50demo to see what to do." This line "宣言するs" the 手続き.

So far so good, I hope. Let's 接近 two more things from the DLL...

After the SayHi line, 追加する....
        showmessage('Hi (機の)カム from DD50demo, 見解/翻訳/版 '+pcDLLVersion);
.... and after the 宣言 of SayHi, 追加する...
        機能(する)/行事 pcDLLVersion:pchar; stdcall 外部の 'DD50demo.dll';
//    N.B.: Do not use "string" type variables
Run the program now, and you see two messages. One comes from within the DLL, the other comes from within the program you have written.... but notice: part of the message in "your" message (機の)カム from inside the DLL.

An aside: you will need to master pchar type strings to send things to or from DLLs. You can receive things from DLLs into a normal Delphi string, as we have just done, but さもなければ: it is pchar time. They aren't a big hassle, just a distraction to get past.

While we're コースを変えるd from the main story anyway, let me について言及する:

The error....
"Debugger kernel error. Error code: 1."
... may arise if the DLL you are trying to use cannot be 設立する. It should be in the same folder as your program, or in Windows/ system, or in Windows/System32. Alternatively, it may be that you have typos in lines like "手続き SayHi; stdcall 外部の 'DD50demo.dll';".

支援する to work!....

The things we've done to date have been pretty trivial.

Now we're going to use a more 複雑にするd 機能(する)/行事 from inside the DLL. It's 職業 is to 人物/姿/数字 out what the price of something would be with 税金 from what the price would be without 税金, and the 税金 率. In other words, if you knew something cost $100 without 税金, and that the 税金 率 was 10%, the program should be able to tell you that the cost with 税金 would be $110.

The code gets a little messy, but only because to input and 生産(高) numbers, we go through strings.

Just before the main "var", with the two 類似の lines we've already 追加するd, 追加する the に引き続いて to the program......
機能(する)/行事 singWithTax(singBeforeTax,singTaxRate:選び出す/独身):選び出す/独身; stdcall 外部の 'DD50demo.dll';

Also 追加する a new button, call it buCalcTax. For now, make it's OnClick handler say....
showmessage(floattostr(singWithTax(100,12)));
... and get that much running. When you run the program, and click buCalcTax, you should get a message with 112 in it. A little 天然のまま, so we'll develop it as follows... but you've just seen all of the DLL 面s of this.

Put a label on your form, and change buCalcTax's OnClick handler to...
label1.caption:=floattostr(singWithTax(100,12));
.. and now when you click the button, the answer should appear in the label.

Now 追加する an edit box to the form. 始める,決める it's text 所有物/資産/財産 to 1000. Change buCalcTax's OnClick handler to...
label1.caption:=(floattostr(singWithTax(strtofloat(edit1.text),10)));
.. and now you can enter the "before 税金" price in the edit box, and when you click the button, the with-税金 price appears in the label. If you enter something 非,不,無-numeric in the edit box, the program will 落ちる over, but 取引,協定ing with that isn't hard, just not germane to our topic: DLLs.

So! You've seen how to 令状 programs to 接近 things from within a DLL. How do you know what is in them? The 供給者s of the DLL will 供給する that (警察などへの)密告,告訴(状), and / or there are ソフトウェア 道具s you can use to learn something of what's 利用できる.

If you want to try working with "real" DLLs, you could try to 割れ目 using cards.dll, which will be on any computer with Solitaire (silly game), or Freecell (井戸/弁護士席 価値(がある) learning.) With cards.dll you can draw cards from within your own programs, with very little 成果/努力. For some good (警察などへの)密告,告訴(状) on cards.dll visit Paul Kimmel's article on Developer.com. He 作品 in VB... poor chap.. but what he has written should translate to Delphi. There are only three things to 接近 within the DLL! I have to 収容する/認める 敗北・負かす in the half hour I spent trying to do it... if you 後継する please let me know? (And no, I don't want to do it with the VCL wrappers that Google will show you.)

All of this started because I 手配中の,お尋ね者 to use things from within InpOut32.dll, which lets you 接近 your PCs ports. I've written a tutorial on that, which I was able to do!

Anyway... you've 完全にするd the first part of my tutorial... you've seen how to use things from within a DLL. Next we'll look at 令状ing a DLL, and what better DLL to 令状 than the one you've just been using??!


令状ing a DLL



If you have not already done so, create a new folder to work in, I was using one called DD50. If you created a DD50 for the first part of this tutorial, you can continue working there.

Start Delphi, do とじ込み/提出する| New| DLL

Just before the "begin" and "end." at the end of the library (which is much like the 部隊s we more frequently work with), 挿入する...

手続き SayHi;stdcall;
begin
showmessage('Hi');
end;//SayHi
(I would 追加する rems to the "begin" and "end" which were 供給するd as part of the DLL 骸骨/概要....
begin //start of DLL

end.//of DLL
BE VERY CAREFUL to 含む the ";stdcall;" at the end of the first line, and in 類似の places later on in this work. The Delphi error trapper doesn't seem as able to 位置/汚点/見つけ出す errors when you are making DLLs, at least in Delphi 見解/翻訳/版 2. Absence of that ";stdcall;" cost me かなりの wasted time while creating this tutorial!

Save your work. Be sure to navigate to the folder you 始める,決める up, say DD50, and save the 事業/計画(する) as DD50.dpr. You won't be asked about saving anything else. Don't try to run or 収集する it yet.

The "Showmessage" 手続き is one that you are likely to have used frequently. It is 供給するd for you in the Delphi Dialogs 部隊. Unlike working with 使用/適用 部隊s, you will need to 追加する "Dialogs" to the uses 条項 of the DLL manually, making it....

uses
  SysUtils,
  Classes,
  Dialogs;
You don't ordinarily have to 追加する the 部隊 指名する for things 供給するd by Delphi as you build your code, but you do while building DLLs.

Now you must create an "輸出(する)s" 条項. It is as follows...

輸出(する)s SayHi;
It goes just before the...
begin //start of DDL

end. //of DLL
...at the end of the library.

Now try to 収集する your DLL. You can either use ctrl-F9, or, go though the menus, using 事業/計画(する)| 収集する DD50. If there are no errors in your code, you won't see much. If there are errors... 直す/買収する,八百長をする them!

By the way: If you try to run the code, you'll get a message 説 you can't. This is normal. Don't try to run the code, but don't worry if you try, and get the "can't" message. This isn't something for you to 直す/買収する,八百長をする. You will 結局 収集する your code, and then 接近 the DLL that has been created.. but 接近 it with an separate, 十分な, .exe 使用/適用. DLLs can't run on their own, like an .exe.

With Windows Explorer or 類似の, you should now see a とじ込み/提出する called DD50.DLL in your DD50 folder. The program you wrote for the first part of this tutorial should work with it. You can proceed in either of two ways...

選択 1: 改名する the old "DD50demo.DLL" as "DD50demo.old", and 改名する "DD50.DLL" (Which you just created) "DD50demo.DLL"

選択 2: Re-open DD50Test, and change the 言及/関連s to "DD50demo.DLL" to "DD50.DLL"

選択 2 has the advantage of 許すing you to easily make その上の changes to either the DLL or DD50Test to see what happens.

In any 事例/患者, so far so good. Now we will 追加する the sDLLVersion 機能(する)/行事. Just after the "SayHi" 機能(する)/行事, 追加する....

機能(する)/行事 pcDLLVersion:pchar;stdcall;
begin
result:='1.0.0';
end;
(The pchar type is 類似の to the string type. You can't use string type variables for passing things to and from DLLs. If you do, you may get an "接近 違反" message when you run the program.)

You must also and, 延長する the "輸出(する)s" 条項 as follows...

輸出(する)s SayHi,
        pcDLLVersion;
Yes... you 名簿(に載せる)/表(にあげる) just the 決まりきった仕事's 指名する in the 輸出(する)s 条項... you don't 示す in any way the parameters that go with it.

Forgetting to 追加する the 機能(する)/行事 or 手続き to the 輸出(する)s 条項 is another way to get the "Debugger kernel" error message.


Calling our DLL from OpenOffice

So! We have a DLL. The "innards" don't 事柄 now... That's one of the delights of DLLs!! But what is it good for?

The DLL we've written? Not a lot... except it does give us a "tame" DLL we can use to 実験(する) our しっかり掴む of using things within DLLs.

The DLL we've created (you can just 収穫 the 完全にするd DLL from the dd50 DDL tutorial zip 古記録, if you wish) can be used with OpenOffice. In, for instance, the spreadsheet module, ooCalc, you can 始める,決める things up so that you can have a 独房 陳列する,発揮する the string returned by the pcDLLVersion 機能(する)/行事 inside the DLL. Now, in our "for 演習s" DLL, that is always 単に "1.0.0" But there is no 推論する/理由 that you couldn't 令状 (or buy) a different DLL which would return the 気温 sensed by some electronics 監視するd by the ソフトウェア in the DLL.

In 新規加入 to 供給(する)ing the ooCalc spreadsheet with values, the DLL can be used to 延長する the 能力s of an ooCalc worksheet. Again, the trivial example which can be 論証するd with dd50demo.dll is the 陳列する,発揮する of "Hello World" on the 審査する in a message box. Of course, you can get ooCalc to do this without 訴える手段/行楽地ing to a subroutine in a DLL, but if you can get a message box to pop up, imagine the other things that could be taken care of inside a DLL?

I have a separate tutorial which explains the 詳細(に述べる)s of 接近ing 機能(する)/行事s and subroutines in a DDL from an OpenOffice ooCalc worksheet, which, Oh Google, some people call a "spreadsheet", of course. (I prefer to reserve "spreadsheet for VisiCalc, ooCalc, Excel, etc... the programs, and "worksheet" for the sort of 文書 manipulated with a spreadsheet.)


An extra delight...

So! I hope you feel you've got the basic idea? To 終わりにする/要約する, we'll be a little more adventurous.

To make a start, we'll do something not using a DLL, but putting the code in our program. Then we'll move it out to the DLL, just to see how 平易な it can be.

We're going to create a 機能(する)/行事 that 作品 out the price "with 税金" of something from it's "before 税金" price. If the 税金 率 is 12, and the before-税金 price is $100, then the with-税金 price is $112.

Before you read on: In the wise words that Mr. Dent had trouble がまんするing by: Don't panic! The に引き続いて code looks a little convoluted, but it is 99% because of the "joys" of working with パーセントs, and of moving things from strings to numbers and 支援する again. Just (1)偽造する/(2)徐々に進む ahead to where it says "Breath again".....

追加する the に引き続いて 機能(する)/行事 to the program you've been using to 実験(する) your DLL:

機能(する)/行事 singWithTax(singBeforeTax,singTaxRate:選び出す/独身):選び出す/独身;
begin
result:=singBeforeTax*((singTaxRate/100)+1);
end;

As with any 機能(する)/行事, you'll also need to 宣言する it in the interface section, so, just after "機能(する)/行事 pcDLLVersion:pchar; stdcall..." 追加する...

機能(する)/行事 singWithTax(singBeforeTax,singTaxRate:選び出す/独身):選び出す/独身;

Put an edit box on the form, and a button. Make the edit box's text 所有物/資産/財産 100. Call the button buCalcTax. Make the buttons's OnClick handler....

手続き TDD50testF1.buCalcTaxClick(Sender: TObject);
begin
label1.caption:=floattostr(singWithTax(strtofloat(edit1.text),12));
end;

Okay! Breath again!

That code should work. It will throw up errors if you have something other than a number in the edit box when you click "CalcTax", but you'd 推定する/予想する that, I hope? (It isn't a big 問題/発行する, and a distraction from out 追求(する),探索(する) to master DLLs... Let it go.)

Get it working!

Now we're going to move the 機能(する)/行事 over to the DLL. Why not leave it in the program? You could... but if you 手配中の,お尋ね者 to 令状 a "singWithTax" once and for all, never have to fuss with it again, no 事柄 how many programs you 令状, you could do so, put it in a DLL, and henceforth just bundle the DLL with any new programs, and just call singWithTax from the DLL. (There are other ways to "一括" things you want to use again and again for your own 目的s... but that's a story for another day. You are 令状ing this DLL so that you'll have a better しっかり掴む of what you are doing when you use other people's DLLs.)

So... to move the 機能(する)/行事 to the DLL...

Don't try running anything until その上の notice.

In the program that 実験(する)s the DLL, move the first line 説....

機能(する)/行事 singWithTax(singBeforeTax,singTaxRate:選び出す/独身):選び出す/独身;

... to just after the program's uses 条項, and 修正する it to say....

機能(する)/行事 singWithTax(singBeforeTax,singTaxRate:選び出す/独身):選び出す/独身; stdcall 外部の 'DD50demo.dll';

From the interface part of your program that 実験(する)s your DLL, 削減(する) (select, then 圧力(をかける) ctrl-X) all of the に引き続いて:

機能(する)/行事 singWithTax(singBeforeTax,singTaxRate:選び出す/独身):選び出す/独身;
begin
result:=singBeforeTax*((singTaxRate/100)+1);
end;

Save your program that 実験(する)s the DLL.

Open the DLL itself for editing and re- 収集するing.

Put your insertion point just before the word "輸出(する)s", paste (ctrl-V) what you 削減(する) a moment ago, and 追加する...

;stdcall;

...to the first line of the 構成要素 you just 輸入するd into the DLL.

追加する "singWithTax" to the "輸出(する)s" 条項.

Save the DLL. 収集する it (ctrl-F9). の近くに that; Run the program that 実験(する)s the DLL... it should run okay! You've done it!




            力/強力にするd by FreeFind
  場所/位置 search Web search
場所/位置 地図/計画する    What's New    Search This search 単に looks for the words you enter. It won't answer "Where can I download InpOut32?"


Click here if you're feeling 肉親,親類d! (促進するs my 場所/位置 経由で "Top100Borland")

If you visit 1&1's 場所/位置 from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service. They 申し込む/申し出 things for the beginner and the 会社/団体.www.1and1.com icon

広告 from page's editor: Yes.. I do enjoy 収集するing these things for you. I hope they are helpful. However... this doesn't 支払う/賃金 my 法案s!!! Sheepdog ソフトウェア (tm) is supposed to help do that, so if you 設立する this stuff useful, (and you run a Windows or MS-DOS PC) please visit my freeware and shareware page, download something, and 循環させる it for me? Links on your page to this page would also be 高く評価する/(相場などが)上がるd!
Click here to visit editor's freeware, shareware page.

Link to Tutorials main page
How to 接触する the editor of this page, Tom Boyd


Valid HTML 4.01 Transitional Page 実験(する)d for 同意/服従 with INDUSTRY (not MS-only) 基準s, using the 解放する/自由な, 公然と accessible validator at validator.w3.org


If this page 原因(となる)s a script to run, why? Because of things like Google パネル盤s, and the code for the search button. Why do I について言及する scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....