I'm translating a WinAPI of Native WiFi API to delphi, and I wrote a rotine inside a button to test, and it worked. But is not working inside a procedure, and I can't figure out WHY because I just Copied and Pasted.
This exactly code is WORKING
procedure TForm1.Button1Click(Sender: TObject);
var
nVersion:DWORD;
clientHandle:HWND;
return:DWORD;
size:DWORD;
pdata:pWLAN_HOSTED_NETWORK_CONNECTION_SETTINGS;
vtype:pWLAN_OPCODE_VALUE_TYPE;
pfail:PWLAN_HOSTED_NETWORK_REASON;
ssid:array[0..DOT11_SSID_MAX_LENGTH] of UCHAR;
name:String;
begin
return:=WlanOpenHandle(2,nil,@nVersion,@clientHandle);
return:=WlanHostedNetworkQueryProperty(clientHandle,wlan_hosted_network_opcode_connection_settings,@size,@pdata,@vtype,nil);
if return<>ERROR_SUCCESS then ShowMessage('Returned ERROR '+ IntToStr(return));
name:=Edit1.Text;
StrCopy(@pdata.hostedNetworkSSID.ucSSID, @name[1]);
pdata.hostedNetworkSSID.uSSIDLength:=Length(name);
return:=WlanOpenHandle(2,nil,@nVersion,@clientHandle);
return:=WlanHostedNetworkSetProperty(clientHandle,wlan_hosted_network_opcode_connection_settings,size,pdata,@pfail,nil);
if return<>ERROR_SUCCESS then ShowMessage('Returned ERROR '+ IntToStr(return));
end;
but if a put this code inside another procedure to make the code clean and call the function inside a button, it does NOT WORK!
procedure setSSID(text:String);
var
nVersion:DWORD;
clientHandle:HWND;
return:DWORD;
size:DWORD;
pdata:pWLAN_HOSTED_NETWORK_CONNECTION_SETTINGS;
vtype:pWLAN_OPCODE_VALUE_TYPE;
pfail:PWLAN_HOSTED_NETWORK_REASON;
ssid:array[0..DOT11_SSID_MAX_LENGTH] of UCHAR;
name:String;
begin
return:=WlanOpenHandle(2,nil,@nVersion,@clientHandle);
return:=WlanHostedNetworkQueryProperty(clientHandle,wlan_hosted_network_opcode_connection_settings,@size,@pdata,@vtype,nil);
if return<>ERROR_SUCCESS then ShowMessage('Returned ERROR' + IntToStr(return)); <<<<<< RETURNING ERROR 1734
name:=text;
StrCopy(@pdata.hostedNetworkSSID.ucSSID, @name[1]);
pdata.hostedNetworkSSID.uSSIDLength:=Length(name);
return:=WlanOpenHandle(2,nil,@nVersion,@clientHandle);
return:=WlanHostedNetworkSetProperty(clientHandle,wlan_hosted_network_opcode_connection_settings,size,pdata,@pfail,nil);
if return<>ERROR_SUCCESS then ShowMessage('Returned ERROR '+ IntToStr(return));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
setSSID('test');
end;
I get error 1734
on highlighted line and after I get Access violation at address 004084D0 in module Project1.exe. Write of address 000000000.
I just can't find any problem because is exactly the same code!
function WlanHostedNetworkQueryProperty(
hClientHandle:HANDLE;
OpCode:WLAN_HOSTED_NETWORK_OPCODE;
pdwDataSize:PDWORD;
ppvData:PPVOID;
pWlanOpcodeValueType:PWLAN_OPCODE_VALUE_TYPE;
pvReserved:PVOID
):DWORD; stdcall; external 'Wlanapi.dll';
function WlanHostedNetworkSetProperty(
hClientHandle:HANDLE;
OpCode:WLAN_HOSTED_NETWORK_OPCODE;
dwDataSize:DWORD;
pvData:PVOID;
pFailReason:PWLAN_HOSTED_NETWORK_REASON;
pvReserved:PVOID
):DWORD; stdcall; external 'Wlanapi.dll';
** OBS: if I change the line name:=text;
to name:='hello';
it works! And I still don't know why, probably something related to pointers and memory overflow.**