_begintheadex function call problem - c++

I have a class SoundManager which contains a function called 'recordLoop'. In the constructor of the SoundManager, I am using this code:
recordHandle = (HANDLE)_beginthreadex(NULL,0,recordLoop,
(void*)exinfo->length,CREATE_SUSPENDED,0);
It is giving me the following errors:
error C3867: 'SoundManager::recordLoop': function call missing argument list; use '&SoundManager::recordLoop' to create a pointer to member
IntelliSense: argument of type "unsigned int (__stdcall SoundManager::*)(void *params)" is incompatible with parameter of type "unsigned int (__stdcall *)(void *)"
So I've tried using the &SoundManager::recordLoop as suggested, but it gives me this:
error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int (__stdcall SoundManager::* )(void *)' to 'unsigned int (__stdcall *)(void *)'
IntelliSense: argument of type "unsigned int (__stdcall SoundManager::*)(void *params)" is incompatible with parameter of type "unsigned int (__stdcall *)(void *)"
Is it illegal to start a thread on a class method or did I do something wrong?
Thanks in advance
EDIT: Sorry forgot to add the recordLoop >.< here it is:
public:
unsigned __stdcall recordLoop(void* params);

It's illegal to start a thread on a non-static class member since there is no way for the created thread to know what this is.
What is the definition of recordLoop?

I had the same problem with casting.
Ignoring all other problems like one mentioned in the answer above, function pointer must be cast to (unsigned(__stdcall*)(void*)) in _beginthreadex, no matter what type the function is or what is its parameter list.

Related

What is the correct function pointer for an unsigned WINAPI function?

I've a function declared in this way:
unsigned WINAPI searchSTR(void *j);
And I need a pointer to this function. My idea was:
unsigned (*pointerF) (void*);
pointerF = &searchSTR;
But there is an error:
"1 error C2440: '=' : cannot convert from 'unsigned int (__stdcall *)(void *)'
to 'unsigned int (__cdecl *)(void *)' ".
I tried other sintax, but nothing seems correct, he doesn't like the word WINAPI.
Can you suggest me the correct syntax? Maybe it is easy but I am blocked ! Thanks to all
The WINAPI macro expands to __stdcall, which is a different calling convention from the default __cdecl. You need to mark your function pointer with the calling convention to use:
unsigned (WINAPI *pointerF) (void*)
auto pointerF = &searchSTR;
Stop writing 1990's code. The compiler already knows the right type.

C++ Defining function pointer

I am trying to create a function pointer to another function in c++.
This is what I have so far:
LONG (*function)(LPSTR,LPVIPERVAR4,LONG)=&CWilExtender::DllVarHandler;
When I try to compile my program, I get this error:
.\MyExtender.cpp(132) : error C2440: 'initializing' : cannot convert from
'LONG (__thiscall CWilExtender::* )(LPSTR,LPVIPERVAR4,LONG)' to
'LONG (__cdecl *)(LPSTR,LPVIPERVAR4,LONG)'
There is no context in which this conversion is possible
I don't know how the DllVarHandler was defined, and I don't know how to reproduce the type for the function pointer.
How do I change the (_cdecl *) to match (__thisscall CWilExtender::*)?
Specifically, what does LONG (__thiscall CWilExtender::* )(LPSTR,LPVIPERVAR4,LONG) mean and how do I write that as the function pointer's type?
Thanks.
Thanks to the comments by #OliCharlesworth and #user814628, I solved my problem.
The correct code should be:
LONG (CWilExtender::* function)(LPSTR,LPVIPERVAR4,LONG)=&CWilExtender::DllVarHandler;
Thanks for being so quick to help!

c++ pointers to overloaded functions

I'm trying to expose a overloaded function using boost::python.
the function prototypes are:
#define FMS_lvl2_DLL_API __declspec(dllexport)
void FMS_lvl2_DLL_API write(const char *key, const char* data);
void FMS_lvl2_DLL_API write(string& key, const char* data);
void FMS_lvl2_DLL_API write(int key, const char *data);
I'v seen this answer: How do I specify a pointer to an overloaded function?
doing this:
BOOST_PYTHON_MODULE(python_bridge)
{
class_<FMS_logic::logical_file, boost::noncopyable>("logical_file")
.def("write", static_cast<void (*)(const char *, const char *)>( &FMS_logic::logical_file::write))
;
}
results with the following error:
error C2440: 'static_cast' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(const char *,const char *)'
None of the functions with this name in scope match the target type
trying the following:
void (*f)(const char *, const char *) = &FMS_logic::logical_file::write;
results:
error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(const char *,const char *)'
None of the functions with this name in scope match the target type
what's wrong and how to fix it?
EDIT
I forgotten to mention a few things:
I'm using vs2010 pro on win-7
write is a member function of logical_file
FMS_logic is a namespace
Well the second attemp should work, if write is a pure function. From your code it seems you do have a memberfunction. Pointers to member-functions are ugly, you'd rather use a function object.
However: you would have to post the whole code, it is not clear whether write is a member-function or not.
Edit: if it is a member-function of FMS_logic::logical_file the syntax would be:
void (FMS_logic::logical_file::*f)(const char *, const char *) = &FMS_logic::logical_file::write;
This just applies for non-static member function, i.e. if a function is static or logical_file is just a namespace it is as you wrote it before.
Your code doesn't work because your function pointer type is wrong. You need to include all type qualifiers (your DLL qualifier is missing) and, as Klemens said, the class name. Putting this together, your code should read
.def("write", static_cast<void FMS_lvl2_DLL_API
(FMS_logic::logical_file::*)(const char *, const char *)>
(&FMS_logic::logical_file::write))
Thanks for the hint with the static_cast<>, I had the same problem as you, just without the dllexport, and after adding the static_cast it works :-)

create multi-thread

I use Multi-thread method in vs2008 ,use c++ language. when I use _beginthreadex function, I got the follow error:
error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int
(__stdcall *)(void *)' to 'unsigned int (__stdcall *)(void *)'
ps: I use the template on the threadFunc like this:
template<class T>
unsigned int WINAPI closingReconstruction_ThreadFunc(void* pvPara)
{...}
,and then i creat the thread
_beginthreadex(NULL,0,closingReconstruction_ThreadFunc<T>,(PVOID)(tPara+i),0,NULL)
so i get this error , of course , if I remove the template ,it can be work,but i need to use template, are there any methods to solve this.
You forgot to mention the immediately preceding error message,
error C2065: 'T' : undeclared identifier
Let me just state that looking at the first error message first, is generally a good idea.
Cheers & hth.,

C++ Missing type specifier: syntax error

I hook a function with windows detours in C++.
I get an error in following code:
void (*asmFunction)(const char *text);
void hookFunction(const char *text) {
__asm nop;
asmFunction(text);
}
asmFunction = (void (__cdecl *)(const char *))DetourFunction((PBYTE)0x433A90, (PBYTE)&hookFunction);
The compiler (MSVC++ 2008) says:
error C4430: Missing type specifier - int assumed. Hint: "default-int" is not supported in C++. Yadda yadda …
error C2373: 'asmFunction': redefinition with different specifiers
error C2440: 'in initialization': 'void (__cdecl *)(const char *)' cannot be converted to 'int'. There is no context in which this conversion is valid.
The code worked yesterday. What's wrong with it? How can I fix it without destructing the hook?
This expression needs to be within a function, e.g.
int main() {
asmFunction = (void (__cdecl *)(const char *))DetourFunction(
(PBYTE)0x433A90, (PBYTE)&hookFunction
);
// ...
}
Go read a book on C++.