0
votes

//In A.h I have the following code

include "afxwin.h"

include "msclr\auto_gcroot.h"

using namespace System;

using msclr::auto_gcroot;

namespace A

{

public ref class A

{

public:

virtual bool Func();

A();

~A();

virtual bool Connect();

protected:

DWORD WINAPI threadConnect(void* pParam);

};

public class AHelper

{

public:

auto_gcroot A;

};

}

//In A.cpp I have below code

// This is the main DLL file.

include "stdafx.h"

include "A.h"

include "string"

include "sstream"

include "stdlib.h"

include "strsafe.h"

include "windows.h"

include "tchar.h"

namespace A

{

A::A()

{

m_FuncHandle = mpsNil;

}

A::~A()

{

}

bool A::Func()

{

return true;

}

bool A::Connect()

{

AHelper* AHelper;

m_retVal = false;

AHelper = new AHelper();

AHelper->A = this;

HANDLE Handle_Of_Thread = 0;

DWORD dwThreadId;

//DWORD WINAPI threadConnect(void* pParam);

//If I declare the function declaration here I am getting

//error LNK2001: unresolved external symbol "unsigned long __stdcall threadConnect(void *)" (?threadConnect@@YGKPAX@Z)

Handle_Of_Thread = CreateThread (NULL, 0, threadConnect, AHelper, 0, &dwThreadId); // with this code I am getting

//error C3867: 'A::A::threadConnect': function call missing argument list; use '&A::A::threadConnect' to create a pointer to member

return m_retVal;

}

DWORD WINAPI A::threadConnect(void* pParam)

{

AHelper* AHelper = reinterpret_cast(pParam);

//Here I need to call Func

return 0;

}

}

1
What is that ref thing? It certainly is not standard C++. Also, you can just call any function you like in a thread, there is no restriction in C++ generally.PlasmaHH

1 Answers

1
votes

the usual trick is to have a static function that takes an id of some sort, this function determines which someFunc() to call (as each object will have its own someFunc, you obviously need to know which one) either using a lookup, or as is common in C/C++ apps, by passing the address of the object directly.

so you have something like:

static bool thread_func(object o)
{
    return o->someFunc();
}

The trick is that the static function must be reentrant, so holds no state itself for the threads to interfere with (or if it does, make sure its thread safe)

That all supposes you're calling a method on an object that was not created within the thread. If you're just calling a function and you already have the object created within your thread, just call the function!