Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winapi 如何在Win32控制台应用程序中使用GetLastError函数?_Winapi - Fatal编程技术网

Winapi 如何在Win32控制台应用程序中使用GetLastError函数?

Winapi 如何在Win32控制台应用程序中使用GetLastError函数?,winapi,Winapi,我试图使用Windows API中的RegOpenKeyEx函数打开注册表项,并获得以下代码: #include <windows.h> #include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; int wmain(int argc, wchar_t*argv []) { HKEY hKey = HKEY_CURRENT_USER;

我试图使用Windows API中的RegOpenKeyEx函数打开注册表项,并获得以下代码:

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

using namespace std;

int  wmain(int argc, wchar_t*argv [])
{
    HKEY hKey = HKEY_CURRENT_USER;
    LPCTSTR lpSubKey = L"Demo";
    DWORD ulOptions = 0;
    REGSAM samDesired = KEY_ALL_ACCESS;
    HKEY phkResult;

    long R = RegOpenKeyEx(hKey, lpSubKey, ulOptions, samDesired, &phkResult);

    if (R == ERROR_SUCCESS)
    {
        cout << "The registry key has been opened." << endl;
    }
    else //How can I retrieve the standard error message using GetLastError() here?
    {

    }

}
#包括
#包括
#包括
#包括
使用名称空间std;
int wmain(int argc,wchar_t*argv[])
{
HKEY HKEY=HKEY_当前_用户;
LPCTSTR lpSubKey=L“Demo”;
DWORD-ulOptions=0;
REGSAM SAMREQUIRED=键\u所有\u访问;
HKEY phkResult;
long R=RegOpenKeyEx(hKey、lpSubKey、ulOptions、samDesired和phkResult);
如果(R==错误\成功)
{

cout注册表函数不使用
GetLastError()
。它们直接返回实际的错误代码:

long R = RegOpenKeyEx(hKey, lpSubKey, ulOptions, samDesired, &phkResult);

if (R == ERROR_SUCCESS)
{
    cout << "The registry key has been created." << endl;
}
else
{
    cout << "The registry key has not been created. Error: " << R << endl;
}
试试这个

HKEY hKey = HKEY_CURRENT_USER;
LPCTSTR lpSubKey = L"Demo";
DWORD ulOptions = 0;
REGSAM samDesired = KEY_ALL_ACCESS;
HKEY phkResult;


char *ErrorMsg= NULL;

long R = RegOpenKeyEx(hKey, lpSubKey, ulOptions, samDesired, &phkResult);

if (R == ERROR_SUCCESS)
{

    printf("The registry key has been opened.");
}
else //How can I retrieve the standard error message using GetLastError() here?
{
     FormatMessageA(
    FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |       FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_ALLOCATE_BUFFER,
    NULL,
    R,
    0,
    (LPSTR)&ErrorMsg,
    0,
    NULL
);

      printf("Error while creating Reg key.");

}

是的,但是我如何在我的示例代码中使用FormatMessage?在你编辑你的问题之前,我的评论是有意义的。下次一定要更具体一些。太棒了!非常感谢,@Remy
HKEY hKey = HKEY_CURRENT_USER;
LPCTSTR lpSubKey = L"Demo";
DWORD ulOptions = 0;
REGSAM samDesired = KEY_ALL_ACCESS;
HKEY phkResult;


char *ErrorMsg= NULL;

long R = RegOpenKeyEx(hKey, lpSubKey, ulOptions, samDesired, &phkResult);

if (R == ERROR_SUCCESS)
{

    printf("The registry key has been opened.");
}
else //How can I retrieve the standard error message using GetLastError() here?
{
     FormatMessageA(
    FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |       FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_ALLOCATE_BUFFER,
    NULL,
    R,
    0,
    (LPSTR)&ErrorMsg,
    0,
    NULL
);

      printf("Error while creating Reg key.");

}