Does anybody know what would cause the FindWindow function to return the error:
ALREADY_EXISTS error (183)
I could understand a FILE_NOT_FOUND (2), but why would it return a 183?
MSDN says, that FindWindowand FindWindowEx return NULL if the function fails and that you should check GetLastError. It seems that this documentation is wrong. Take this code fragment:
SetLastError(12345);
HWND h = FindWindow(L"class_name_that_does_not_exist", nullptr);
cout << h << ' ' << GetLastError() << endl;
It will output
00000000 12345
So as you can see FindWindow fails to set the last error. In your case this means that the ERROR_ALREADY_EXISTS was the last error set before FindWindow was called.
Related
HKEY hkey;
LPCSTR data = "HelloWrold\0";
if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sample", 0,KEY_ALL_ACCESS, &hkey))
{
cout << "Registry Open SUccess" << endl;
if (ERROR_SUCCESS == RegSetValueEx(hkey, L"NAME", 0, REG_SZ, (LPBYTE)(data), strlen(data)+1))
cout << "Value Set Success" << endl;
else
cout << "Value Set Failed" << endl;
}
else
cout << "Registry Open Failed " << GetLastError() << endl;
when i use this code the code works fine, but in registry I'm not getting Helloworld but getting some chinese characters, any suggestion what to do?
Why does misdetected Unicode text tend to show up as Chinese characters?
If you take an ASCII string and cast it to Unicode, the results are usually nonsense Chinese.
Your project setting are probably defining UNICODE and your call to RegSetValueEx ends up calling RegSetValueExW but you are passing it a narrow C-style string.
Change it to RegSetValueExA(hkey, "NAME", 0, REG_SZ, (LPBYTE)(data), strlen(data)+1) or change data to be a Unicode string (WCHAR/LPWSTR).
You can catch this sort of error in C++ by changing (LPBYTE)(data) to (LPBYTE)const_cast<LPTSTR>(data).
When I call the function CreateProcessAsUser(), it returns a failure.
And then the call to GetLastError() to check why the error occurred returns the value 0x16f.
I couldn't find out what the error is supposed to mean.
ERROR_CHILD_PROCESS_BLOCKED is converted NTSTATUS - STATUS_CHILD_PROCESS_BLOCKED (0xC000049D) - I search in ntoskrnl.exe and found that this code referenced only from 2 place when NtCreateUserProcess called - from SeSubProcessToken and for log error:
NtCreateUserProcess
PspAllocateProcess
PspInitializeProcessSecurity
SeSubProcessToken
if (!SeTokenIsNoChildProcessRestricted(Token))
{
status = STATUS_CHILD_PROCESS_BLOCKED;
}
if (PspAllocateProcess() == STATUS_CHILD_PROCESS_BLOCKED)
{
EtwTraceDeniedTokenCreation();
}
so when SeTokenIsNoChildProcessRestricted(Token) return FALSE you can got ERROR_CHILD_PROCESS_BLOCKED from CreateProcess.
this is new api, exist only from 1607 build of win10
#if (NTDDI_VERSION >= NTDDI_WIN10_RS1)
NTKERNELAPI
BOOLEAN
SeTokenIsNoChildProcessRestricted(
_In_ PACCESS_TOKEN Token
);// return (Token->TokenFlags & 0x80000) != 0;
#endif
declared in ntifs.h but not documented.
so process, which fail call CreateProcessAsUser is somehow restricted. Windows Store sandbox , as how Harry Johnston guess ?
Should I call SDL_DestroyWindow if Window Creation fails? I have the following code below:
if(this->Window == NULL)
{
std::cout << "Error: Can't create the SDL Window \n" << SDL_GetError() << "\n";
SDL_DestroyWindow(this->Window);
std::exit(EXIT_FAILURE);
}
Is it wrong?
From the SDL wiki :
If window is NULL, this function will return immediately after setting the SDL error message to "Invalid window"
You don't have to call SDL_DestroyWindow if you don't have a window in the first place : it will not do anything (other than setting an error message).
You can think of it like free in C or delete in C++. If you give them NULL or nullptr (respectively), they do nothing.
I have the following function
std::tuple<int,val*>Socket::recv(val* values ) // const
{
char buf [ MAXRECV + 1 ];
memset ( buf, 0, MAXRECV + 1 );
int status = ::recv ( m_sock, buf, MAXRECV, 0 );
if ( status == -1 )
{
std::cout << "status == -1 errno == " << errno << " in Socket::recv\n";
// return std::make_tuple(0,NULL);//this is not working
}
else if ( status == 0 )
{
//return std::make_tuple(0,NULL); //this is not working
}
else
{
struct val* values=(struct val*) buf;
if(!std::isnan(values->val1) &&
!std::isnan(values->val2) &&
!std::isnan(values->val3) &&
!std::isnan(values->val4),
!std::isnan(values->val5),
!std::isnan(values->val6))
printf("received:%f %f %f %f %f %f\n", values->val1, values->val2,
values->val3, values->val4, values->val5, values->val6);
return std::make_tuple(status,values);
}
}
The received values are printed out in to standard output correctly within the function.
But when I try to access these received values out of the function by calling as follows what I get is all 0's.[after creating Socket rcvd object]
Would you tell me how to access these values outside the function?
1.
std::cout << std::get<1>(rcvd.recv(&values)->val1)
<< std::get<1>(rcvd.recv(&values)->val2)
<< std::get<1>(rcvd.recv(&values)->val3)
<< std::get<1>(rcvd.recv(&values)->val4)
<< std::get<1>(rcvd.recv(&values)->val5)
<< std::get<1>(rcvd.recv(&values)->val6)
<< std::endl;
2.
std::cout << std::get<1>(rcvd.recv(&values).val1)
<< std::get<1>(rcvd.recv(&values).val2)
<< std::get<1>(rcvd.recv(&values).val3)
<< std::get<1>(rcvd.recv(&values).val4)
<< std::get<1>(rcvd.recv(&values).val5)
<< std::get<1>(rcvd.recv(&values).val6)
<< std::endl;
3.
std::cout << std::get<1>(rcvd.recv(&values)[0])
<< std::get<1>(rcvd.recv(&values)[1])
<< std::get<1>(rcvd.recv(&values)[2])
<< std::get<1>(rcvd.recv(&values)[3])
<< std::get<1>(rcvd.recv(&values)[4])
<< std::get<1>(rcvd.recv(&values)[5])
<< std::endl;
where "values" comes from
struct val {
val1;
val2;
val3;
val4;
val5;
val6;} values;
All the three options of calling the function or access the struct val could not work for me.
Would you tell me
how to access these received values externally from any function?
how to return zero to struct pointer [NULL is not working ] when status is 0 or -1
Try
return std::make_tuple<int, val*>(0, nullptr);
The type of tuple is deduced from arguments, so by using 0,NULL you are actually using the null constant wich is evaluted to 0 and hence deduced type is <int,int>.
By the way, I see no reason for using NULL in C++11, if you need that really for some reason then cast NULL to val*
static_cast<val*>(NULL);
EDIT:
Other viable alternatives are
val* nullval = nullptr;
return std::make_tuple(0, nullval);
Or
return std::make_tuple(0, static_cast<val*>(nullptr));
Or (as comment suggest)
return {0, nullptr};
Choose the one that seems more clear to you.
You are lucky that the outside function is printing zeroes. It might have as well just dumped the core on you :)
What you are doing is accessing a buffer, that was created on a stack, after that stack was released (once the function's execution finished). That is HIGHLY UNSAFE and, pretty much, illegal.
Instead what you should do is allocate your data buffer in a 'free memory", using functions like malloc (in C) or operator new/new[] (in C++).
The quick fix is to replace the line
char buf [ MAXRECV + 1 ];
with
char * buf = new char [ MAXRECV + 1 ];
And when you do a type casting on line
struct val* values=(struct val*) buf;
you really ought to be sure that what you do is correct. If the sizeof() of you struct val is more than the sizeof(char[MAXRECV + 1]) you'll get in memory access troubles.
After you are done using the returned data buffer don't forget to release it with a call to free (in C) or delete/delete[] (in C++). Otherwise you'd have what is called a memory leak.
I am currently trying to get the names of a list of programs whose pid I have.
The program is run as administrator, but GetModuleFileNameEx fails with error code 5.
I open the program with OpenProcess(PROCESS_TERMINATE,PROCESS_QUERY_INFORMATION) and I have the SE_DEBUG_PRIVILEGE enabled.
The process handle passed to GetModuleFileNameEx() requires PROCESS_QUERY_INFORMATION and PROCESS_VM_READ access rights.
This worked for me:
HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE,
6088);
if (0 == h)
{
std::cerr << "OpenProcess() failed: " << GetLastError() << "\n";
}
else
{
char exe_path[2048] = {};
if (GetModuleFileNameEx(h, 0, exe_path, sizeof(exe_path) - 1))
{
std::cout << exe_path << "\n";
}
else
{
std::cerr << "GetModuleFileNameEx() failed: " <<
GetLastError() << "\n";
}
CloseHandle(h);
}
However, as others have pointed out (and is also stated in documentation for GetModuleFileNameEx()) there are safer ways to acquire this information:
GetProcessImageFileName()
QueryFullProcessImageName()
According to this thread that error is returned when there's not enough information to return the filename.