Pass Strings To A Function In A Delphi DLL
The code was made by me because i did not find any information about how to pass a VB string to a delphi dll function (accepting strings) hope this code helps.This code shows how to call a delphi DLL and how to code the Delphi dll to accept strings from VB. The code (2 demo projects, 1 in VB and 1 in Delphi) are pretty simple, but you get the idea. FEEL FREE TO VOTE FOR ME!
AI
Podsumowanie AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Kod źródłowy
-------------------
VB CODE:
(START A NEW PROJECT, REPLACE THE EXISTING CODE WITH THIS:)
-------------------
Option Explicit
Private Declare Function CountLetters Lib "..\delphi\project1.dll" (ByVal Str As String) As Long
Private Sub Form_Load()
Call CountLetters("This is a teststring, passed to a function in a delphi DLL")
End Sub
-------------------
DELPHI CODE
(START A NEW LIBRARY, REPLACE THE EXISTING CODE WITH THIS:)
-------------------
library Project1;
uses
Windows,
SysUtils;
function CountLetters(pData : PChar) : Cardinal; export; stdcall;
var
Handle : Integer;
tMsg : String;
begin
tMsg := 'The string passed by you; "' + pData + '" is counting ' + IntToStr(Length(pData)) + ' letters.';
MessageBoxA(Handle, pChar(tMsg), 'Delphi DLL', MB_OK);
end;
exports
CountLetters name 'CountLetters' resident;
begin
end.
Upload
<p>This is a technique I developed back in 2000-ish for one of my first
commercial clients. I had actually offered it up before I was even sure
I could do it, but nonetheless I found a way to tap into the mapquest.com
driving directions<br />
application.</p>
<p>Ever wanted to offer driving directions from your website instead of
settling for a link to just a map? It's easier than you might think, no
ASP or CGI involved, just plain old HTML. This also adds a whole new element
to your website that's just plain neat.</p>
<p>The code here has been modified from my original version so that it is
now XHTML 1.0 Valid.</p>
<p>This is pretty much a simple copy and paste job, but I'll go over it
once for you.</p>
<p>When you go to mapquest and request driving directions from one point
to another the URL you get is something like this:</p>
<p><font color="#FF0000">http://www.mapquest.com/directions/main.adp?go=1<br />
&do=nw&ct=NA&1y=US&1a=233+South+Wacker+Drive<br />
&1p=&1c=Chicago&1s=IL&1z=60606&1ah=&2y=US<br />
&2a=1+Microsoft+Way&2p=&2c=Redmond&2s=WA<br />
&2z=98052&2ah=&lr=2&x=57&y=15</font></p>
<p>In this example I used Chicago's Wrigley Field as the "From"
address</p>
<p>233 S Wacker Dr<br />
Chicago, IL<br />
60606</p>
<p>And the Microsft Headquartersas the "To" address</p>
<p>1 Microsoft Way<br />
Redmond, WA<br />
20005</p>
<p>Now the task is to break this long, long URL down. By breaking it at
each ampersand we can get a better look at this it.</p>
<p><br />
<font color="#FF0000">http://www.mapquest.com/directions/main.adp?<br />
go=1<br />
&do=nw<br />
&ct=NA<br />
&1y=US<br />
&1a=233+South+Wacker+Drive<br />
&1p=<br />
&1c=Chicago<br />
&1s=IL<br />
&1z=60606<br />
&1ah=<br />
&2y=US<br />
&2a=1+Microsoft+Way<br />
&2p=<br />
&2c=Redmond<br />
&2s=WA<br />
&2z=98052<br />
&2ah=<br />
&lr=2<br />
&x=57<br />
&y=15</font></p>
<p><br />
Now I make no allusion to understanding every single bit of that, I do
however know what I need to know. Granted this is for US addresses only,
if you want international values you'll have to look into that one on
you own.</p>
<p>So by further breaking the URL down we see that:</p>
<p><br />
<font color="#FF0000">http://www.mapquest.com/directions/main.adp?</font></p>
<p>is the page that is being posted to by mapquest, so this is where we
too must post to..</p>
<p><br />
and there are only 4 lines that seem particular to our "to"
address</p>
<p><br />
<font color="#FF0000">&2a=1+Microsoft+Way<br />
&2c=Redmond<br />
&2s=WA<br />
&2z=98052</font></p>
<p>These are our constants, this is our address, basically<br />
After we remove these we are left with</p>
<p><br />
<font color="#FF0000">go=1<br />
&do=nw<br />
&ct=NA<br />
&1y=US<br />
&1a=233+South+Wacker+Drive<br />
&1p=<br />
&1c=Chicago<br />
&1s=IL<br />
&1z=60606<br />
&1ah=<br />
&2y=US<br />
&2ah=<br />
&lr=2<br />
&x=57<br />
&y=15</font></p>
<p>This is the information we want our website visitor to enter.</p>
<p>So putting this into a form would look like this:</p>
<p> <font color="#FF0000"><form name="ddform" action=http://mapquest.com/directions/main.adp
target=_blank method=get></font></p>
<p><br />
<!--hidden fields--><br />
<font color="#FF0000"><input type="hidden" name='go' value='1'><br />
<input type="hidden" name='do' value='nw'><br />
<input type="hidden" name='ct' value='NA'><br />
<input type="hidden" name='1ah' value=''></font></p>
<p><br />
<!--The From Street--><br />
<font color="#FF0000">Street:<input name='1a' type=text size="15"><br
/></font></p>
<p><br />
<!--hidden field--><br />
<font color="#FF0000"><input type="hidden" name='1p' value=''></font></p>
<p><!--The From City--><br />
<font color="#FF0000">City:<input name='1c' type=text size="15"><br
/></font></p>
<p><!--The From State--><br />
<font color="#FF0000">State: <input name='1s' type=text size="5"><br
/></font></p>
<p><!--The From Zip Code--><br />
<font color="#FF0000">Zip:<input name="1z" type="text"
size="15"><br /></font></p>
<p><!--hidden field--><br />
<font color="#FF0000"><input type="hidden" name='z' value=''></font></p>
<p><!--hidden field for From Country--><br />
<font color="#FF0000"><input type="hidden" name='1y' value='US'></font></p>
<p><!--hidden field--><br />
<font color="#FF0000"><input type="hidden" name='2ah' value=''></font></p>
<p><br />
<!--hidden field for To Street Address--><br />
<font color="#FF0000"><input type="hidden" name='2a' value=1+Microsoft+Way'></font></p>
<p><!--hidden field--><br />
<font color="#FF0000"><input type="hidden" name='2p' value=''></font></p>
<p><!--hidden field for To City--><br />
<font color="#FF0000"><input type="hidden" name='2c' value=Redmond'></font></p>
<p><!--hidden field for To State--><br />
<font color="#FF0000"><input type="hidden" name='2s' value='WA'></font></p>
<p><!--hidden field for To Zip Code--><br />
<font color="#FF0000"><input type="hidden" name='2z' value='98052'></font></p>
<p><!--hidden field for the To Country--><br />
<font color="#FF0000"><input type="hidden" name='2y' value='US'></font></p>
<p><!--hidden field--><br />
<font color="#FF0000"><input type="hidden" name='lr' value='2'><br />
<input type="hidden" name='x' value='75'><br />
<input type="hidden" name='y' value='8'><br />
<br /><br />
<br /><br />
<input type=submit value='Submit'><br />
</form></font></p>
<p></p>
<p>And there you have it.<br />
If your business was located at the Ms headquarters anybody could get
direction right from your website. Here's a working Example</p>
<p>
<form name=ddForm action=http://mapquest.com/directions/main.adp target=_blank method=get>
<input type=hidden name='go' value='1'>
<input type=hidden name='do' value='nw'>
<input type=hidden name='ct' value='NA'>
<input type=hidden name='1ah' value=''>
Street:
<input name='1a' type=text size="15">
<input type=hidden name='1p' value=''>
City:
<input name='1c' type=text size="15">
State:
<input name='1s' type=text size="5">
<input type=hidden name='z' value=''>
<input type=hidden name='1y' value='US'>
<input type=hidden name='2ah' value=''>
<input type=hidden name='2a' value='1+Microsoft+Way'>
<input type=hidden name='2p' value=''>
<input type=hidden name='2c' value='Redmond'>
<input type=hidden name='2s' value='WA'>
<input type=hidden name='2z' value='98052'>
<input type=hidden name='2y' value='US'>
<input type=hidden name='lr' value='2'>
<input type=hidden name='x' value='75'>
<input type=hidden name='y' value='8'>
<br>
<br>
<input type=submit value='Submit'>
</form>
<p>In a follow up article to this I'll approach the same concept using databases
for this process.</p>
Oryginalne komentarze (3)
Odzyskane z Wayback Machine