delphi2010调用wlan api的问题

lght 2011-09-30 11:36:33
在delphi2010里调用wlan的api函数,网上没找到现成的声明,自己翻译了一部分,调用函数不报错,函数返回结果也对。但传入的参数返回结果不对。
各位帮忙看看。

以下是部分声明

_WLAN_INTERFACE_STATE = (
wlan_interface_state_not_ready = 0,
wlan_interface_state_connected = 1,
wlan_interface_state_ad_hoc_network_formed = 2,
wlan_interface_state_disconnecting = 3,
wlan_interface_state_disconnected = 4,
wlan_interface_state_associating = 5,
wlan_interface_state_discovering = 6,
wlan_interface_state_authenticating = 7
);
//} WLAN_INTERFACE_STATE, *PWLAN_INTERFACE_STATE;
WLAN_INTERFACE_STATE = _WLAN_INTERFACE_STATE;
PWLAN_INTERFACE_STATE = ^_WLAN_INTERFACE_STATE;

_WLAN_INTERFACE_INFO = packed record
InterfaceGuid: TGUID;
strInterfaceDescription: array[0..255] of WideChar;
isState: WLAN_INTERFACE_STATE;
end;
//} WLAN_INTERFACE_INFO, *PWLAN_INTERFACE_INFO;
WLAN_INTERFACE_INFO = _WLAN_INTERFACE_INFO;
PWLAN_INTERFACE_INFO = ^_WLAN_INTERFACE_INFO;

_WLAN_INTERFACE_INFO_LIST = packed record
dwNumberOfItems: DWORD;
dwIndex: DWORD;
InterfaceInfo: PWLAN_INTERFACE_INFO;
end;
//} WLAN_INTERFACE_INFO_LIST, *PWLAN_INTERFACE_INFO_LIST;
WLAN_INTERFACE_INFO_LIST = _WLAN_INTERFACE_INFO_LIST;
PWLAN_INTERFACE_INFO_LIST = ^_WLAN_INTERFACE_INFO_LIST;
PPWLAN_INTERFACE_INFO_LIST = ^PWLAN_INTERFACE_INFO_LIST;

function WlanOpenHandle(dwClientVersion: DWORD; pReserved: Pointer;
pdwNegotiatedVersion: PDWORD; phClientHandle: PHandle
): DWORD; stdcall;

function WlanCloseHandle(hClientHandle: THandle;
pReserved: Pointer): DWORD; stdcall;

function WlanSetProfile(hClientHandle: THandle; const pInterfaceGuid: PGUID;
dwFlags: DWORD; strProfileXml: LPCWSTR; strAllUserProfileSecurity: LPCWSTR;
bOverwrite: BOOL; pReserved: Pointer; var pdwReasonCode: DWORD
): DWORD; stdcall;

function WlanEnumInterfaces(hClientHandle: THandle; pReserved: Pointer;
ppInterfaceList: PPWLAN_INTERFACE_INFO_LIST): DWORD; stdcall;

function WlanGetProfile(hClientHandle: THandle;
const pInterfaceGuid: PGUID; strProfileName: LPCWSTR;
pReserved: Pointer; var pstrProfileXml: PWideChar;
var pdwFlags: DWORD; var pdwGrantedAccess: DWORD): DWORD; stdcall;


以下是调用
WlanEnumInterfaces的参数pList.InterfaceInfo.InterfaceGuid不对,我用网上下的一个c#的代码调试了,得到的是正确的。

var
dwVer, dwRet, dwFlag, dwAccess: DWORD;
phClient: THandle;
pList: PWLAN_INTERFACE_INFO_LIST;
pInfo: WLAN_INTERFACE_INFO;
i: Integer;
profile: PWideChar;
begin
dwRet := WlanOpenHandle(1, nil, @dwVer, @phClient);
if dwRet <> ERROR_SUCCESS then
Exit;

pList := nil;
dwRet := WlanEnumInterfaces(phClient, nil, @pList);
if dwRet <> ERROR_SUCCESS then
begin
OutputDebugString(PChar(IntToStr(dwRet)));
Exit;
end;
for i := 0 to pList.dwNumberOfItems - 1 do
begin
pInfo := pList.InterfaceInfo^;
lst1.Items.Add(GUIDToString(pInfo.InterfaceGuid));

dwFlag := 0;
dwAccess := 0;
profile := nil;
dwRet := WlanGetProfile(phClient, @pInfo.InterfaceGuid, 'vlan', nil,
profile, dwFlag, dwAccess);
if dwRet = ERROR_SUCCESS then
begin
lst1.Items.Add(profile);
end;
Inc(pList.InterfaceInfo);
end;

WlanCloseHandle(phClient, nil);
...全文
547 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lght 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 m617105 的回复:]

类型翻译没看出来错误。
pList.InterfaceInfo.InterfaceGuid是一部分不正确还是全都不正确?
[/Quote]

pList参数的InterfaceInfo整个都不对,全部是0,dwNumberOfItems是对的
浩南_哥 2011-09-30
  • 打赏
  • 举报
回复
类型翻译没看出来错误。
pList.InterfaceInfo.InterfaceGuid是一部分不正确还是全都不正确?
jingtuzhong 2011-09-30
  • 打赏
  • 举报
回复
xuexi
lght 2011-09-30
  • 打赏
  • 举报
回复
改正一下
'<keyType>networkKey</keyType>'

改为
'<keyType>passPhrase</keyType>'

浩南_哥 2011-09-30
  • 打赏
  • 举报
回复
不错 收藏一下
lght 2011-09-30
  • 打赏
  • 举报
回复
感谢m617105
lght 2011-09-30
  • 打赏
  • 举报
回复
搞定了,结贴


_WLAN_INTERFACE_STATE = (
wlan_interface_state_not_ready = 0,
wlan_interface_state_connected = 1,
wlan_interface_state_ad_hoc_network_formed = 2,
wlan_interface_state_disconnecting = 3,
wlan_interface_state_disconnected = 4,
wlan_interface_state_associating = 5,
wlan_interface_state_discovering = 6,
wlan_interface_state_authenticating = 7
);
//} WLAN_INTERFACE_STATE, *PWLAN_INTERFACE_STATE;
WLAN_INTERFACE_STATE = _WLAN_INTERFACE_STATE;
PWLAN_INTERFACE_STATE = ^_WLAN_INTERFACE_STATE;

_WLAN_INTERFACE_INFO = packed record
InterfaceGuid: TGUID;
strInterfaceDescription: array[0..255] of WCHAR;
isState: WLAN_INTERFACE_STATE;
end;
//} WLAN_INTERFACE_INFO, *PWLAN_INTERFACE_INFO;
WLAN_INTERFACE_INFO = _WLAN_INTERFACE_INFO;
PWLAN_INTERFACE_INFO = ^_WLAN_INTERFACE_INFO;

_WLAN_INTERFACE_INFO_LIST = packed record
dwNumberOfItems: DWORD;
dwIndex: DWORD;
InterfaceInfo: array[0..SizeOf(_WLAN_INTERFACE_INFO)] of WLAN_INTERFACE_INFO;
end;
//} WLAN_INTERFACE_INFO_LIST, *PWLAN_INTERFACE_INFO_LIST;
WLAN_INTERFACE_INFO_LIST = _WLAN_INTERFACE_INFO_LIST;
PWLAN_INTERFACE_INFO_LIST = ^_WLAN_INTERFACE_INFO_LIST;
PPWLAN_INTERFACE_INFO_LIST = ^PWLAN_INTERFACE_INFO_LIST;

function WlanOpenHandle(dwClientVersion: DWORD; pReserved: Pointer;
pdwNegotiatedVersion: PDWORD; phClientHandle: PHandle
): DWORD; stdcall;

function WlanCloseHandle(hClientHandle: THandle;
pReserved: Pointer): DWORD; stdcall;

function WlanSetProfile(hClientHandle: THandle; const pInterfaceGuid: PGUID;
dwFlags: DWORD; strProfileXml: LPCWSTR; strAllUserProfileSecurity: LPCWSTR;
bOverwrite: BOOL; pReserved: Pointer; var pdwReasonCode: DWORD
): DWORD; stdcall;

function WlanEnumInterfaces(hClientHandle: THandle; pReserved: Pointer;
ppInterfaceList: PPWLAN_INTERFACE_INFO_LIST): DWORD; stdcall;

function WlanGetProfile(hClientHandle: THandle;
const pInterfaceGuid: PGUID; strProfileName: LPCWSTR;
pReserved: Pointer; pstrProfileXml: PPWideChar;
pdwFlags: PDWORD; pdwGrantedAccess: PDWORD): DWORD; stdcall;

procedure WlanFreeMemory(pMemory: Pointer); stdcall;



//获取一个无线网络的设置
function GetWifiProfile(AName: PWideChar): string;
var
dwVer, dwRet, dwFlag: DWORD;
dwAccess: DWORD;
phClient: THandle;
pList: PWLAN_INTERFACE_INFO_LIST;
pInfo: WLAN_INTERFACE_INFO;
i: Integer;
profile: PWideChar;
begin
dwRet := WlanOpenHandle(1, nil, @dwVer, @phClient);
if dwRet <> ERROR_SUCCESS then
Exit;

pList := nil;
dwRet := WlanEnumInterfaces(phClient, nil, @pList);
if dwRet <> ERROR_SUCCESS then
begin
OutputDebugString(PChar(IntToStr(dwRet)));
Exit;
end;
for i := 0 to pList.dwNumberOfItems - 1 do
begin
pInfo := pList.InterfaceInfo[i];
dwFlag := 0;
dwAccess := 0;
profile := nil;
dwRet := WlanGetProfile(phClient, @pInfo.InterfaceGuid, AName, nil,
@profile, @dwFlag, @dwAccess);
if dwRet = ERROR_SUCCESS then
Result := profile;
end;
WlanFreeMemory(pList);

WlanCloseHandle(phClient, nil);
end;

{
添加一个无线网络
AName:无线网络名称
APass:无线网络密码 长度大于等于8
}
function SetWifiProfile(AName, APass: string): Boolean;
const
CsProfile = '<?xml version="1.0"?>'
+ '<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">'
+ '<name>%s</name>'
+ '<SSIDConfig>'
+ '<SSID>'
+ '<name>%s</name>'
+ '</SSID>'
+ '</SSIDConfig>'
+ '<connectionType>ESS</connectionType>'
+ '<MSM>'
+ '<security>'
+ '<authEncryption>'
+ '<authentication>WPAPSK</authentication>'
+ '<encryption>TKIP</encryption>'
+ '<useOneX>false</useOneX>'
+ '</authEncryption>'
+ '<sharedKey>'
+ '<keyType>networkKey</keyType>'
+ '<protected>false</protected>'
+ '<keyMaterial>%s</keyMaterial>'
+ '</sharedKey>'
+ '</security>'
+ '</MSM>'
+ '</WLANProfile>';
var
dwVer, dwRet, dwReason: DWORD;
phClient: THandle;
profileXml: PWideChar;
pList: PWLAN_INTERFACE_INFO_LIST;
pInfo: WLAN_INTERFACE_INFO;
begin
Result := False;
dwRet := WlanOpenHandle(1, nil, @dwVer, @phClient);
if dwRet <> ERROR_SUCCESS then
Exit;

pList := nil;
dwRet := WlanEnumInterfaces(phClient, nil, @pList);
if dwRet <> ERROR_SUCCESS then
begin
OutputDebugString(PChar(IntToStr(dwRet)));
Exit;
end;
if pList.dwNumberOfItems > 0 then
begin
pInfo := pList.InterfaceInfo[0];
profileXml := PWideChar(Format(CsProfile,
[AName, AName, APass]));
dwRet := WlanSetProfile(phClient, @pInfo.InterfaceGuid, 0, profileXml, nil,
True, nil, dwReason);
Result := dwRet = ERROR_SUCCESS;
end;
WlanFreeMemory(pList);

WlanCloseHandle(phClient, nil);
end;
浩南_哥 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lght 的回复:]

解决一个,还有一个:WlanGetProfile返回错误
[/Quote]
MSDN:
ERROR_ACCESS_DENIED
The caller does not have sufficient permissions. This error is returned if the pstrProfileXml parameter specifies an all-user profile, but the caller does not have read access on the profile.

ERROR_INVALID_HANDLE
A handle is invalid. This error is returned if the handle specified in the hClientHandle parameter was not found in the handle table.

ERROR_INVALID_PARAMETER
A parameter is incorrect. This error is returned if any of the following conditions occur:
hClientHandle is NULL.
pInterfaceGuid is NULL.
pstrProfileXml is NULL.
pReserved is not NULL.

ERROR_NOT_ENOUGH_MEMORY
Not enough storage is available to process this command. This error is returned if the system was unable to allocate memory for the profile.

ERROR_NOT_FOUND
The profile specified by strProfileName was not found.

Other
Various RPC and other error codes. Use FormatMessage to obtain the message string for the returned error.

lght 2011-09-30
  • 打赏
  • 举报
回复
解决一个,还有一个:WlanGetProfile返回错误
lght 2011-09-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 m617105 的回复:]

网上找了两个Delphi的你看看吧
http://195.128.127.177/forum/actualthread.aspx?bid=20&tid=460659&hl=
http://www.sql.ru/Forum/actualthread.aspx?bid=20&tid=463720&hl=
[/Quote]

找到问题了

_WLAN_INTERFACE_INFO_LIST = packed record
dwNumberOfItems: DWORD;
dwIndex: DWORD;
InterfaceInfo: array[0..SizeOf(_WLAN_INTERFACE_INFO)] of WLAN_INTERFACE_INFO;
end;

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧