|
That landed me in boost territory. It was a little daunting at first but here's what I came up with.
#include "stdafx.h"
#include <boost bind.hpp="">
#include "debugstr.h"
class CTest
{
public:
void Test(const char* str)
{
printf("member function: %s", str);
}
};
template <class f="">
void foo(F f)
{
for (int i=0; i<10; i++)
{
char buf[128] = {0};
sprintf(buf, "test %i\n", i);
f(buf);
}
}
void Test(const char* str)
{
printf("global function: %s", str);
}
int main(int argc, char* argv[])
{
CTest test;
foo(boost::bind(&CTest::Test, boost::ref(test), _1));
foo(boost::bind(Test, _1));
return 0;
}
Todd Smith
CPUA 0x007 ... shaken not stirred
|
|
|
|
|
It is doable, but it is not correct unless your member function is a staic one. Why do not pass pointer to that class as a parameter to foo() and inside foo() call p->DoSomething()
soptest
|
|
|
|
|
Well if you can keep the same parameters for the function then this might work
template <typename T>
class FunctionPointer {
typedef void (T::*FuncPtr)(int);
FunctionPointer( T* src, FuncPtr func ):m_source( src ), m_func(func) {};
void doit( int x ) {
(m_source->*m_func)(x)
}
FuncPtr m_func;
T* m_source;
};
template <typename T>
void foo(FunctionPointer<T> fp )
{
int x = 0;
for (int i=0; i<10; i++) {
fp.doit(x);
}
}
MyClass::blah()
{
foo( FunctionPointer<MyClass>(this, DoSomething) );
}
would this work ?
|
|
|
|
|
Hi All,
I'm deriving a view class from the CHtmlView, and sticking this in a child frame window of an MDI app. Anyway, I've got a toolbar with a forward and backward arrow for going forward and backward through the CHtmlView's history list. Going forward and back is simple enough (through CHtmlView::GoForward, and CHtmlView::GoBack), but how can I determine if there are previous entries or next entries in the history list of the browser from the current page? What I need is something like a CanGoBack or a CanGoForward method so I can update the buttons on the toolbar in the CmdUI handler.
Thanks,
Aaron
|
|
|
|
|
keybord event can generate a keystroke ,but how do you direct this keystroke to a particular window or its message queue.
|
|
|
|
|
I posted this a while back, and have been meaning to clean it up and offer it to the god of FAQ - maybe it will help.
|
|
|
|
|
I am sorry,I could not locate anything on 'this' link.
|
|
|
|
|
Ok - here is the example I posted, which should be made into a SendKeys type function. Here I hardcoded a known window handle - (Calculator).
Basically, the trick is to post messages for keydown and keyup, and its nice to format the lparam properly.
#include "stdafx.h"
#include "crtdbg.h"
#include "conio.h"
#include "windows.h"
#include "winbase.h"
int main(int argc, char* argv[])
{
HWND hwnd = (HWND) 0x025c;
INT Vk;
INT Scan;
CHAR character[2];
CHAR oemchar[2];
CHAR ch;
do {
while(!_kbhit());
ch = getch();
character[0]=ch;
character[1]='\0';
Vk = VkKeyScan(character[0]) & 0xFF;
CharToOem(character, oemchar);
Scan = OemKeyScan(oemchar[0]) & 0xFF;
printf("Key = %c, VK code = %d, scan = %d\n", ch, Vk, Scan);
UINT lKeydata = 0;
UINT mask = 1;
lKeydata |= mask;
lKeydata |= Scan << 15;
::PostMessage(hwnd, WM_KEYDOWN, Vk, lKeydata);
lKeydata |= mask;
lKeydata |= Scan << 15;
lKeydata |= mask << 31;
::PostMessage(hwnd, WM_KEYUP, Vk, lKeydata);
}
while(1);
return 0;
}
|
|
|
|
|
thanks!! it is a great help
|
|
|
|
|
Hi everybody,
Is ther a way [read: a program] to find out what functions a dll is exporting? Is there a tool with MVS that can do this?
To learn more about this I chose the default implementation of a sample dll in VC++: New->Projects->Win32 dynamic link-library.
Now, there's a test function called fnDllTest (my project is called DllTest) which is exported like this:
__declspec(dllexport) int fnDllTest(void)
{
return 42;
}
Now, I created a simple console application which should call this dll function. I loaded the dll using LoadLibrary and that works fine... BUT, now I wish to get the function fnDllTest through the use of GetProcAddress but I can't seem to get it working!
If the dll and the console app loading the dll are residing in the same place then the call to GetProcAddress should look like this right?
blabla = (blabla)GetProcAddress(libPtr, "fnDllTest");
where libPtr is the return value of LoadLibrary... What kind of type should blabla be of?
I haven't found any documentation on how to call dll exported functions...
Thx for all your help,
/Tommy
|
|
|
|
|
find the tool called DUMPBIN. it will tell you the function names. i'm not sure if it can tell you the params, tho.
-c
Smaller Animals Software, Inc.
Few things are harder to put up with than the annoyance of a good example.
Mark Twain
|
|
|
|
|
Thx!
I tried and this was the result:
...
ordinal hint RVA name
1 0 0000100A ?fnDllTest@@YAHXZ
...
What does this tell me? I can't very likely do like this, or...?
... GetProcAddress(libPtr, "?fnDllTest@@YAHXZ");
I mean, there must be a way for me to know what the exporting function name will look like when it is me writing them...?
THx for your help!
/Tommy
|
|
|
|
|
blabla will be a function pointer that is the same type as the function that you are loading. This is why it is hard to use functions in a dll without a header file or good documentation.
In the example that you have given, here would be the function pointer that you would declare:
int (fnDllTestPtr*)(void);
fnDllTestPtr becomes the name of the function pointer type. To use this you would now call this:
fnDllTestPtr fnDllTest = (fnDllTestPtr)GetProcAddress(libPtr, "fnDllTest);
As far as determining which functions are exported pragmatically, you will need to traverse the PE file structure of the DLL in order to read the exports section. Lookup Mike Peitrics article on the PE file format. He created a smaple program called PEDump.
|
|
|
|
|
No, that can't be right...
It crashed when I tried to do that... I mean, shouldn't there be a typedef somewhere?
|
|
|
|
|
Hi again,
Shouldn't it be:
int (*fnDllTestPtr)(void);
and then
fnDllTestPtr = (???)GetProcAddress(libPtr, "fnDllTest");
but I don't know what to put in '???'...
/Tommy
|
|
|
|
|
I am sorry, this is the correct declaration for the function pointer. Typedef it so that you can use it later.
typedef int(*fnDllTestPtr)(void);
Also, if you do not encapsulate the function that you are exporting with extern "c"{ }, then the function will be name mangled, and you will have to include the mangled name in order to load it. So for the function that you have sent in, it should be managled like this:
?fnDllTest@@YAHXZ
Before you attempt to call the function, you should check the return value from GetProcAddress to insure that the function was found and properly pointed to. If the function fails, it will return NULL.
Here is the code that you should use.
typedef int(*fnDllTestPtr)(void);
fnDllTestPtr fp = (fnDllTestPtr)::GetProcAddress(libPtr, "?fnDllTest@@YAHXZ");
if (NULL == fp)
{
MessageBox(NULL, "an error has occurred. The function did not load.", "error", MB_OK);
}
else
{
int i = fp();
}
|
|
|
|
|
Thx Kilowatt! It worked!
But how on earth can it be possible to conclude the above code from the documentation?!?!
First of all; how can this assignment work? Isn't fnDllTestPtr a function pointer variable...?
But again, thx!
/Tommy
|
|
|
|
|
Move casting to left side of assignment:
EDIT - it should be FARPROC &
(FARPROC & )fnDllTestPtr = GetProcAddress(...);
Tomasz Sowinski -- http://www.shooltz.com
- It's for protection - Protection from what? Zee Germans?
|
|
|
|
|
Use Dependency Walker (depends). There is a (usually old) version in the Platform SDK. You can get the latest version from http://www.dependencywalker.com.
|
|
|
|
|
The function name in a DLL can be viewed using Dependency Walker that comes along with Microsoft Developer Studio 6.0. It shows as Depends in the tools panel of Developer studio
Sanjay Mishra
|
|
|
|
|
Background...
I'm using the msado15.dll ADO type library for database access.
I'm using a class derived from CADORecordBinding to bind to a _RecordsetPtr object, which I'm using to update a list view with. The class is given below.
I'm currently reading the MailID field out of the database as an adVariant type, but it's actually an adGUID type.
The reason I'm reading as an adVariant, is that I'm not sure how to store an adGUID type as a c/c++ variable.
I'm a bit new to all this, so please forgive my greenness!
Destroying the CMessageHeadersCache Object...
You'll notice that there are COleVariant member variables in the CMessageHeadersCache class. When I call delete on objects this class, the program crashes! When the COleVariant member variables are absent, deallocation goes just fine! I'm sure that there must be some safe/correct/appropriate way of deallocating these objects containing COleVariants, but I'm a bit stuck! Please help!
Many thanx,
funBag
#pragma once
#include "icrsint.h"
// ============================================================================
// Class CMessageHeadersCache
// Purpose ADO Record binding for the Messsage Headers list view object.
// History funbag 24th March 2002 Initial Rev.
// ============================================================================
class CMessageHeadersCache : public CADORecordBinding
{
public:
CMessageHeadersCache();
~CMessageHeadersCache();
BEGIN_ADO_BINDING(CMessageHeadersCache)
// DateTimeReceived
ADO_VARIABLE_LENGTH_ENTRY2(1,
adVariant,
DateTimeReceived,
sizeof(DateTimeReceived),
status[0],
FALSE)
ADO_VARIABLE_LENGTH_ENTRY2(2,
adVarChar,
SenderMailAddress,
sizeof(SenderMailAddress),
status[1],
FALSE)
ADO_VARIABLE_LENGTH_ENTRY2(3,
adVarChar,
SubjectLine,
sizeof(SubjectLine),
status[2],
FALSE)
ADO_VARIABLE_LENGTH_ENTRY2(4,
adVarChar,
Opened,
sizeof(Opened),
status[3],
FALSE)
ADO_VARIABLE_LENGTH_ENTRY2(5,
adVariant,
MailID,
sizeof(MailID),
status[4],
FALSE)
END_ADO_BINDING()
public:
COleVariant DateTimeReceived;
char SenderMailAddress[100];
char SubjectLine[1024];
char Opened[2];
COleVariant MailID;
ULONG status[5];
};
skydiving....if at first you don't succeed, you're fecked!
|
|
|
|
|
How to get a pointer of the view object into mainframe?
|
|
|
|
|
This code is by Craig Henderson:
CView *GetActiveView(void)
{
CFrameWnd *pFrameWnd;
pFrameWnd = (CFrameWnd *)AfxGetMainWnd();
if (pFrameWnd != NULL && pFrameWnd->IsKindOf(RUNTIME_CLASS(CMDIFrameWnd)))
pFrameWnd = ((CMDIFrameWnd *)pFrameWnd)->GetActiveFrame();
return (pFrameWnd == NULL)? NULL : pFrameWnd->GetActiveView();
}
This function will always return the active view in an SDI or MDI application.
Mazy
"So,so you think you can tell,
Heaven from Hell,
Blue skies from pain,...
How I wish,how I wish you were here." Wish You Were Here-Pink Floyd-1975
|
|
|
|
|
Also you can check this
Mazy
"So,so you think you can tell,
Heaven from Hell,
Blue skies from pain,...
How I wish,how I wish you were here." Wish You Were Here-Pink Floyd-1975
|
|
|
|
|
Hi,
Im trying to profile some code to determine where to concentrate in order to speed things up. Obviously i want time statistics. This is the first time ive tried to use profiling. I've successfully generated a pbt file, and im trying to convert this to a text file using PLIST. I can produce function coverage output fine, but whenever i attempt to produce time statistics, PLIST gives me the following error.
PLIST : warning PRF4481: clock frequency is zero
I can assure you, my clock frequency is not zero....Does the profiler have difficulties with GHz machines, or am i doing something stupid or....?
Any suggestions are welcome.
Thanks,
Alex
|
|
|
|
|