|
Two issues here:- Your
Func method has to be static if you want to pass it on as a regular function pointer. Check Mike Dunn's C++ FAQ for details.
- Once you hace declared your method static, you can set its calling convention like this:
class CMy{
...
static void __cdecl Func();
};
Hope this helps.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Joaquín is right.
A standard solution to this common problem is to have 2 static items: (1) a pointer to a CMy object and (2) a function of the type you expect to pass as a parameter, eg:
static CMy* pMy = NULL;
static void StaticFunc()
{
if (pMy != NULL) {
pMy->Func();
}
} Then, have a CMy object set the static pMy to "this" before you pass StaticFunc() to CallIt(), eg:
CMy my;
pMy = &my;
CallIt (&StaticFunc);
Hope this helps.
/ravi
"There is always one more bug..."
ravib@ravib.com
http://www.ravib.com
|
|
|
|
|
I'm probably stating the obvious here, but my solution is slightly different from Joaquin's in that CMy's member function doesn't have to be static.
/ravi
"There is always one more bug..."
ravib@ravib.com
http://www.ravib.com
|
|
|
|
|
|
Thanks everyone for assistance, it solves the problem as I state it, but the situation I have is like this: I have a function which is predefined and this function takes a callback function as parameter. I'm creating class, each copy of which should have it's own call back function. (I'm using glut (for OpenGL) and I'm creating window class whcih uses glut functions, and I'd like glut call a member-function of the class.)
Let's say I have function
void CallIt( void(*func)(ini,int)) );
And I can not change it.
The problem is that I want to have a couple of classes which are waiting for the callback, and CallIt doesn't provide inforamtion what class set the callback (
And as understood I can use only static functions or global functions and I can use class specific functions to link with CallIt.
I dont' see a way to do it, right now, but hope I'm wrong.
Regards, Alexander.
|
|
|
|
|
Well, the standard solution is to provide a pointer to the class when you register your function (this is usually called a callback parameter): this callback parameter can be used by your static callback function to transfer the function call to the actual class responsible for doing the job.
Unfortunately, GLUT seems not to provide any mechanism to store any callback parameter on callback registration. But do not abandon any hope, there's still a way (if a little convoluted) to retrieve the class, via glutGetWindow() . The idea is having a map associating windows IDs with classes, and having the static method dispatch to the appropriate class using this map. This is s little piece of pseudocode that describes the process using the glutMouseFunc callback:
class MyGlutWindow{
private:
static std::map<int,MyGlutWindow*> ID2MyGlutWindow;
public:
MyGlutWindow(){
...
ID2MyGlutWindow[glutGetWindow()]=this;
}
static void __cdecl glutMouseFuncCallback(int button,int state,int x, int y){
ID2MyGlutWindow[glutGetWindow()]->doGlutMouseFuncCallback(button,state,x,y);
}
void doGlutMouseFuncCallback(int button,int state,int x, int y){
...
}
}; There are some details missing (cleanup when windows are destroyed, for instance), but the basics are here, I hope you get the idea and find it useful.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thanks a lot! It really helps Have not thought about such aproach. Thanks.
Regards, Alexander
|
|
|
|
|
Well I'm no Delphi expert, but I guess the best thing to do is having the array allocated (even statically) on the Delphi client. Just like this:
var
myarray packed array [1..801] of char;
...
begin
...
TheDLLFuntion(...,@(myarray[1]),...);
...
end; where TheDLLFunction accepts a char * (a ^char in Delphi).
As I said before, I'm no expert in Delphi, so forgive me if this is off-topic (or simply incorrect).
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
PS: Has anybody had problems trying to reply other posts? For some reason I'm not being able and have been forced to post the reply on a new thread.
|
|
|
|
|
Are there API functions available to access a USB port? Just as we have CreateFile() for the serial port?
|
|
|
|
|
I wonder how to check an item in a menu?
(this code in OnInitDialog())
CMenu menu;
menu.LoadMenu(IDR_POPUP);
menu.CheckMenuItem(ID_POPUP_ALLTIDVERST,MF_CHECKED | MF_BYCOMMAND);
Isn't the the menu allready created from start?
Or do I have to overriden some function?
It wont be selected?
The menu is a tray menu...
------------------------------
©0d3 ©®4©k3® - That's me!
------------------------------
|
|
|
|
|
This menu is being created and then destroyed right away. To check or uncheck an item, you need to use the OnCmdUI handler, or you need to check it in OnContextMenu, just before you show it.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
If you're not using ON_COMMAND_UI handlers (i.e: if you have a standalone menu or are building a dialog based app), you can use CMenu::ModifyMenu() to check/disable/gray a menu item.
/ravi
"There is always one more bug..."
ravib@ravib.com
http://www.ravib.com
|
|
|
|
|
My function (in my DLL-VC) have to return to the Delphi client program a array of 801 characters . BYTE arr[801]
that 801 can include everyone from the characters that exist and even NULL s
since Delphi have not type BYTE* or CString
The problem:
If my function gets char* and try to write into,
it gives error "access invalidation..." at Delphi
If the function returns char* ,
the Delphi program don't know where the string terminate because that string can contain some NULLs that confuses the program.
How it possible that the function vc++ could to give array of characters at constant length (as 801 bytes) for Delphi program ???
|
|
|
|
|
in my Document class the function serialize... I wan,t to save my data as a monochrome bitmap so I do
void CWoopDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
CBitmap BitMapTemp;
CDC CBitmapDC;
CBitmap* OldBitMap;
CBitmapDC.CreateCompatibleDC(NULL);
void * bitmapbitstemp = malloc( Size_X*Size_Y / sizeof(BYTE));
BitMapTemp.CreateBitmap(Size_X,Size_Y,1,1,bitmapbitstemp);
OldBitMap = CBitmapDC.SelectObject(&BitMapTemp);
BITMAPFILEHEADER BmpFileH;
BITMAPINFOHEADER bmpInfoH;
BITMAP bm;
BitMapTemp.GetBitmap(&bm);
BmpFileH.biBitCount = bm.bmBitsPixel;
BmpFileH.biCompression = BI_RGB;
BmpFileH.biHeight = bm.bmHeight;
BmpFileH.biPlanes = bm.bmPlanes;
BmpFileH.biWidth = bm.bmWidth;
BmpFileH.biSizeImage = BI_RGB;
BmpFileH.biClrUsed = 0;
BmpFileH.biClrImportant = 2;
BmpFileH.biXPelsPerMeter = 0;
BmpFileH.biYPelsPerMeter = 0;
BmpFileH.biSize = sizeof(BmpFileH);
bmpInfoH.bfOffBits = sizeof(bmpInfoH)+sizeof(BmpFileH);
bmpInfoH.bfSize = bm.bmWidthBytes;
bmpInfoH.bfType = ('M' << 8 | 'B');
bmpInfoH.bfReserved1 = 0;
bmpInfoH.bfReserved2 = 0;
ar.Write(&BmpFileH,sizeof(BmpFileH));
ar.Write(&bmpInfoH,sizeof(bmpInfoH));
ar.Write(bm.bmBits,bm.bmWidthBytes);
CBitmapDC.SelectObject(OldBitMap);
delete bitmapbitstemp;
}
else
{
}
}
BitMapTemp.Serialize(ar); //do not work...
any help will be fine...
Remi Morin
Rmorin@Operamail.com
Remi.Morin@Lyrtech.com
|
|
|
|
|
Are you sure you're drawing in the correct map? Using CBitMapDC->LineTo etc?
It sounds as if there is nothing in BitMapTemp.
Hope this helps,
Bill
|
|
|
|
|
Hi, all, I have problem doing a replacement of text in my text editor using
CRichEditView class. I want to replace some text that the user types but
have no idea what functions I can use. I am aware of the
GetRichEditCtrl( ).Paste, which can paste a copied text at the position of
the caret, but how do I insert a not clipboard stuff, such as a CString
object.
Peace, no war
|
|
|
|
|
Hi, all, I'm programming a text editor with spell check using MFC's
CRichEditView class. Anybody knows how to move the caret?
My usage is that when the user types a word that is found spelled wrong, the
editor has to move its caret to the word's beginning position and replace
the word. I'm new to Windows programming. I have no idea how to move the
caret and replace the word. I tried typing SetCaretPos( ) and ShowCaret( )
in the CMainFrame derived from CFrameWnd, but it didn't actually move the
caret. The caret seemed to change its postion to the one I set in
SetCaretPos( ), but when I used GetRichEditCtrl( ).Paste to paste a text for
a test, the text was pasted at the position as if I didn't do the
SetCaretPos( ).
Peace, no war
|
|
|
|
|
try :
GetRichEditCtrl().SetSel(caretPos, caretPos);
|
|
|
|
|
Thanks, squizz, this is great.
|
|
|
|
|
hi, all
i derived a class from CObject, and also let it support "runtime type information"(RTTI), so i did these:
// Derived.h
class CDerived : public CObject{
DECLARE_DYNAMIC(CDerived)
public:
CDerived(){};
~CDerived(){};
}
// Derived.cpp
IMPLEMENT_DYNAMIC(CObject, CDerived)
but there are a warning and a error:
Derived.cpp(19) : warning C4273: 'public: static struct CRuntimeClass const CObject::classCObject' : inconsistent dll linkage. dllexport assumed.
Derived.cpp(19) : error C2491: 'CObject::classCObject' : definition of dllimport static data member not allowed
can you tell me why?
thank you!
|
|
|
|
|
Hello Grant,
I think you make a little mistake:
you typed:
IMPLEMENT_DYNAMIC(CObject, CDerived)
instead:
IMPLEMENT_DYNAMIC(CDerived, CObject)
Hope that help!
|
|
|
|
|
Thank you very much, thank you all.
such a silly question, ha...
|
|
|
|
|
RTTI information is a feature of the C++ language that requires no M$ macros or classes. Just use dynamic_cast, and if the return is NULL, the object was not of the type you were casting to.
The M$ version, like the M$ container classes, were a stopgap, not a replacement.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
Has anybody used MySQL via MFC? All I need to do first off is get a noddy database working programmaticly using MySQL in MFC. eg
Name Table:-
(Unique ID,Surname,Forename,DOB,UniqueKey)
Address Table:-
(Unique ID,LineOne,District,Town,County,PostCode)
Contact Table:-
(UniqueID,Number,Type)
Can anybody do this in their coffee break? Im stuck where to start is the problem. Im sure its a doddle though.
"Life begins at 140"
|
|
|
|
|
Really what is the need to repost? I answered your first question. Perhaps you should follow my advice. Read MSDN a little too. People here are happy to help, but not to do your work for you.
Sorry to dissapoint you all with my lack of a witty or poignant signature.
|
|
|
|
|