Click here to Skip to main content
15,899,679 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: Mapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
alleyes25-Oct-10 14:55
professionalalleyes25-Oct-10 14:55 
GeneralRe: Mapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
Luc Pattyn25-Oct-10 15:14
sitebuilderLuc Pattyn25-Oct-10 15:14 
GeneralRe: Mapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
alleyes25-Oct-10 15:18
professionalalleyes25-Oct-10 15:18 
AnswerRe: Mapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
Nish Nishant26-Oct-10 10:02
sitebuilderNish Nishant26-Oct-10 10:02 
GeneralRe: Mapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
alleyes27-Oct-10 3:43
professionalalleyes27-Oct-10 3:43 
GeneralRe: Mapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
Nish Nishant27-Oct-10 4:01
sitebuilderNish Nishant27-Oct-10 4:01 
GeneralRe: Mapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
alleyes27-Oct-10 4:31
professionalalleyes27-Oct-10 4:31 
GeneralRe: Mapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
Nish Nishant27-Oct-10 5:24
sitebuilderNish Nishant27-Oct-10 5:24 
alleyes wrote:
Is that what you are suggesting? How does the native callback return percentcomplete through a BGW? I still need a native callback that takes the two args. Not real clear then


I quickly put together a small sample that should clarify what I am talking about. It's simplified code but you should get the gist:

mcpp
typedef	bool (*CB_UpdateStatus)(unsigned PercentComplete);

HRESULT NativeFileRead(size_t DataSize, BYTE* Data, CB_UpdateStatus UpdateStatus)
{
	UpdateStatus(100);
	return 0;
}

bool UpdateStatus(unsigned PercentComplete)
{   
	printf("From native code: %d\r\n", PercentComplete);   
	return false;
}

ref class Test
{
private:
	BackgroundWorker^ worker;

public:
	delegate bool ManagedUpdateStatus(unsigned int);

	int FileRead(int dataSize, array<byte>^ data, ManagedUpdateStatus^ updateStatus, BackgroundWorker^ worker)
	{
		pin_ptr<ManagedUpdateStatus^> tmp =  &updateStatus;
		NativeFileRead(0, NULL, (CB_UpdateStatus)Marshal::GetFunctionPointerForDelegate(updateStatus).ToPointer());  

		return 0;
	}

	void Foo() 
	{
		worker = gcnew BackgroundWorker();
		worker->WorkerReportsProgress = true;
		worker->DoWork += gcnew DoWorkEventHandler(this, &Test::Worker_DoWork);
		worker->ProgressChanged  += gcnew ProgressChangedEventHandler(this, &Test::Worker_ProgressChanged);
		worker->RunWorkerAsync();
	}
	
	bool DoManagedUpdateStatus(unsigned int percentage)
	{
		worker->ReportProgress(percentage);
		return false;
	}

	void Worker_DoWork(Object^ sender, DoWorkEventArgs^ e)
	{
		FileRead(0, nullptr, gcnew ManagedUpdateStatus(this, &Test::DoManagedUpdateStatus), (BackgroundWorker^)sender);
	}

	void Worker_ProgressChanged(Object^ sender, ProgressChangedEventArgs^ e)
	{
		Console::WriteLine("From the worker thread : {0}", e->ProgressPercentage);
	}
};

int main(array<System::String ^> ^args)
{
	NativeFileRead(0, NULL, UpdateStatus);
	(gcnew Test())->Foo();
	Console::ReadKey();
	return 0;
}

Regards,
Nish
My technology blog: voidnish.wordpress.com

Code Project Forums : New Posts Monitor
This application monitors for new posts in the Code Project forums.

GeneralRe: Mapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
alleyes27-Oct-10 7:43
professionalalleyes27-Oct-10 7:43 
GeneralRe: Mapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
Nish Nishant27-Oct-10 7:55
sitebuilderNish Nishant27-Oct-10 7:55 
GeneralRe: Mapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
alleyes28-Oct-10 2:40
professionalalleyes28-Oct-10 2:40 
GeneralRe: Mapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
Nish Nishant28-Oct-10 3:47
sitebuilderNish Nishant28-Oct-10 3:47 
GeneralRe: Mapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
alleyes28-Oct-10 6:10
professionalalleyes28-Oct-10 6:10 
GeneralRe: Mapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
Nish Nishant28-Oct-10 8:27
sitebuilderNish Nishant28-Oct-10 8:27 
QuestionIs this the fastest way to access the RGB values of a Pixel ? Pin
inayathussaintoori23-Oct-10 23:56
inayathussaintoori23-Oct-10 23:56 
AnswerRe: Is this the fastest way to access the RGB values of a Pixel ? Pin
Nish Nishant24-Oct-10 6:38
sitebuilderNish Nishant24-Oct-10 6:38 
AnswerRe: Is this the fastest way to access the RGB values of a Pixel ? Pin
Nish Nishant24-Oct-10 6:40
sitebuilderNish Nishant24-Oct-10 6:40 
GeneralRe: Is this the fastest way to access the RGB values of a Pixel ? Pin
inayathussaintoori24-Oct-10 7:57
inayathussaintoori24-Oct-10 7:57 
GeneralRe: Is this the fastest way to access the RGB values of a Pixel ? Pin
Nish Nishant24-Oct-10 8:03
sitebuilderNish Nishant24-Oct-10 8:03 
Questionreplacing the default list box with custom made list box Pin
emmmatty123-Oct-10 8:54
emmmatty123-Oct-10 8:54 
AnswerRe: replacing the default list box with custom made list box Pin
Nish Nishant23-Oct-10 11:49
sitebuilderNish Nishant23-Oct-10 11:49 
Questiontwo dimension pointers Pin
hasani200722-Oct-10 10:00
hasani200722-Oct-10 10:00 
AnswerRe: two dimension pointers Pin
Nish Nishant23-Oct-10 5:03
sitebuilderNish Nishant23-Oct-10 5:03 
QuestionHow to start interrupted detection process again Pin
rajneshmalik21-Oct-10 2:00
rajneshmalik21-Oct-10 2:00 
AnswerRe: How to start interrupted detection process again Pin
Nish Nishant21-Oct-10 2:21
sitebuilderNish Nishant21-Oct-10 2:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.