C++ Delphi-转换(读取)C++;来自dll调用的以NULL结尾的数组
救命,我快疯了:) 我正在玩TeamSpeak3,有一个dll调用,它会在线返回一组客户端(ID)。我的问题是,它返回的是一个我无法在对象Pascal中读取的结构 SDK手册规定了以下内容:C++ Delphi-转换(读取)C++;来自dll调用的以NULL结尾的数组,c++,delphi,C++,Delphi,救命,我快疯了:) 我正在玩TeamSpeak3,有一个dll调用,它会在线返回一组客户端(ID)。我的问题是,它返回的是一个我无法在对象Pascal中读取的结构 SDK手册规定了以下内容: 这是到目前为止我的Delphi代码 type PPanyID = ^PAnyID; PanyID = ^anyID; anyID = word; // declaration looks like this, some guy called CodeJunkie ported it fr
这是到目前为止我的Delphi代码
type
PPanyID = ^PAnyID;
PanyID = ^anyID;
anyID = word;
// declaration looks like this, some guy called CodeJunkie ported it from C++)
function ts3client_getClientList(serverConnectionHandlerID: uint64; result: PPanyID): longword; cdecl; external CLIENT_DLL {$IFDEF MACOS}name '_ts3client_getClientList'{$ENDIF};
procedure TfrmMain.RequestOnlineClients;
var
clientids : PAnyId;
i : Integer;
begin
error := ts3client_getClientList(FTSServerHandlerID, @clientids);
if (error <> ERROR_ok) then
begin
if (ts3client_getErrorMessage(error, @errormsg) = ERROR_ok) then
begin
LogMsg(Format('Error requesting online clients: %s', [errormsg]));
ts3client_freeMemory(errormsg);
end;
end else
begin
// Put in into some regular Object array here, but how the h......
// Or at least interate the received array of ID's
end;
end;
请问,有人能帮忙吗?你得到的指针指向第一个结果。执行
Inc(clientId)
将继续下一个结果。继续操作,直到到达一个为零(“空”)的anyid
(您还需要保存<代码> clitDS < /C>的原始值,以便您可以将其传递到Free函数,因为文档说明了避免内存泄漏。”< /P> < P> G是真的,这是从C++ DLL返回的数组的唯一读取方法吗?64位也能用吗
procedure TfrmMain.RequestOnlineClients;
var
ids : array of anyID;
pids : PanyID;
aid : anyID;
begin
error := ts3client_getClientList(FTSServerHandlerID, @ids);
if (error <> ERROR_ok) then
begin
if (ts3client_getErrorMessage(error, @errormsg) = ERROR_ok) then
begin
LogMsg(Format('Error requesting online clients: %s', [errormsg]));
ts3client_freeMemory(errormsg);
end;
end else
begin
pids := @ids[0];
while (pids^ <> 0) do
begin
aid := pids^;
LogMsg(format('id %u',[aid]));
inc(pids);
end;
ts3client_freeMemory(@pids^);
end;
end;
过程TfrmMain.requestOnlineClient;
变量
ids:anyID的数组;
pids:PanyID;
援助:anyID;
开始
错误:=ts3client_getClientList(FTSServerHandlerID,@ids);
如果(错误\确定),则
开始
如果(ts3client\u getErrorMessage(error,@errormsg)=error\u ok),则
开始
LogMsg(格式('请求联机客户端时出错:%s',[errormsg]);
TS3客户端空闲内存(errormsg);
终止
结束其他
开始
PID:=@ids[0];
而(pids^0)执行
开始
援助:=pids^;
LogMsg(格式('id%u',[aid]);
公司(pids);
终止
ts3client_freemory(@pids^);
终止
终止
您的错误处理已关闭。当函数调用失败时,不要调用ts3client\u freemory
。文件清楚地说明了这一点。这个API确实定义得很好。我认为你不应该对此抱怨。另外,与其使用pids:=@ids[0]代码>写入pids:=ids代码>谢谢你回复David。1.如果电话打不通,你为什么说我可以自由,请解释一下?2.你凭什么认为我在抱怨?我只是沮丧,因为我还不够好,将C++代码移植到Delphi中——所以如果这里有抱怨,那是关于我的技能。3。PID:=@ids[0];无法转换为pids:=ids;3.哦,我现在明白了。我假设id是PAnyID,因为它与函数声明相匹配。因此,有一个更大的问题,那就是将动态数组的地址传递给函数。这与函数声明不匹配。实际上,这里没有动态数组的位置。所以你还有一段路要走才能解决这个问题。
void showClients(uint64 serverConnectionHandlerID) {
anyID *ids;
anyID ownClientID;
int i;
unsigned int error;
printf("\nList of all visible clients on virtual server %llu:\n", (unsigned long long)serverConnectionHandlerID);
if((error = ts3client_getClientList(serverConnectionHandlerID, &ids)) != ERROR_ok) { /* Get array of client IDs */
printf("Error getting client list: %d\n", error);
return;
}
if(!ids[0]) {
printf("No clients\n\n");
ts3client_freeMemory(ids);
return;
}
/* Get own clientID as we need to call CLIENT_FLAG_TALKING with getClientSelfVariable for own client */
if((error = ts3client_getClientID(serverConnectionHandlerID, &ownClientID)) != ERROR_ok) {
printf("Error querying own client ID: %d\n", error);
return;
}
for(i=0; ids[i]; i++) {
char* name;
int talkStatus;
if((error = ts3client_getClientVariableAsString(serverConnectionHandlerID, ids[i], CLIENT_NICKNAME, &name)) != ERROR_ok) { /* Query client nickname... */
printf("Error querying client nickname: %d\n", error);
break;
}
if(ids[i] == ownClientID) { /* CLIENT_FLAG_TALKING must be queried with getClientSelfVariable for own client */
if((error = ts3client_getClientSelfVariableAsInt(serverConnectionHandlerID, CLIENT_FLAG_TALKING, &talkStatus)) != ERROR_ok) {
printf("Error querying own client talk status: %d\n", error);
break;
}
} else {
if((error = ts3client_getClientVariableAsInt(serverConnectionHandlerID, ids[i], CLIENT_FLAG_TALKING, &talkStatus)) != ERROR_ok) {
printf("Error querying client talk status: %d\n", error);
break;
}
}
printf("%u - %s (%stalking)\n", ids[i], name, (talkStatus == STATUS_TALKING ? "" : "not "));
ts3client_freeMemory(name);
}
printf("\n");
ts3client_freeMemory(ids); /* Release array */
}
procedure TfrmMain.RequestOnlineClients;
var
ids : array of anyID;
pids : PanyID;
aid : anyID;
begin
error := ts3client_getClientList(FTSServerHandlerID, @ids);
if (error <> ERROR_ok) then
begin
if (ts3client_getErrorMessage(error, @errormsg) = ERROR_ok) then
begin
LogMsg(Format('Error requesting online clients: %s', [errormsg]));
ts3client_freeMemory(errormsg);
end;
end else
begin
pids := @ids[0];
while (pids^ <> 0) do
begin
aid := pids^;
LogMsg(format('id %u',[aid]));
inc(pids);
end;
ts3client_freeMemory(@pids^);
end;
end;