Ramblings

Delphi Programming Observations

Source Code License About

Tuesday, March 23, 2010

Custom Protocol Handler in Delphi

I have wanted to look into creating a custom protocol handler to plug into a TWebBrowser with Delphi for a while, called an Asynchronous Pluggable Protocol. I have seen a few implementations, but they were all quite old, and none did exactly what I wanted, which is to be able to create a temporary APP, which is local to the application and self contained.


(more…)

posted by Jason at 7:11 pm  

Saturday, September 5, 2009

Updated SetVersion.exe to set File Version info in RES or EXE

My first version of the SetVersion.exe tool to update the Version Information of a Delphi executable allowed you to set the version info stored in a Delphi Resource file (*.RES), which is the main place Delphi stores it.  When you perform a build in Delphi, the version info in the Resource File will override anything defined in a *.BDSPROJ or *.DPROJ for the resulting binary (*.EXE, *.DLL).

Now, SetVersion.exe will allow you to set the Version Information on an EXE file after it has been built. See latest code at github.com/jasonpenny/democode

posted by Jason at 12:17 am  

Friday, July 31, 2009

SQL Function to Get NVARCHAR FROM UTF-8 Stored in VARCHAR

I (am forced to) use StarTeam at work. I think it’s terrible as a Source Code Management system, but I think the Customer Relationship Management aspects (Change Requests, Tasks, Work Records, etc.) work ok. The java GUI client, however, is slow and lacking robust reporting. We certainly have a lot of information stored in StarTeam, so it would take a really good reason to change to another tool.

So, we have a Delphi program to directly query the SQL views in the MS SQL Server database to perform reporting how we want.  Early on, we ran into some strange characters coming out of the database (things like the leaning quotes that MS Word likes to automatically change).  So I found out that StarTeam was storing UTF-8 in the VARCHAR and TEXT fields, basically treating them like byte arrays.  In Delphi 2009, I can just use UTF8ToUnicodeString( RawByteString(queryResultString) ); and the output seems to match what is displayed in StarTeam.

Now I’m learning ASP.NET MVC and using LINQ to SQL to read from the StarTeam SQL views. After some searching, the only information that I found was advice against storing UTF-8 in VARCHAR fields, but of course I can’t change the database.

I didn’t want to have to decode the UTF-8 any time I wanted to read the fields from the Model, and I actually did not find a way to decode them properly in C# using LINQ to SQL.

So in my searching, I came across some JavaScript to change ASCII to UTF-8 and back. I then realized that I might be able to translate that to TSQL, to have my own SQL View return NVARCHARs to LINQ to SQL.

And it seems to work:

CREATE FUNCTION dbo.UTF8_TO_NVARCHAR(@in VarChar(MAX))
	RETURNS NVarChar(MAX)
AS
BEGIN
	DECLARE @out NVarChar(MAX), @i int, @c int, @c2 int, @c3 int, @nc int

	SELECT @i = 1, @out = ''

	WHILE (@i <= Len(@in))
	BEGIN
		SET @c = Ascii(SubString(@in, @i, 1))

		IF (@c < 128)
		BEGIN
			SET @nc = @c
			SET @i = @i + 1
		END
		ELSE IF (@c > 191 AND @c < 224)
		BEGIN
			SET @c2 = Ascii(SubString(@in, @i + 1, 1))

			SET @nc = (((@c & 31) * 64 /* << 6 */) | (@c2 & 63))
			SET @i = @i + 2
		END
		ELSE
		BEGIN
			SET @c2 = Ascii(SubString(@in, @i + 1, 1))
			SET @c3 = Ascii(SubString(@in, @i + 2, 1))

			SET @nc = (((@c & 15) * 4096 /* << 12 */) | ((@c2 & 63) * 64 /* << 6 */) | (@c3 & 63))
			SET @i = @i + 3
		END

		SET @out = @out + NChar(@nc)
	END
	RETURN @out
END
GO

I used VARCHAR(Max) so that this Function will work with TEXT fields, as well as VARCHAR fields. I believe VARCHAR(Max) was introduced in SQL Server 2005; it allows you to have a VARCHAR variable longer than 8000 characters.

posted by Jason at 10:15 pm  

Friday, May 8, 2009

Program To Set Delphi Resource Version Info from commandline

With Delphi’s decision to move to using MSBuild, building Delphi projects from the commandline became a lot easier, but it lacks a way to set the Version Info on the resultant EXE.

Using the code from the XN Resource Editor, it was pretty easy to write a commandline program to change the Version Info stored in a Delphi Resource file, SetVersion.exe. The source code for XN Resource Editor is available at http://www.wilsonc.demon.co.uk/delphi.htm. I found one little problem with the Delphi 2009 version, the RES files were being corrupted after running the SetVersion.exe program, but simply changing a Char and PChar to Byte and PByte fixed it.

Calling the program before calling MSBuild (or DCC32 if you don’t use the latest Delphi) will allow you to set the Version Info, which will be compiled into the EXE.

See the github repository for the source code (SetVersion/ directory). The code only compiles with Delphi2009 (I tried with Delphi 2006 and it failed), but I think it should only take getting the correct version of the XN Resource Editor’s source code files at http://www.wilsonc.demon.co.uk/delphi.htm and dropping them into Others/ColinWilson/.

posted by Jason at 11:49 pm  

Friday, May 1, 2009

Using DLLs stored as Resources in Delphi programs

There are a few DLLs which my Delphi programs interface to, which were developed in other languages. Using LoadLibrary() and GetProcAddress(), your code can avoid crashing by checking for the existance of the DLL. This also allows storing the DLL as a resource, inside the executable, which you can extract to the filesystem and before calling LoadLibrary() and GetProcAddress().

Extracting the DLL to the filesystem works, but there is a better way: BTMemoryModule. BTMemoryModule allows you to extract the DLL to a TMemoryStream, and use it as a DLL, with BTMemoryLoadLibrary and BTMemoryGetProcAddress.
(more…)

posted by Jason at 7:31 pm  

Friday, April 3, 2009

Git and Delphi

At work, we use Borland Starteam for code management and bug/change tracking. Lately, I have been using Git for code management, in addition. I really like having the benefits of Git, while not forcing the other developers to change how they work. Git can track all my little code changes, and then I checkin to Starteam to share with the other developers

I keep a Git bash (part of mSysGit) shell open all day while I’m working. It took a little while to get used to looking at Unix-style diffs (comparison of the last version of a file and what is currently on the disk), but now I definitely prefer it to working in Starteam or with TortoiseSVN which force you to use the mouse. Also Starteam has the nasty habit of not being able to determine the Status of a file, constantly saying Unknown even after an explicit Refresh (F5) and right click -> Update Status, instead of realizing a file is Current or Modified.

Delphi and Git Bash
(more…)

posted by Jason at 11:08 pm  

Saturday, February 7, 2009

JQuery and TableSorter in a TWebBrowser

JQueryTableSorterTest Screenshot

As a follow up to my first post on JQuery in a TWebBrowser, I wanted to add the JQuery plugin TableSorter to an HTML report in order to add interactive sorting. Again, I wanted the program to be fully self-contained, so I didn’t want to add any <script> tags referring to temp files or anything like that.

Download the Delphi project which injects JQuery, JQuery plugin cssRule and JQuery plugin TableSorter. I tested that it works with Delphi 2006 and Delphi 2009.
(more…)

posted by Jason at 1:47 am  

Sunday, January 11, 2009

Google Maps in a TWebBrowser from Delphi: Directions

Screenshot

The GoogleMaps API has a GDirections object which can be used to retrieve directions between points.  Full JavaScript applications are possible, but interaction with Delphi allows further possibilities.  Previous demo programs showed how to call JavaScript from Delphi, this demo also allows the JavaScript to call Delphi functions (using JavaScript’s external object, explained later).

The demo program allows you to enter two or three addresses, and will retrieve the directions with the GoogleMaps API.  The directions will be sent to Delphi from JavaScript, and they will be displayed in a TListBox.  Clicking on an item in the TListBox will show the map “blowup” (a zoomed in box for a step, as seen in the screenshot above).

(more…)

posted by Jason at 12:04 am  

Tuesday, November 25, 2008

Google Maps in a TWebBrowser from Delphi, Part 2

I received an email about my first Google Maps in a TWebBrowser post asking how to remove the white border around the map, in order to let the GoogleMap fill the whole area of the TWebBrowser.

The white border is simply the default margin of the body of the document. Adding style=”margin:0″ to the <body> tag will push the map to the upper left (so changing the original example, the new line will be “<body onload=”load()” onunload=”GUnload()” style=”margin:0″>”).

Allowing the map to fill the whole TWebBrowser is a little more involved.
(more…)

posted by Jason at 1:09 pm  

Friday, November 21, 2008

JQuery in a TWebBrowser, in a self contained Delphi app

I wanted to find a way to use JQuery from Delphi on a page in a TWebBrowser, without requiring downloading over the internet, nor from the filesystem.

Download the Delphi 2009 project.

It turns out you can just execute the whole contents of the JQuery javascript file.
(more…)

posted by Jason at 9:42 pm  
Next Page »

Powered by WordPress