Showing posts with label Tiburon. Show all posts
Showing posts with label Tiburon. Show all posts

Thursday, September 25, 2008

Delphi 2009 - Exit

Exit is now very similar to the C++ return keyword where you can supply an optional parameter that is assigned to a functions return value. For example:

function DoStuff(Value: Boolean): Integer;
begin
  if Value then
    Exit(1);

  Result := 0;
end;

Tuesday, September 23, 2008

Delphi 2009 - Unicode in Type Libraries

If you've opened up the Tools | Options in Delphi 2009 and taken a look at C++ Type Library Options and Delphi Type Library Options you'll notice the options are relatively the same as previous version but there are a few additions that might not be all that easy to understand. Here's a screen capture of the two dialogs:


Notice the two UTF8 options:

- Store Unicode data as UTF8 in type library
- Check for UTF8 data in type library

I'll explain what these do but first I we need a bit of a back story.

Back when the COM team was working on the new COM features for Tiburon (Delphi 2009 and C++ Builder 2009), we found that ICreateTypeLib, ICreateTypeLib2, ICreateTypeInfo and ICreateTypeInfo2 don't actually support BSTRs even thoough all their string paramters are BSTRs. Somewhere in the writing of the .tlb file the data is narrowed down and the unicode data is lost. After some testing using MIDL 5.01.0164 we found that Microsoft had apparently known about this and worked around it by UTF8 encoding the data and then stuffing it into the BSTR. Then we found that the latest version of MIDL 7.00.0500 produced errors when compiling files with unicode data.

So we wanted to support unicode throughout the product so we added support for UTF8 data in type libraries. The two options read UTF8 data and write UTF8 data. This means you can create unicode identifiers (functions and classes) for use between Delphi and C++Builder 2009 (and apparently MIDL 5.01.0164) but that's about it. I would suggest staying away from unicode identifiers in your Type Libraries.

Friday, September 19, 2008

Delphi and C++Builder 2009 - TApplication is Apartment Threaded

In Delphi 2009 and C++Builder 2009 (AKA Tiburon) by default the main thread of a VCL application will be apartment threaded. Take a look at TApplication.Create for the details. This isn't really a change from previous versions of VCL but the location of the call and how to override it is new. And now VCL calls OleInitialize instead of CoInitialize so there is a slight behavior change.

There may be reasons why a developer won't want this default behavior and the best way of doing this is to create this unit:

unit InitCom;

interface

implementation

uses
  ActiveX;

var
  NeedToUninitialize: Boolean;
initialization
  NeedToUninitialize := Succeeded(CoInitializeEx(nil, COINIT_MULTITHREADED));
finalization
  if NeedToUninitialize then
    CoUninitialize;
end.

And place it in the .dpr as the first unit (it must be before Forms or anything that brings in Controls).

It isn't recommended to run any UI under a multithreaded apartment but this will allow those who need it to change the default threading model.

Here is a blog post by Raymond Chen about why running the UI thread as multithreaded isn't a good idea.