Author: Tomas Rutkauskas
I have a very simple piece of test code to generate and display a GUID on the
screen every time a user clicks on a button on a form. When I compile the program
and run the executable on my Windows 2000 machine, I get a unique GUID every time I
click the button, which is what I expect from the documentation. However, when I
run the same executable on any Windows 98 SE machine, and even a Windows NT 4.0
Server (with SP5), it simply generates the exact same GUID over, and over, and over
again.
Answer:
1 Using RAW API:
2 3 { ... }4 var5 Form1: TForm1;
6 UuidCreateFunc: function(var guid: TGUID): HResult; stdcall;
7 8 implementation9 10 {$R *.DFM}11 12 uses13 ComObj;
14 15 procedure TForm1.Button1Click(Sender: TObject);
16 var17 hr: HRESULT;
18 m_TGUID: TGUID;
19 handle: THandle;
20 begin21 handle := LoadLibrary('RPCRT4.DLL');
22 @UuidCreateFunc := GetProcAddress(Handle, 'UuidCreate');
23 hr := UuidCreateFunc(m_TGUID);
24 if failed(hr) then25 RaiseLastWin32Error;
26 ShowMessage(GUIDToString(m_TGUID));
27 end;
28 29 //With WIN2K support:30 31 { ... }32 var33 Form1: TForm1;
34 UuidCreateFunc: function(var guid: TGUID): HResult; stdcall;
35 36 implementation37 38 {$R *.DFM}39 40 uses41 ComObj;
42 43 procedure TForm1.Button1Click(Sender: TObject);
44 var45 hr: HRESULT;
46 m_TGUID: TGUID;
47 handle: THandle;
48 WinVer: _OSVersionInfoA;
49 begin50 handle := LoadLibrary('RPCRT4.DLL');
51 WinVer.dwOSVersionInfoSize := sizeof(WinVer);
52 getversionex(WinVer);
53 if WinVer.dwMajorVersion >= 5 then{Windows 2000 }54 @UuidCreateFunc := GetProcAddress(Handle, 'UuidCreateSequential')
55 else56 @UuidCreateFunc := GetProcAddress(Handle, 'UuidCreate');
57 hr := UuidCreateFunc(m_TGUID);
58 if failed(hr) then59 RaiseLastWin32Error;
60 ShowMessage(GUIDToString(m_TGUID));
61 end;