0

Delphi XE8 on Windows 10 Pro

So i use WlanHostedNetworkStartUsing from Native Wifi API to start Hosted Network using this code :

procedure TForm1.btn_1Click(Sender: TObject);
var
  hClient              : THandle;
  dwVersion            : DWORD;
  Result               : DWORD;
  pReason              : Pndu_WLAN_HOSTED_NETWORK_REASON;
  pStatus              : Pndu_WLAN_HOSTED_NETWORK_STATUS;
begin
  if btn_1.Caption = 'start' then
  begin
    Result :=WlanOpenHandle(1, nil, @dwVersion, @hClient);
    if  Result <> ERROR_SUCCESS then
    begin
       ShowMessage(IntToStr(Result));
       Exit;
    end;

    Result:=WlanHostedNetworkStartUsing(hClient,@pReason, nil);
    if  Result <> ERROR_SUCCESS then
    begin
       ShowMessage(IntToStr(Result));
    end else
    begin
      btn_1.Caption := 'stop';
      ShowMessage(HNSToString(pReason^)); // << Im not sure what i am doing with this
    end;
  end else
  begin
    Result:=WlanOpenHandle(1, nil, @dwVersion, @hClient);
    if  Result <> ERROR_SUCCESS then
    begin
       ShowMessage(IntToStr(Result));
       Exit;
    end;

    Result:=WlanHostedNetworkForceStop(hClient,@pReason, nil);
    if  Result <> ERROR_SUCCESS then
    begin
       ShowMessage(IntToStr(Result));
    end else
    begin
      btn_1.Caption := 'start';
    end;
  end;
end;

Then i declare this function to convert pReason value to string :

function TForm1.HNSToString(reason: Tndu_WLAN_HOSTED_NETWORK_REASON): string;
begin
Result := '';
  case reason of
    wlan_hosted_network_reason_success : Result := 'Success';
    wlan_hosted_network_reason_unspecified : Result := 'Unspecified';
    wlan_hosted_network_reason_bad_parameters : Result := 'Bad Parameters';
    wlan_hosted_network_reason_service_shutting_down : Result := 'Service Shutting Down';
    wlan_hosted_network_reason_insufficient_resources : Result := 'Inssuficient Resources';
    wlan_hosted_network_reason_elevation_required : Result := 'Elevation Required';
    wlan_hosted_network_reason_read_only : Result := 'Read Only';
    wlan_hosted_network_reason_persistence_failed : Result := 'Persistance Failed';
    wlan_hosted_network_reason_crypt_error : Result := 'Crypt Error';
    wlan_hosted_network_reason_impersonation : Result := 'Impersonation';
    wlan_hosted_network_reason_stop_before_start : Result := 'Stop Before Start';
    wlan_hosted_network_reason_interface_available : Result := 'Interface Available';
    wlan_hosted_network_reason_interface_unavailable : Result := 'Interface Unavailable';
    wlan_hosted_network_reason_miniport_stopped : Result := 'Miniport Stopped';
    wlan_hosted_network_reason_miniport_started : Result := 'Miniport Started';
    wlan_hosted_network_reason_incompatible_connection_started : Result := 'Incompatible Connection Started';
    wlan_hosted_network_reason_incompatible_connection_stopped : Result := 'Incompatible Connection Stopped';
    wlan_hosted_network_reason_user_action : Result := 'User Action';
    wlan_hosted_network_reason_client_abort : Result := 'Client Abort';
    wlan_hosted_network_reason_ap_start_failed : Result := 'AP Start Failed';
    wlan_hosted_network_reason_peer_arrived : Result := 'Peer Arrived';
    wlan_hosted_network_reason_peer_departed : Result := 'Peer Departed';
    wlan_hosted_network_reason_peer_timeout : Result := 'Peer Timeout';
    wlan_hosted_network_reason_gp_denied : Result := 'GP Denied';
    wlan_hosted_network_reason_service_unavailable : Result := 'Service Unavailable';
    wlan_hosted_network_reason_device_change : Result := 'Device Change';
    wlan_hosted_network_reason_properties_change : Result := 'Properties Change';
    wlan_hosted_network_reason_virtual_station_blocking_use : Result := 'Virtual Station Blocking Use';
    wlan_hosted_network_reason_service_available_on_virtual_station : Result := 'Service Available On Virtual Station';
  end;
end;

But im getting Access Violation blabla when using code above

Is it possible to get integer or string value from WLAN_HOSTED_NETWORK_REASON ?

https://learn.microsoft.com/en-us/windows/win32/api/wlanapi/nf-wlanapi-wlanhostednetworkstartusing

https://learn.microsoft.com/en-us/windows/win32/api/wlanapi/ne-wlanapi-wlan_hosted_network_reason

https://github.com/coolshou/WlanAPI

Thanks in advance :D

  • *"Parameters ... pFailReason: An optional pointer to a value that receives the failure reason..."* Pay attention to the description: *"pointer to a value"*. You're passing the address of a pointer to nothing. Declare a "Tndu_..." in the variable section and pass its address to the function (using @). IOW, change "pReason : Pndu_WLAN_HOSTED_NETWORK_REASON;" to "Reason : Tndu_WLAN_HOSTED_NETWORK_REASON;" and in the call use @Reason. – Sertac Akyuz Oct 11 '19 at 12:14
  • Similar potential problem with pStatus, but since it's not used in the code I can't tell exactly. – Sertac Akyuz Oct 11 '19 at 12:17
  • Additional potential problem with *"Access Violation blabla*", they are seldom blabla but instead information that would help you locate your problem. – Sertac Akyuz Oct 11 '19 at 12:20
  • _change "pReason : Pndu_WLAN_HOSTED_NETWORK_REASON;" to "Reason : Tndu_WLAN_HOSTED_NETWORK_REASON;" and in the call use @Reason._ Nice, this solved my problem, thank you :D – co2thunderboy Oct 12 '19 at 06:52
  • You're welcome! I'd suggest you to have a look at [Rudy's article](http://rvelthuis.de/articles/articles-pointers.html) when you can spare the time. – Sertac Akyuz Oct 12 '19 at 07:06
  • thanks, i'll take a look on it :D – co2thunderboy Oct 12 '19 at 11:37

0 Answers0