As far as I can see, your code should be identical to the MSDN sample. However, as Code says, the MSDN pattern looks a bit scared. Indeed, it seems to me that the MSDN sample only works by chance.
Note that the comment in this code that reads:
Then it allocates space in pProviders to store one pointer. However, the value contained in pProviders does matter. It cannot be NULL . In Delphi code, you actually nullify that memory twice. Once with AllocMem and once with ZeroMemory . If you simply modify the Delphi code so that the content of the providers non-zero, then the Delphi code will start working.
Here is a very simple project that accurately illustrates what is happening:
program _EnumerateTraceGuidsFaultDemo; {$APPTYPE CONSOLE} function EnumerateTraceGuids( GuidPropertiesArray: Pointer; PropertyArrayCount: Cardinal; var GuidCount: Cardinal): Cardinal; stdcall; external 'advapi32.dll'; var providers: Pointer; providerCount: LongWord; registeredProviderCount: LongWord; res: LongWord; begin providerCount := 0; registeredProviderCount := 0; providers := AllocMem(SizeOf(Pointer));//zeroises memory res := EnumerateTraceGuids(providers, providerCount, registeredProviderCount); Writeln(res);//outputs 87 PInteger(providers)^ := 1; res := EnumerateTraceGuids(providers, providerCount, registeredProviderCount); Writeln(res);//outputs 234 Readln; end.
So, I think this explains the problem, but I would solve it more fully. I will move on to the next step of your work and declare EnumerateTraceGuids completely using real Delphi equivalent to the TRACE_GUID_PROPERTIES structure.
I would probably write code like this:
program _EnumerateTraceGuids; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, Windows; type PTraceGuidProperties = ^TTraceGuidProperties; TTraceGuidProperties = record Guid: TGUID; GuidType: ULONG; LoggerId: ULONG; EnableLevel: ULONG; EnableFlags: ULONG; IsEnable: Boolean; end; function EnumerateTraceGuids( var GuidPropertiesArray: PTraceGuidProperties; PropertyArrayCount: ULONG; var GuidCount: ULONG ): ULONG; stdcall; external 'advapi32.dll'; function GetRegisteredProviderCount: ULONG; var provider: TTraceGuidProperties; pprovider: PTraceGuidProperties; providerCount: LongWord; registeredProviderCount: ULONG; res: ULONG; begin providerCount := 0; pprovider := @provider; res := EnumerateTraceGuids(pprovider, providerCount, registeredProviderCount); if (res<>ERROR_MORE_DATA) and (res<>ERROR_SUCCESS) then RaiseLastOSError; Result := registeredProviderCount; end; var i: Integer; provider: TTraceGuidProperties; pprovider: PTraceGuidProperties; providers: array of TTraceGuidProperties; pproviders: array of PTraceGuidProperties; providerCount: ULONG; registeredProviderCount: ULONG; res: ULONG; begin providerCount := GetRegisteredProviderCount; SetLength(providers, providerCount); SetLength(pproviders, providerCount); for i := 0 to providerCount-1 do pproviders[i] := @providers[i]; res := EnumerateTraceGuids(pproviders[0], providerCount, registeredProviderCount); if res<>ERROR_SUCCESS then RaiseLastOSError;
Instead of trying to be too cute in GetRegisteredProviderCount , I passed a pointer to the real TRACE_GUID_PROPERTIES .