繁体   English   中英

您如何使用size_t遍历uint64 *,c#的等效项是什么?

[英]How do you iterate through a uint64* with size_t, and what is the equivilent for c#?

我正在使用c#编写一个Teamspeak3插件(使用此base ),并且正在获取频道列表,这是在c中的方法:

/* Print list of all channels on this server */
    char* s;
    char msg[1024];
    anyID myID;
    uint64* ids;
    size_t i;
    unsigned int error;

    if(ts3Functions.getChannelList(serverConnectionHandlerID, &ids) != ERROR_ok) {
        ts3Functions.logMessage("Error getting channel list", LogLevel_ERROR, "Plugin", serverConnectionHandlerID);
        return;
    }
    printf("PLUGIN: Available channels:\n");
    for(i=0; ids[i]; i++) {
        /* Query channel name */
        if(ts3Functions.getChannelVariableAsString(serverConnectionHandlerID, ids[i], CHANNEL_NAME, &s) != ERROR_ok) {
            ts3Functions.logMessage("Error querying channel name", LogLevel_ERROR, "Plugin", serverConnectionHandlerID);
            return;
        }
        printf("PLUGIN: Channel ID = %llu, name = %s\n", (long long unsigned int)ids[i], s);
        ts3Functions.freeMemory(s);
    }
    ts3Functions.freeMemory(ids);  /* Release array */

for(i=0; ids[i]; i++) ,它声明正在使用size_t迭代uint64*

我想知道如何遍历它,并且在C#中最接近的等效项是什么?

我找到了答案(感谢localhost和Chris找到了答案!)。 基本上, ulong*指向一个数组,并且size_t正在遍历它,试图查找通道的ID。 因为在C ++中,在for循环中,除零以外的每个数字都是真实的,因此它将一直查询直到没有更多的通道ID,并返回零为止。 TeamSpeak社区论坛帖子

对于使用此插件对C#中的代码感兴趣的任何人:

if (funcs.getChannelList(serverConnectionHandlerID, ref v) != Errors.ERROR_ok) {
    funcs.logMessage("Failed", LogLevel.LogLevel_ERROR, "Plugin", serverConnectionHandlerID);
    break;
}
// Convert it to a ulong*
ulong * ptr = (ulong * ) v.ToPointer();
// Iterate through the array
for (ulong t = 0; ptr[t] != 0; t++) {
    // The String result
    string result;
    // The pointer result
    IntPtr res = IntPtr.Zero;
    /*
     Channel Variable Arguments:
    1: The server connection ID
    2: The iterated channel id
    3: An IntPtr at 0, which signifies CHANNEL_NAME
    4: A reference to stores results
    */
    if (
    funcs.getChannelVariableAsString(serverConnectionHandlerID, ptr[t], new IntPtr(0), ref res) != Errors.ERROR_ok) {
        // Error message
        funcs.logMessage("Error", LogLevel.LogLevel_WARNING, "Plugin", serverConnectionHandlerID);
        break;
    }
    // Convert the pointer to a string
    if ((result = Marshal.PtrToStringAnsi(res)) == null) break;
    // Print it
    funcs.printMessageToCurrentTab(result);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM