0

我知道我是 Windows 编程高手,所以我只是在学习。

我正在编写一个命令行工具来处理 Windows API 的一些注册表函数,但我需要转换char *来自argv[]数组的 a 以使用内容初始化LPCTSTR变量,但我不知道该怎么做。

这是我到目前为止的代码:

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

int main(int argc, char *argv [])
{
    int count;
    DWORD Reserved = 0;
    LPTSTR lpClass = NULL;
    DWORD dwOptions = REG_OPTION_NON_VOLATILE;
    REGSAM samDesired = KEY_ALL_ACCESS;
    LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL;
    HKEY phkResult;
    DWORD lpdwDisposition;

    if (argv[1] == 0)
    {
        printf("There are no arguments, pleas type one at least. \n");
    }
    else if (std::string(argv[1]) == "-Clave")
    {
        if (std::string(argv[2]) == "HKCU")
        {
            printf("You are going to create a HKCU sub-key \n");
            HKEY hKey = HKEY_CURRENT_USER;

            if (std::string(argv[3]) != "")
            {

                printf("You are going to create this sub-key: %s \n",argv[3]);

                //This is what I tried.

                LPCTSTR lpSubKey = TEXT("%s",argv[3]);

                RegCreateKeyEx(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, &phkResult, &lpdwDisposition);

                if (lpdwDisposition == REG_CREATED_NEW_KEY)
                {
                    printf("The registry key has been created. \n");
                }

            }
            else
                printf("No one");
        }
        else
        {
            printf("No key has been specified \n");
        }

    }

    system("Pause");

}

你能帮我吗?

非常感谢。

4

3 回答 3

0

最简单的解决方案是显式调用 Ansi 版本的注册表函数(RegCreateKeyExA等)并让 Windows 为您处理转换。您当前正在调用函数的 Unicode 版本(RegCreateKeyExW等),否则您一开始就不会遇到转换问题:

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

int main(int argc, char *argv [])
{
    int count;
    HKEY hkResult;
    DWORD dwDisposition;

    if (argc < 1)
    {
        printf("There are no arguments, pleas type one at least. \n");
    }
    else if (strcmp(argv[1], "-Clave") == 0)
    {
        if (argc < 3)
        {
            printf("There are not enough arguments typed in. \n");
        }
        else if (strcmp(argv[2], "HKCU") == 0)
        {
            if (strcmp(argv[3], "") != 0)
            {
                printf("You are going to create HKCU sub-key: %s \n", argv[3]);

                if (RegCreateKeyExA(HKEY_CURRENT_USER, argv[3], 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkResult, &dwDisposition) == 0)
                {
                    if (dwDisposition == REG_CREATED_NEW_KEY)
                    {
                        printf("The registry key has been created. \n");
                    }
                    else
                    {
                        printf("The registry key already exists. \n");
                    }

                    RegCloseKey(hkResult);
                }
                else
                {
                    printf("Unable to create the registry key. \n");
                }
            }
            else
            {
                printf("No HKCU sub-key has been specified \n");
            }
        }
        else
        {
            printf("No root key has been specified \n");
        }
    }

    system("Pause");
    return 0;
}

更新:如果您想在政治上正确,大多数处理文本数据的 Win32 API 都会处理TCHAR(这是您尝试使用的,但没有成功),因此可以使用单个代码库为 Ansi 和 Unicode 编译它们,例如:

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <tchar.h>

int _tmain(int argc, TCHAR *argv [])
{
    int count;
    HKEY hkResult;
    DWORD dwDisposition;

    if (argc < 1)
    {
        _tprintf(_T("There are no arguments, pleas type one at least. \n"));
    }
    else if (_tcscmp(argv[1], _T("-Clave")) == 0)
    {
        if (argc < 3)
        {
            _tprintf(_T("There are not enough arguments typed in. \n"));
        }
        else if (_tcsicmp(argv[2], _T("HKCU")) == 0)
        {
            if (_tcscmp(argv[3], _T("")) != 0)
            {
                _tprintf(_T("You are going to create HKCU sub-key: %s \n"), argv[3]);

                if (RegCreateKeyEx(HKEY_CURRENT_USER, argv[3], 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkResult, &dwDisposition) == 0)
                {
                    if (dwDisposition == REG_CREATED_NEW_KEY)
                    {
                        _tprintf(_T("The registry key has been created. \n"));
                    }
                    else
                    {
                        _tprintf(_T("The registry key already exists. \n"));
                    }

                    RegCloseKey(hkResult);
                }
                else
                {
                    _tprintf(_T("Unable to create the registry key. \n"));
                }
            }
            else
            {
                _tprintf(_T("No HKCU sub-key has been specified \n"));
            }
        }
        else
        {
            _tprintf(_T("No root key has been specified \n"));
        }
    }

    _tsystem(_T("Pause"));
    return 0;
}

话虽如此,既然你正在开始一个新项目,你最好不要忘记 Ansi 甚至存在,而只使用 Unicode 处理一切:

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

int wmain(int argc, WCHAR *argv [])
{
    int count;
    HKEY hkResult;
    DWORD dwDisposition;

    if (argc < 1)
    {
        wprintf(L"There are no arguments, pleas type one at least. \n");
    }
    else if (wcscmp(argv[1], L"-Clave") == 0)
    {
        if (argc < 3)
        {
            wprintf(L"There are not enough arguments typed in. \n");
        }
        else if (_wcsicmp(argv[2], L"HKCU") == 0)
        {
            if (wcscmp(argv[3], L"") != 0)
            {
                wprintf(L"You are going to create HKCU sub-key: %s \n", argv[3]);

                if (RegCreateKeyExW(HKEY_CURRENT_USER, argv[3], 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkResult, &dwDisposition) == 0)
                {
                    if (dwDisposition == REG_CREATED_NEW_KEY)
                    {
                        wprintf(L"The registry key has been created. \n");
                    }
                    else
                    {
                        wprintf(L"The registry key already exists. \n");
                    }

                    RegCloseKey(hkResult);
                }
                else
                {
                    wprintf(L"Unable to create the registry key. \n");
                }
            }
            else
            {
                wprintf(L"No HKCU sub-key has been specified \n");
            }
        }
        else
        {
            wprintf(L"No root key has been specified \n");
        }
    }

    _wsystem(L"Pause");
    return 0;
}
于 2013-08-12T23:37:43.473 回答
0

另一种可能的解决方案是以这种方式更改main函数声明:

int _tmain(int argc, TCHAR* argv[])

引用MSDN

您还可以使用在 TCHAR.h 中定义的 _tmain。_tmain 解析为 main,除非定义了 _UNICODE。在这种情况下,_tmain 解析为 wmain。

RegCreateKeyEx的第二个参数在类型* LPCTSTR上。
lpSubKey由相同的类型声明,并在作为参数传递给函数时正确初始化RegCreateKeyEx

这是使用 Visual C++ 编译的源代码,并使用 多字节字符集_MBCS定义了宏):

// RegCreate.cpp : Defines the entry point for the console application.
// VC++ Compiler Options :
// cl /W3 /MT /O2 /D WIN32 /D _CONSOLE /D _MBCS /EHsc /TP RegCreate.cpp 
#include <Windows.h>
#include <iostream>
#include <string>
#include <tchar.h>

#ifndef _MBCS
    #define _MBCS
#endif

#pragma comment(lib, "Advapi32.lib")

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

    DWORD Reserved = 0;
    LPTSTR lpClass = NULL;
    DWORD dwOptions = REG_OPTION_NON_VOLATILE;
    REGSAM samDesired = KEY_ALL_ACCESS;
    LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL;
    HKEY phkResult;
    DWORD lpdwDisposition;
    if(argc < 3)
    {
        std::cout << "There are no arguments, pleas type one at least. " << std::endl;
        return 1;
    }
    if((std::string(argv[1]) == "-Clave") && (std::string(argv[2]) == "HKCU") && (argc == 4))
    {

        std::cout << "You are going to create a HKCU sub-key " << std::endl;
        HKEY hKey = HKEY_CURRENT_USER;

        if(std::string(argv[3]) != "")
        {
            std::cout << "You are going to create this sub-key: " << argv[3] << std::endl;
            //This is what I tried.
            LPCTSTR lpSubKey = argv[3];
            if(ERROR_SUCCESS != RegCreateKeyEx(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired,
                                               lpSecurityAttributes, &phkResult, &lpdwDisposition))
            {
                return 1;
            }
            if(lpdwDisposition == REG_CREATED_NEW_KEY)
            {
                std::cout << "The registry key has been created. " << std::endl;
            }
            RegCloseKey(phkResult);
        }
        else
        {
            std::cout << "No one";
        }
    }
    else
    {
        std::cout << "No key has been specified " << std::endl;
    }
    return 0;
}

我们可以使用TEXT宏来定义字符串是否为Unicode,但在上面的示例中,这不是必需的。

如果您使用 C++,我建议您更改printfstd::cout,它适用于 ASCII 字符。

于 2013-08-13T10:10:30.137 回答
0

查看 Windows.h 中的 MultiByteToWideChar 函数。这是一个很好的快速示例:

const char * orig = "text1";
WCHAR buffer[6];
MultiByteToWideChar(0, 0, orig, 5, buffer, 6 );
LPCWSTR text = buffer;

哎呀,那是给 LPCWSTR 的。对于 LPCTSTR,只需使用:

LPCTSTR text = _T("text1");
于 2013-08-12T18:45:36.953 回答