MFC OnTimer() method not working - mfc

I created an MFC dialog based app and wanted to add timer on the form. But it turns out that MFC is different than the .NET windows forms.
I added the ON_WM_TIMER() in the messagemap. and added the function definition for CMyDialog::OnTimer(UINT_PTR x)
{
}
But I am getting a compiler error in VS2005. I do not know what i am doing wrong.
"error C2509: 'OnTimer' : member function not declared in 'CMyDialog'"
Help is greatly appreciated. Thanks.

Obviously, you forgot to declare the function in MyDialog.h, in CMyDialog declaration:
afx_msg void OnTimer(UINT_PTR x);
Note that afx_msg is purely informative and can be omitted.

The documentation for the ON_WM_TIMER map macro shows that you're doing the right thing. The only thing I can think of is that you have left the afx_msg qualifier off of your function definition.
Edit: At the risk of stating the obvious, did you also include the prototype of the OnTimer function in your class declaration?

Related

Are there events in c++?

I have a situation where the simplest answer would be a .net style event. I have never used events in c++ though and was under the impression that it didn't even have any. Regardless, I found this msdn page: http://msdn.microsoft.com/en-us/library/ee2k0a7d.aspx and tried following its example adding
[event_source(native)]
to the top of my class and
__event void fileChanged(std::wstring fileName);
as a public member but it won't compile and gives me the error "Cannot use __try in functions that require object unwinding" which i don't even know what it means or what __try has to do with anything (since i don't have any try blocks in the class if thats what its referring to). And object unwinding? wat.
Your MSDN page tells you that there are events in C++. The example on that page compiles and works properly. This makes me think that it is having problems with the specific event that you are trying to set up.
So the next thing I'd do is to look at the MSDN page for the error: C2712
This page states that this error can occur when using the __event keyword and you pass an argument by value (see link for exact description). Indeed, changing the call to a constant reference causes the error to go away:
__event void fileChanged(const std::wstring& filename);

What difference between _set_purecall_handler and _set_purecall_handler_m?

I debug a code, and I use _set_purecall_handler to set function that be called when pure call virtual function happened. This exemple from MSDN work nice for me and do what I want: code from msdn
So, you can see the declaration of the function
void myPurecallHandler(void)
{
printf("In _purecall_handler.");
exit(0);
}
this function MUST return a void value and don't have any arguments, this function is called when a pure call virtuall function happened. I have trying to overload this function to passe it a parameters (The line number where pure call virtuall function happened ), but can't success.
If you see, there is another function there: _set_purecall_handler_m
What is the difference between this function and _set_purecall_handler?
Thanks a lot,
_set_purecall_handler_m is for use with mixed mode CRT when using C++ and C++-CLI. If you are not working with C++-CLI you really don't need to use it. If however you are creating say a DLL that may be used with C++-CLi applications you might want to consider using it.

Using enum parameter in functions

I'm working on Windows, in C++ with Visual Studio.
I have a class that has a:
enum algorithmStatus { LOADING, DETECTION, TRACKING, LOST };
In the declaration I want to use a setter and getter to change the status, something like:
void MyStatusClass::setAlgorithmStatus(algorithmStatus newStatus)
{
//_Status = newStatus;
//_Status = MyStatusClass::algorithmStatus::LOADING;
}
But I can't compile because I get:
Error 5 error C2511: 'void MyStatusClass::setAlgorithmStatus(MyStatusClass::algorithmStatus)' : overloaded member function not found in 'Nft_Status' c:\MyStatusClass.cpp 197
How can I do that setter correctly?
EDIT:
In header is already declarated:
void setAlgorithmStatus(MyStatusClass::algorithmStatus newStatus);
and:
void setAlgorithmStatus(algorithmStatus newStatus);
In cpp the function is declared just i write on top.
SOLVED
The problem was i used a MyStatusClass::algorithmStatus in the constructor, you don´t need to use the MyStatusClass::, and its advisable don´t use it if you don´t need it.
The MSDN documentation for error code C2511 gives you a good list to lookout for:
identifier : overloaded member function not found in class
No version of the function is declared with the specified parameters. Possible causes:
Wrong parameters passed to function.
Parameters passed in wrong order.
Incorrect spelling of parameter names.
Always, lookup the error codes to get help in resolving compilation errors.

Is preceding a function with CALLBACK or WINAPI or PASCAL (in MFC) is absolutely necessary or only for readability purpose?

In MFC VC++, setTimer function is setted using a CALLBACK procedure. From the link I read that
A function that is marked with __stdcall uses the standard calling
convention so named because all Win32 API functions (except the few
that take variable arguments) use it.
And from that, this is what I have understand, ALL THE VC++ MFC FUNCTIONS USE __stdcall as their calling conversions.
And CALLBACK is defined as follows....
#define CALLBACK __stdcall
What I have read:
Preceding a function with CALLBACK is used to emphasise that the particular function will be called automatically whenever necessary(like in this setTimer case or onClick case in javascript),
My doubt is , In MFC VC++ all functions(except the few
that take variable arguments) has a default calling convention of __stdcall. Hence either preceding or not preceding a function with CALLBACK or WINAPI or PASCAL has a same effect?
Is it absolutely necessary for the computer? It depends on the context. When you mismatch the calling convention, you could either get lucky because the datatypes on the stack happen to match the requirements of the API, or it could fail miserably when your code is run on a different architecture like x64 and crashes every time.
Is it absolutely necessary for the maintenance programmer? Yes, it is. You know, the poor person who will have to figure out your non-standard conventions and clever "optimizations." Some day, that poor person might be you.
The compiler was yelling at you for a reason when you tried to subvert the API.

ESP Error when I call an API function?

platform : win32 , language : c++
I get this error when I call an imported function I declared:
Run-Time Check Failure #0 - The value of ESP was not properly saved
across a function call. This is
usually a result of calling a function
declared with one calling convention
with a function pointer declared with
a different calling convention.
And this is the code I used:
int LoadSongFromFile(int module);
typedef int (CALLBACK* loadSongT)(LPCTSTR);
//...
HINSTANCE dllHandle = NULL;
loadSongT loadSongPtr = NULL;
dllHandle = LoadLibrary(L"miniFMOD.dll");
loadSongPtr = (loadSongT)GetProcAddress(dllHandle,"SongLoadFromFile");
int songHandle = loadSongPtr(L"C:\b.xm");
The function I'm trying to call is SongLoadFromFile which requires one argument (in C# it is string so I assume its LPCTSTR in C++) and returns an int value.
Can somebody check what have I done wrong?
P.S. songHandle gets a weird negative value of -858993460
This is how I can call that function from C# :
[DllImport("MiniFMOD.dll")] public static extern int SongLoadFromFile(string name);
P.S. 2 : Using *typedef int (__cdecl loadSongT)(char);* doesn't return an error but songHandle comes up as 0.
miniFMOD.dll is an unmanaged library
I think the other people are misunderstanding the question. It seems to me that minifmod.dll is a native library that exports a function named SongLoadFromFile. The existing code that calls this is managed code (C#) that uses DllImport to call the function in the native DLL. From what little information I could gather by a few Google searches, it looks as though it should be declared as follows:
typedef int (__cdecl * SongLoadFromFileT)(const char*);
Importantly, it is __cdecl calling convention and it takes an ANSI string instead of a Unicode string.
As an aside, I find it strange that I can't find ANYTHING on minifmod.dll other than a few forum posts on a Russian website and some SO questions from this guy. The only "legitimate" information I can find on minifmod is a small static library with similar functionality. I wonder if minifmod.dll is some kind of commercialized version of the static library; at least that would explain why there is not much public documentation about it.
Ah, I found it; it is a Delph port of minifmod (http://www.cobans.net/minifmod.php).
You need to make sure to specify the right calling convention in your function pointer prototype ('CALLBACK' might be the wrong choice).
The calling code uses the calling convention not matching that of the function being called. See this very similar question. You need to open the header defining that function (should come with the library you try to use), look the convention up and change your function pointer declaartion accordingly.