|
Yes, we already know that; please edit your question and make clear what the actual problem is with your code.
|
|
|
|
|
As far as I know, you cannot do that. That is you cannot pass the result of bind on a class member function as a C-style callback. If you cannot change the prototype of my_init_control then you have to use a workaround (e.g. write the callback as a standard function).
|
|
|
|
|
Try this, noting that Class (with capital C) is incorrect, it should be class . Also that B::proc needs to be static.
#include <stdlib.h>
#include <stdio.h>
typedef int (*callback_fnc)(int);
void my_init_control(callback_fnc cb, int flags)
{
cb(flags);
}
class B
{
public:
void init()
{
my_init_control(&B::proc, 0);
}
static int proc(int msg) {
printf("This is message number %d\n", msg);
return 0;
}
};
int main(
)
{
B bobj;
bobj.init();
return 0;
}
|
|
|
|
|
I cannot modify "my_init_control()"
Maybe someone knows how can it be done using boost::bind ?
|
|
|
|
|
|
|
I think the OP cannot. Consider the following piece of code.
#include <iostream>
#include <functional>
using namespace std;
typedef void (*Callback)(int );
void invoke( Callback cb) { cb(5); }
void foo(int i) { cout << (i*2) << "\n";}
int main()
{
foo(3);
invoke(foo);
auto bf = std::bind(foo, placeholders::_1);
bf(7);
std::bind(foo, 9)();
invoke( std::bind(foo, placeholders::_1)); }
The last call is not allowed due to type mismatch between the bind return value and the C -like callback (I believe you might try similar code with boost ).
|
|
|
|
|
Thanks for that; I looked at the boost documentation and it sort of implied that it could be done. But as I am new to this I probably misunderstood.
|
|
|
|
|
boost::bind returns a function object, not a C function. It may behave like a function pointer, but these types are different, and the compiler won't be able to match the function argument list with a function object as the second argument.
The problem you describe cannot be solved with the limitations you've set. Clearly, part of the limitations are of your own making and need to be revised. To understand that, you just need to think about the flow of control:
1. from your code you set up a callback mechanism that is supposed to call your callback function
2. then you call a function outside of your code
3. At some point this outside function calls your callback function, passing along some data
4. Your callback function is called. the only data it has are the function arguments it got passed from it's calling function. At this point it is entirely out of context from the rest of your application and doesn't know about any of your B objects that you may have created. If the calling function doesn't know about Bs, then the callback function cannot know about them either.
It is impossible to process anything dependend on some B member variables in the callback function, unless you pass these variables all the way from step 1 through step 4! Unfortunately the code in step 3 is outside your control - therefore this is impossible. Or it wouldbe impossible if you insist on passing along a reference to some B object: the caller doesn't know about that class!
There are only two solutions that may work:
1. the library you are using lets you pass along additional data to the callback setup which then will be passed to the callback invocation. If so, you could just pass along the values of var1 and var2.
2. If the above isn't possible, the only alternative I see is to use global variables to store the state of B or just its member variables.
Of course. the premise that your callback absolutely needs to know your B object may have been wrong! Time to think about how you intended to use this callback mechanism!
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Hello, I am using Win32++ and I would like to know how to prevent a CDialog window from closing the main frame (CMainFrame). I have overriden OnOK() and OnClose() and added nothing to these functions.
R.S.V.P
|
|
|
|
|
Member 13721875 wrote: I am using Win32++ What is Win32++? Also, we cannot guess what your code is doing, you need to provide more information.
|
|
|
|
|
|
Thanks, I think I came across this many years ago, but cannot remember where. And I thought MFC was also free these days.
|
|
|
|
|
You have the code so look at what messages a CDialog sends and posts in response to it's WM_CLOSE and WM_DESTROY.
In vino veritas
|
|
|
|
|
I am trying to get the file size, with code:
::FindFirstFile((LPCTSTR)strFileSpec, &fd);
LARGE_INTEGER size;
size.HighPart = fd.nFileSizeHigh;
size.LowPart = fd.nFileSizeLow;
DWORD dwsize = (DWORD)size.QuadPart;
but is not right when the size of the file is huge, because at a file with 7.8 GB in windows explorer, my code says that has 3.03 GB ...
I have tried with this code too:
CFileStatus status;
CFile::GetStatus(strFileName, status);
DWORD dwsize = (DWORD)status.m_size);
the same result ... what I am doing wrong ?
My OS is Win10 64 bit.
modified 14-Mar-19 6:39am.
|
|
|
|
|
|
Yes, that was the problem. Thank you.
|
|
|
|
|
|
I am trying to get an image list handle from system icons, like that:
return (HIMAGELIST)SHGetFileInfo(_T("c:\\"), 0, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
and is working good, except on some PCs, where there is some icons without any icon. I am using this handle to a CListCtrl:
Where icon has found: Capture — Postimage.org[^]
Where icon hasn't: Capture2 — Postimage.org[^]
Of course that OS has the icon and program for every type of files, including case where my code dont't work ...
Last edit: Here is what is looking the list control with this state of icons: Capture — Postimage.org[^]
So one of files has icons, one hasn't ... and I am not figure out why ...
modified 14-Mar-19 3:54am.
|
|
|
|
|
Try adding some debug code to see exactly what is being returned on your call to SHGetFileInfo .
|
|
|
|
|
I have solved, I have used the same image list handle for two controls, CTreeCtrl and CListCtrl ... and I haven't share this image list correctly ... now everything is alright.
|
|
|
|
|
Is there possible to write a registry key inside HKEY_LOCAL_MACHINE whith an app which is not running as admin ? I have tried that, I cannot write to HKEY_LOCAL_MACHINE with a MFC app where is run without administrator rights ...
modified 13-Mar-19 9:17am.
|
|
|
|
|
No - not unless an administrator has previously changed the permissions on the key in question to grant the non-admin user permission to write to it.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
In a template does inline hold the same meaning as when it's used in a class?
I.e. the way I understood it in a class it "requests" the compiler to put it inline but the compiler was not required to do so and it was up to the compiler to decide.
template <typename T>
inline T const& MyTemplateFunc (T const& a, T const& b)
{ return a < b ? b:a;
}
|
|
|
|