Warning when casting int to HWND - c++

I have a program I'm writing for Win64 in C++ that is executed from a parent program, and needs to set its parent window to the parent program's window. The parent program passes in it's HWND in as a command line argument, and I'm parsing the argument as an int (using stoi()) before it is cast to an HWND. A simplified version of my code is shown below:
int parentHwnd = stoi(args[HWND_INDEX]);
SetParent(childHwnd, (HWND) parentHwnd);
However, I'm getting the following error when compiling:
warning C4312: 'type cast': conversion from 'int' to 'HWND' of greater size
Is there safe way to cast an int to a HWNDand eliminate this error? Or should I be parsing the given command line arg to something other than an int that will safely be able to be cast to a HWND?

When passing pointers/handles you should use std::stoull;
And use explicit cast HWND parentHwnd = (HWND)std::stoull(args[HWND_INDEX]);

Related

wxWidgets Drop Files eventhandler initialization problem (invalid static_cast)

I am trying to make a simple program to drop files on a list using C++ with a (wxWidgets) wxListCtrl.
Tools I use: Code::Blocks 20.03, MinGW 17.1, wxWidgets 3.1.3, wxFormBuilder 3.9.0.
OS: Windows 10 Pro.
I tried the following: (in a function SetupGUI() which is called in the DnD_SimpleFrame class constructor.)
m_listCtrl1 = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_NO_HEADER|wxLC_REPORT );
m_listCtrl1->DragAcceptFiles(true);
m_listCtrl1->Connect(wxEVT_DROP_FILES, wxDropFilesEventHandler(DnD_SimpleFrame::OnDropFiles), NULL, this);
The function to be called on dropping the files (from explorer) is:
bool DnD_SimpleFrame::OnDropFiles(wxArrayString &filenames)
{
size_t nFiles = filenames.GetCount();
wxString str;
str.Printf( "%d files dropped", (int)nFiles);
m_listCtrl1->DeleteAllItems();
if (m_listCtrl1 != NULL)
{
m_listCtrl1->InsertItem(0, str);
for ( size_t n = 1; n < (nFiles+1); n++ )
m_listCtrl1->InsertItem(n, filenames[n]);
}
return true;
}
The build messages on the m_listCtrl1->Connect(...); line are:
||=== Build: Debug in DnD_Simple (compiler: GNU GCC Compiler) ===|
F:\Data\__C++\wxApps\DnD_Simple\DnD_SimpleMain.cpp||In member function 'void DnD_SimpleFrame::SetupGUI()':|
F:\SDKs\wx313\include\wx\event.h|149|error: invalid static_cast from type 'bool (DnD_SimpleFrame::*)(wxArrayString&)' to type 'wxDropFilesEventFunction' {aka 'void (wxEvtHandler::*)(wxDropFilesEvent&)'}|
F:\SDKs\wx313\include\wx\event.h|4196|note: in expansion of macro 'wxEVENT_HANDLER_CAST'|
F:\Data\__C++\wxApps\DnD_Simple\DnD_SimpleMain.cpp|92|note: in expansion of macro 'wxDropFilesEventHandler'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
What am I doing wrong here (or am forgetting)?
If you carefully look at the error message:
error: invalid static_cast from type 'bool (DnD_SimpleFrame::*)(wxArrayString&)' to type 'wxDropFilesEventFunction' {aka 'void (wxEvtHandler::*)(wxDropFilesEvent&)'}
you can see that it says that it can't convert something from one type to the other one.
The types may be difficult to parse if you're new to C++, but you should be able to see that these are function pointers (in fact, pointers to a class member). And looking at your code, you should also be able to see that the first is them is the type of your DnD_SimpleFrame::OnDropFiles function and hence the problem is that it can't be converted to the expected function type.
Finally, the reason for this is just that your function doesn't have the right parameter type: you're supposed to give wxWidgets something taking wxDropFilesEvent&, but your function takes wxArrayString& instead. You will have to change it to take wxDropFilesEvent& event and then use GetFiles() method of the event object to get the actual files.
On a completely different topic, you should use Bind() and not Connect() in the new code. If you're following a tutorial which uses the latter, it's a good sign that it's very outdated. Your code still wouldn't have compiled with Bind(), but the error messages would have been slightly simpler.

MessageBox in c++ using string

I am getting an error on the following code and I can't figure it out.
string name1 = om->get_name();
if (om->search(name, code))
{
MessageBox::Show("name"+name1);
}
else
MessageBox::Show("such a car doesn't exist");
Gives me the following error
error C2665: 'System::Windows::Forms::MessageBox::Show' : none of the 21 overloads could convert all the argument types
It looks like you are passing in a std::string when you should be passing in a managed String^.
Either convert om->get_name() to return a managed String^ or use the Win32 MessageBox function directly (which takes LPCTSTR parameters).
Reference for System::Windows::Forms::MessageBox::Show is at https://msdn.microsoft.com/en-us/library/519bytz3(v=vs.110).aspx
Reference for Win32 API MessageBox function is at https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx

"PVOID" is incompatible with parameter of type "LPCTSTR"

I am trying to create a file on an FTDI chip so that I can write and output data. I get from the manual that to create a file the following line of code needs to be written:
ftHandleFile = FT_W32_CreateFile((PVOID)LocId, GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
However, I get the following errors
Error 1 error C2664: 'FT_HANDLE
FT_W32_CreateFile(LPCTSTR,DWORD,DWORD,LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE)'
: cannot convert argument 1 from 'PVOID' to
'LPCTSTR' c:\users\caristid\documents\visual studio
2013\projects\ftd2xx\ftd2xx\ftd2xx.cpp 100 1 ftd2xx
IntelliSense: argument of type "PVOID" is incompatible with parameter of type "LPCTSTR" c:\Users\caristid\Documents\Visual Studio 2013\Projects\ftd2xx\ftd2xx\ftd2xx.cpp 100 35 ftd2xx
I assume that these are general purpose errors and it can be solved by simply using the right variables.
Does anyone know how to do it ?
The compiler expects the first parameter to be of type LPCTSTR which is a "Long Pointer to a Const TCHAR STRing".
By casting LocId to PVOID you're passing in a void* as first parameter.
If LocId is already a string, remove the cast; otherwise find a string to pass to the function.

MinGW-w64 pthread_cleanup_pop() error

when compiling a C++ program which contains a "call" to the pthread_cleanup_pop(E) macro, the following error is thrown by g++:
error: second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'
Now, the apparent problem here is that the macro above expands to
(*pthread_getclean() = _pthread_cup.next, (E?_pthread_cup.func((pthread_once_t *)_pthread_cup.arg):0));} where the second expression is a call to a function returning void, but the third expression is simply 0.
Althoug I get the basic issue here, I really don't see why the warning occurs in this particular use, as the "result" of the condition is not assigned to anything.
For the record: I compiled the C++ program using MinGW-w64 3.3.0 (GCC 4.9.2) both with and without -std=c++98. In both cases, the error occurs. If I compile the same code as a C program (both with or without -std=c99), there is no error.
Does anyone know how I could get rid of that error in C++, other than by editing pthread.h?
Many thanks in advance!
EDIT: Here is some example code for reference:
#include <pthread.h>
static int cancelled = 0;
static void test_thread_cleanup(void *_arg){
cancelled = 1;
}
static void* test_thread(void *_arg){
pthread_cleanup_push(&test_thread_cleanup, NULL); // push cleanup handler on stack
while (1){ // never left unless cancelled via pthread_cancel() from main()
pthread_testcancel(); // just test for pthread_cancel() having been called
}
pthread_cleanup_pop(1); // pop cleanup handler from stack
return NULL; // actually never reached
}
int main(void){
pthread_t th;
pthread_create(&th, NULL, &test_thread, NULL);
pthread_cancel(th);
pthread_join(th, NULL);
return cancelled;
}

Void cast to type

I have MFC dialog based application.
void CThr_MfcDlg::OnBnClickedButton1()
{
this->SetWindowTextW(L"bla");
(CThr_MfcDlg*)GetDlgItem(IDD_THR_MFC_DIALOG)->SetWindowText(L"hello") ;
}
Line this->SetWindowTextW(L"bla"); changes form caption to bla
I expect line (CThr_MfcDlg*)GetDlgItem(IDD_THR_MFC_DIALOG)->SetWindowText(L"hello") ; should change caption to hello, but have compile error:
Error 1 error C2440: 'type cast' : cannot convert from 'void' to 'CThr_MfcDlg *'
Read this. Since -> operator's precedence (2) is higher than cast operator's (3), your code is parsed in this way:
(CThr_MfcDlg*) (GetDlgItem(IDD_THR_MFC_DIALOG)->SetWindowText(L"hello")) ;
To avoid this, you should use parenthesis with casting.
// this will be correct.
((CThr_MfcDlg*)GetDlgItem(IDD_THR_MFC_DIALOG))->SetWindowText(L"hello");