简体   繁体   English

解析 c 中的 argc 和 argv[] 参数

[英]Parsing argc and argv[] parameter in c

I have written the following program which is working fine when I compile the program in x86 configuration but when I change the configuration of the platform to x64, my program doesn't work at all.我编写了以下程序,当我在 x86 配置中编译程序时运行良好,但是当我将平台配置更改为 x64 时,我的程序根本无法运行。 Where is the problem:问题出在哪里:

int main(int argc, char* argv[])
{
    char* cp_UserName[MAX_PATH];
    char* cp_DomainName[MAX_PATH];
    char* cp_HashNtlm[MAX_PATH];
    char* cp_ComputerName[MAX_PATH];

    DnPthIconSetup();
    DnPthInitialization(TRUE);

    if (argc > 4)
    {
        for (size_t i = 1; i <= sizeof(argv); i++)
        {
            if (strstr(argv[i], "user") != NULL)
            {
                strtok_s(argv[i], ":", cp_UserName);
            }
            if (strstr(argv[i], "domain") != NULL)
            {
                strtok_s(argv[i], ":", cp_DomainName);
            }
            if (strstr(argv[i], "pc") != NULL)
            {
                strtok_s(argv[i], ":", cp_ComputerName);
            }
            if (strstr(argv[i], "ntlm") != NULL)
            {
                strtok_s(argv[i], ":", cp_HashNtlm);
            }
        }

        printf("%s\n", *cp_UserName);
        printf("%s\n", *cp_DomainName);
        printf("%s\n", *cp_ComputerName);
        printf("%s\n", *cp_HashNtlm);
        system("PAUSE");
        // ParametricCredentialDispatcher(cp_UserName, cp_DomainName, cp_HashNtlm, cp_ComputerName);
    }
    else if (argc == 1)
    {
        InteractiveMode();
    }
    else
    {
        printf("\nUsage: ./program user:[] domain:[] pc:[] ntlm:[]\n");
        system("PAUSE");
    }


    return 0;
}
  • sizeof(argv) is a size of the pointer, not number of arguments. sizeof(argv)是指针的大小,而不是 arguments 的数量。
  • Your usage of strstr will result into wrong result when, for example, the arguments contains pc:computer_for_an_user .例如,当 arguments 包含pc:computer_for_an_user时,您对strstr的使用将导致错误的结果。
  • This usage of strtok_s will store pointer to the name of configurations, not their values. strtok_s的这种用法将存储指向配置名称的指针,而不是它们的值。

Possible fix of the parsing part:解析部分的可能修复:

for (size_t i = 1; i < arc; i++)
{
    if (strncmp(argv[i], "user:", 5) != NULL)
    {
        *cp_UserName = strchr(argv[i], ':') + 1;
    }
    if (strncmp(argv[i], "domain:", 7) != NULL)
    {
        *cp_DomainName = strchr(argv[i], ':') + 1;
    }
    if (strncmp(argv[i], "pc:", 3) != NULL)
    {
        *cp_ComputerName = strchr(argv[i], ':') + 1;
    }
    if (strncmp(argv[i], "ntlm:", 5) != NULL)
    {
        *cp_HashNtlm = strchr(argv[i], ':') + 1;
    }
}

The added : in the arguments of strncmp is assuming that strchr won't return NULL .添加的:在 strncmp 的strncmp中假设strchr不会返回NULL

Also you should initialize the pointers and check if they are not NULL before passing them to printf .您还应该初始化指针并检查它们是否不是NULL ,然后再将它们传递给printf

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

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