I'm attempting to display the Error message denoted by the code given to me by GetLastError() and formatted by FormatMessage() within a MessageBox. (Using C++/CLI)
However for some reason I just can't match the argument list of MessageBox::Show.
I'm attempting to replicate a solution provided by Doron Moraz in this forum thread:
http://forums.codeguru.com/showthread.php?478858-GetLastError()-in-a-MessageBox()
However when I attempt to compile my code I get:
'System::Windows::Forms::MessageBox::Show' : none of the 21 overloads could convert all the argument types
1> c:\program files (x86)\reference assemblies\microsoft\framework\.netframework\v4.0\system.windows.forms.dll: could be 'System::Windows::Forms::DialogResult System::Windows::Forms::MessageBox::Show(System::String ^,System::String ^,System::Windows::Forms::MessageBoxButtons,System::Windows::Forms::MessageBoxIcon)'
1> c:\program files (x86)\reference assemblies\microsoft\framework\.netframework\v4.0\system.windows.forms.dll: or 'System::Windows::Forms::DialogResult System::Windows::Forms::MessageBox::Show(System::Windows::Forms::IWin32Window ^,System::String ^,System::String ^,System::Windows::Forms::MessageBoxButtons)'
1> while trying to match the argument list '(int, LPCTSTR, const wchar_t [6], long)'
As you can see below my code is fairly similar to the solution provided at the link. only I get the above error. The question is, Why? (see my code below).
if((m_hglrc = wglCreateContext(m_hDC)) == NULL)//if the creation of a wgl context fails
{
MessageBox::Show("wglCreateContext Failed");//let us know
void* lpBuffer; //create a buffer to hold our error message
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, // It´s a system error
NULL, // No string to be formatted needed
::GetLastError(), // Hey Windows: Please explain this error!
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Do it in the standard language
(LPTSTR)&lpBuffer, // Put the message here
0, // Number of bytes to store the message
NULL);
System::Windows::Forms::MessageBox::Show( NULL, (LPCTSTR)lpBuffer, _T("Error"),MB_OK|MB_ICONWARNING);
// Free the buffer.
if(lpBuffer)LocalFree(lpBuffer);
return 0;
}
In case it's relevant, my includes are:
#pragma once
#include <Windows.h>
#include <GL/gl.h>
#include<tchar.h>
using namespace System::Windows::Forms;
Thanks in advance,
Guy
It looks like you've solved this by switching to the unmanaged API, but here's how you'd use the managed one.
If you're going to use the a managed API, you'll want to use managed objects. In your call to MessageBox::Show, you have several unmanaged objects. According to the error message, it interpreted your parameters like this:
MessageBox::Show( NULL, (LPCTSTR)lpBuffer, _T("Error"),MB_OK|MB_ICONWARNING);
// seen by compiler as: int, LPCTSTR, const wchar_t [6], long
Here's the method I think you're trying to call in the MessageBox class:
Show(IWin32Window^ owner, String^ text, String^ caption,
MessageBoxButtons buttons, MessageBoxIcon icon)
NULL is generally #defined as 0, which is an integer. To produce a proper null pointer in C++/CLI, you want to use nullptr.
Both lpBuffer and "Error" need to be managed string objects.
For lpBuffer, you can just do gcnew String(lpBuffer), and it'll invoke the proper constructor (the one that takes a wide or narrow character pointer).
For "Error", just remove the _T(). The compiler will figure out that you want a managed string object and will provide one containing "Error".
In the managed API, the buttons & icon are contained in separate enums. You're referencing the unmanaged integer values here. You'll want to replace this with separate parameters for MessageBoxButtons and MessageBoxIcon.
Once all that is done, here's the final call:
MessageBox::Show(nullptr, gcnew String(lpBuffer), "Error",
MessageBoxButtons::OK, MessageBoxIcon::Warning);
However, we can do a little better: If you're not going to pass in a owner window, don't call the API that has a owner window parameter, call the API that doesn't.
MessageBox::Show(gcnew String(lpBuffer), "Error",
MessageBoxButtons::OK, MessageBoxIcon::Warning);
Related
I use CreateWindowEx which expands to CreateWindowExA. That function uses LPCSTR types. I would like to pass as a second argument MSFTEDIT_CLASS (from Richedit.h):
#define MSFTEDIT_CLASS L"RICHEDIT50W"
The following casting doesn't work:
(LPCSTR)MSFTEDIT_CLASS
CreateWindowEx returns NULL. It works when I pass the second argument this way:
"RICHEDIT50W"
but I don't want to copy a string from the header. How to fix that ?
There is only a single, viable solution here: Call CreateWindowExW, either directly or by defining the UNICODE preprocessor symbol and have the generic-text mapping CreateWindowEx expand to CreateWindowExW.
The window you are creating is a Unicode window, always. The character set used for communicating with a window is set at class registration time. The window class named "RICHEDIT50W" is registered using RegisterClassExW by the system. You don't have control over this.
Since you are eventually going to have to talk to the window using messages, you will need to use the Unicode variants of the message handling functions (GetMessageW, DispatchMessageW, etc.). You cannot use the ANSI versions, unless you are happy with an application, that sometimes doesn't fail.
First let me say that I've been searching for a solution for couple of days now...
I'm trying to get selected item for ListBox. This is my code:
CListBox * pList1 = (CListBox *)GetDlgItem(IDC_LIST1);
CString ItemSelected;
// Get the name of the item selected in the Sample Tables list box
// and store it in the CString variable declared above
pList1->GetText(pList1->GetCurSel(), ItemSelected);
MessageBox(ItemSelected, "TEST", MB_OK);
Now when i try this i get an error message saying "The Parameter is incorect"
Your code looks OK except error handling. Also MessageBox parameters look incorrect. The first parameter should be of type HWND. I believe that this is the root cause of your problems. Use MFC standard AfxMessageBox instead:
CListBox * pList1 = (CListBox *)GetDlgItem(IDC_LIST1);
int nSel = pList1->GetCurSel();
if (nSel != LB_ERR)
{
CString ItemSelected;
pList1->GetText(nSel, ItemSelected);
AfxMessageBox(ItemSelected);
}
If the CListBox is in single selection mode, the CListBox::GetCurSel will return the selected index.
If the CListBox is in multi-selection mode, you should use CListBox::GetSelItems which will return a list of indices.
You cannot mix'n'match the functions.
And always check return codes (as others already wrote).
If You already have a data member MyList(of classCListBox) :
int nSel = MyList.GetCurSel();
CString ItemSelected;
if (nSel != LB_ERR)
{
MyList.GetText(nSel, ItemSelected);
}
CWnd class has a MessageBox function which does not need a HWND parameter. But yes, AfxMessageBox is a little bit more easier to use and can be called anywhere in the MFC code without having a CWnd-derived object. And a beside note: if call a WinAPI function inside MFC code (not needed here, but possible in other cases) it's good to prepend it with scope resolution operator in order to avoid any confusion, mistake and/or name conflict (e.g. ::MessageBox...).
One possible cause for "invalid parameter" error in OP code is that it uses an ANSI string literal ("TEST") in a UNICODE build configuration. This case, must use an UNICODE string literal (L"TEST") or a little bit better, use _T macro (_T("TEST")) that makes it possible to build in both ANSI and UNICODE configurations.
I'm absolutely new to programming. I searched for this question online but couldn't find it anywhere
I'm trying to create window based MFC application using Visual studio 13. I have implemented an OpenFileDialog and obtained the path and file name in a stream.
Now I need to make it appear in a edittext box using the function SetWindowText. It accepts parameter of type LPCTSTR. So how do I make the conversion or Is there any other better approach for this problem?
Thanks in advance!
Don't mess around with dynamic allocation here. Simply get the string of the stream and put it into a CString.
It has an operator LPCTSTR for this purpose.
#include <atlstr.h> //CString
CString csText;
.SetWindowText(csText);
Having a problem here with creating a child window with c++ and win32 api.
If i check the getLastError function its returning "87" but i dont know what that means.
For what i know my code does not contain errors, can someone take a look at my code and help me figure out whats wrong with it.
(This is in the WinProc WM_CREATE section.)
HWND hChildWindow = CreateWindowEx(WS_EX_CLIENTEDGE,0,NULL,WS_OVERLAPPEDWINDOW|WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,hwnd,0,GetModuleHandle(0),NULL);
if(!hChildWindow)
{
char text[256];
int errormsg = (int)GetLastError();
sprintf(text,"Error# %i",errormsg);
MessageBox(0,text,"Error",MB_OK|MB_ICONEXCLAMATION);
return false;
}
87 = Invalid Parameter - be aware that you can use FormatMessage to get a string message from an error code.
The 2nd parameter to CreateWindowEx is a window class (either string or ATOM). Obviously NULL is not a valid value.
P.S.
For what i know my code does not
contain errors...
Beware of such a loud phrases. When something doesn't work everything should be checked carefully. Otherwise you may just accuse something/someone without any good for solving the problem. Check everything vs standard/documentation/specifications/etc. before you make any judgement.
A quick look through the System Error Codes reference indicates ERROR_INVALID_PARAMETER. You're most likely passing in an invalid combination of styles/flags to your window.
So i am playing/implementingtomyown with windows via c book examples and there is something about dll injection part that boggles me and i can't solve it.
I created a dialog that belongs to another thread/process and i am trying to send it TCHAR variable so it can then use that var in some function(both the function and tchar are in the same dll file)
So when the dialog is created and sitting well in another thread i send it a message.
First i declare tchar
TCHAR finalpath[MAX_PATH];
Then later i just fill it with info( i do this in the dll thread, not in the dialog's thread, let me also mention that i must do this in the dll thread because thats only way to fill the required tchar(i am required to get dll working directory and fill it in tchar))
So, when i get this info in my tchar i am trying to send a message to the dialog and use tchar as LPARAM(wparam is hwnd btw)
SendMessage(hWndDIPS, WM_APP, (WPARAM) lista, (LPARAM)finalpath);
Afterwards i do basic schoolwork in another threads dialog procedure loop...
INT_PTR WINAPI Dlg_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
chHANDLE_DLGMSG(hWnd, WM_CLOSE, Dlg_OnClose);
case WM_APP:
SaveListViewItemPositions((HWND) wParam, (TCHAR)lParam);
break;
}
return(FALSE);
}
Function that is supposed to receive the parameter(this function resides in shared dll and is called by the procedure as you see above is defined as follows..
void SaveListViewItemPositions(HWND hWndLV, TCHAR sejv[]) {
...}
The compiler error i get from this is
Error 7 error C2664: 'SaveListViewItemPositions' : cannot convert parameter 2 from 'TCHAR' to 'TCHAR []'
So i have no idea why is this happening. If tchar is array then i need to use it in parameters with [] added as thats how arrays are used in parameters(not to mention that if i dont do it it gives me more errors and i cant use the parameter in function anyways)
So why is it not converting then?
If there is another solution to make this dialog to receive a tchar var then please explain.
Thanks
Even after you'll fix your type declarations and properly cast the LPARAM to a TCHAR*, your code will be incorrect. The 'parameter' you pass in to that window procedure is a pointer, and as any pointer, is only valid within a process address space. The receiver window will have to use ReadProcessMemory and copy the string from your process into its own process. Of course, this implies that the receiver process knows your process id, and has proper privileges to be able to read from your memory. And you also need to pass in the length of the string, since ReadProcessMemory cannot guess where the NULL terminator is (although I reckon that with a MAX_PATH max length, this is not a serious issue).
So you are correct, this is a headache, and more so down the road. The privilege issue may be a show stopper.
There are several IPC mechanisms you could use. An easy one is an anonymous named pipe, see Anonymous Pipe Operations. Shared memory is another, see Using Shared Memory in a Dynamic-Link Library. COM would also work (have the process you 'control' create an instance of a class that is hosted in your process server, and let the COM marshaling do the rest, see Marshaling Details). Or you could hand-marshal a COM interface between the process boundary (see CoMarshalInterface).
I think that your problem is that you're typecasting the LPARAM to a TCHAR instead of an array of TCHARs (TCHAR*). Try changing that and see if it fixes things.