Recent

Author Topic: [SOLVED] Getting error (87) when calling DhcpRequestParams  (Read 1260 times)

tboege

  • New Member
  • *
  • Posts: 16
[SOLVED] Getting error (87) when calling DhcpRequestParams
« on: October 17, 2022, 02:22:21 am »
I am trying to get me head around some relatively simple api-calls. But I cannot really succeed.
When ever I call the DhcpRequestParams in the routine below, I get status=87 (=ERROR_INVALID_PARAMETER), which should refer to AdapterName longer than 256 chars according to https://learn.microsoft.com/en-us/windows/win32/api/dhcpcsdk/nf-dhcpcsdk-dhcprequestparams.

Can  you see, what I am doing wrong?


Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Adaptername: PWCHAR;
  4.   SendParams, RecdParams: DHCPCAPI_PARAMS_ARRAY;
  5.   option15param: DHCPCAPI_PARAMS;
  6.   buffer: pbyte;
  7.   size, Status: DWORD;
  8.  
  9. begin
  10.   size := 1024;
  11.   GetMem(buffer, size);
  12.   Adaptername := '{C90A9165-BE89-49AC-B79C-B2F6D5DF7B91}';
  13.   option15param.Flags := 0;
  14.   option15param.OptionId := OPTION_DOMAIN_NAME;
  15.   option15param.IsVendor := False;
  16.   option15param.Data := nil;
  17.   option15param.nBytesData := 0;
  18.   RecdParams.nParams := 1;
  19.   RecdParams.Params := @option15param;
  20.   SendParams.Params := nil;
  21.   SendParams.nParams := 0;
  22.  
  23.  
  24.  
  25.   Status := DhcpRequestParams(DHCPCAPI_REQUEST_SYNCHRONOUS, nil, Adaptername,
  26.     nil, SendParams, RecdParams, buffer, @size, nil);
  27.   Memo1.Lines.Add(StrPas(PChar(buffer)));
  28.   Button1.Caption := IntToStr(Status);
  29.  
  30. end;
  31.  
« Last Edit: October 19, 2022, 09:38:02 pm by tboege »

tboege

  • New Member
  • *
  • Posts: 16
Re: Getting error (87) when calling DhcpRequestParams
« Reply #1 on: October 17, 2022, 01:48:12 pm »
I figured out, the code works as expected, when I compile for 32bit windows - but I only get the status=87 when compiling for 64 bit

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Getting error (87) when calling DhcpRequestParams
« Reply #2 on: October 19, 2022, 02:36:49 am »
Can  you see, what I am doing wrong?
Could there be an incorrect adapter name?
I don't use DHCP, but this code is executed without errors (gives, of course, an empty domain name):
Code: Pascal  [Select][+][-]
  1. uses Windows, jwadhcpcsdk, jwaiptypes, jwaiphlpapi;
  2.  
  3. function RetrieveDomaintName(AdapterName: PWideChar): string;
  4. var
  5.   Buffer: array[0..1000] of Byte;
  6.   DomainNameParams: DHCPCAPI_PARAMS;
  7.   RequestParams: DHCPCAPI_PARAMS_ARRAY;
  8.   SendParams: DHCPCAPI_PARAMS_ARRAY;
  9.   Size: DWORD = SizeOf(Buffer);
  10.   Status: DWORD;
  11. begin
  12.   Result := '';
  13.   ZeroMemory(@DomainNameParams, SizeOf(DomainNameParams));
  14.   DomainNameParams.Flags := OPTION_DOMAIN_NAME;
  15.   ZeroMemory(@SendParams, SizeOf(SendParams));
  16.   RequestParams.nParams := 1;
  17.   RequestParams.Params := @DomainNameParams;
  18.   Status := DhcpRequestParams(DHCPCAPI_REQUEST_SYNCHRONOUS, nil,
  19.     AdapterName, nil, SendParams, RequestParams, @Buffer, @Size, nil);
  20.   if Status <> 0 then
  21.     RaiseLastOSError(Status);
  22.   SetString(Result, PChar(DomainNameParams.Data), DomainNameParams.nBytesData);
  23. end;
  24.  
  25. procedure TForm1.Button1Click(Sender: TObject);
  26. var
  27.   Version: DWORD;
  28.   BufSize: ULONG;
  29.   P, Buffer: PIP_ADAPTER_INFO;
  30.   AdapterName: UnicodeString;
  31. begin
  32.   if DhcpCApiInitialize(Version) <> 0 then
  33.     Exit;
  34.   try
  35.     BufSize := 0;
  36.     if GetAdaptersInfo(nil, BufSize) <> ERROR_BUFFER_OVERFLOW then
  37.       RaiseLastOSError;
  38.     GetMem(Buffer, BufSize);
  39.     try
  40.       if GetAdaptersInfo(Buffer, BufSize) <> 0 then
  41.         RaiseLastOSError;
  42.       P := Buffer;
  43.       repeat
  44.         AdapterName := P^.AdapterName;
  45.         Memo1.Append(RetrieveDomaintName(Pointer(AdapterName)));
  46.         P := P^.Next;
  47.       until P = nil;
  48.     finally
  49.       FreeMem(Buffer);
  50.     end;
  51.   finally
  52.     DhcpCApiCleanup;
  53.   end;
  54. end;

tboege

  • New Member
  • *
  • Posts: 16
Re: Getting error (87) when calling DhcpRequestParams
« Reply #3 on: October 19, 2022, 09:37:07 pm »
I have tried you code. It seems to work in both 32-bit and 64-bit. I had to replace line 14
with

Code: Pascal  [Select][+][-]
  1. DomainNameParams.Flags := 0;
  2. DomainNameParams.OptionId := OPTION_DOMAIN_NAME;
  3.  
   
(That could be the reason, why you didnt get a domain name)

I have compared your solution with my code, and found, that my cod works, if I replace the uses:
  JwaWindows;
  with
 jwadhcpcsdk;

Thank you, very much - if you have an explanation, why jwaWindows not works in 64bit, but works in 32bit, but have to be replaced with jwadhcpcsdk , I would like to know.
« Last Edit: October 19, 2022, 09:42:56 pm by tboege »

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Getting error (87) when calling DhcpRequestParams
« Reply #4 on: October 19, 2022, 10:03:46 pm »
I had to replace line 14 with
Thanks for the correction

Quote
(That could be the reason, why you didnt get a domain name)
I don't get it because I don't use it. I have static and no domain.

Quote
Thank you, very much - if you have an explanation, why jwaWindows not works in 64bit, but works in 32bit, but have to be replaced with jwadhcpcsdk , I would like to know.
The jwaWindows unit is a container for all jwa units, including jwadhcpcsdk. I think the problem is that one of the included units activates the packing of records, which makes the structure DHCPCAPI_PARAMS not 32 bytes, but only 24.

tboege

  • New Member
  • *
  • Posts: 16
Re: [SOLVED] Getting error (87) when calling DhcpRequestParams
« Reply #5 on: October 20, 2022, 10:39:35 am »
I think you are rigth. I have checked with SizeOf(DHCPCAPI_PARAMS). I have also tried to add {$PACKRECORDS  C} before the definition af the record i jwadhcpcsdk and recompile Jedi.
With that addition, it works even when I use JwaWindows. But since I don't know, if it gives any sideeffects, I will just stick to jwadhcpcsdk.

Should this be reported as a bug to the maintainers of Jedi (if there ara any)?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: [SOLVED] Getting error (87) when calling DhcpRequestParams
« Reply #6 on: October 21, 2022, 04:09:04 pm »
Should this be reported as a bug to the maintainers of Jedi (if there ara any)?

It should be reported as a bug on our own bug tracker, because the Jedi units have been imported from the Jedi project a long time ago and were modified since then.

 

TinyPortal © 2005-2018