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

Managed C++/CLI

 
AnswerRe: Casting a function pointer Pin
Nish Nishant26-Oct-10 10:03
sitebuilderNish Nishant26-Oct-10 10:03 
QuestionMapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
alleyes25-Oct-10 14:33
professionalalleyes25-Oct-10 14:33 
AnswerRe: Mapping a function pointer (CALLBACK) to a delegate to use in BacgroundWorker function parameter Pin
Luc Pattyn25-Oct-10 14:45
sitebuilderLuc Pattyn25-Oct-10 14:45 
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 
Handling the Backgroundworker!
At the risk of being wordy, let me give a little more detail. It may help.

In native code, I have a callback used as a percent complete mechanism. Its signature is below:
typedef	bool (APIENTRY *CB_UpdateStatus)(unsigned PercentComplete);


The function is below:
bool APIENTRY UpdateStatus(unsigned PercentComplete)
{
   printf("\r%d%%...", PercentComplete);
   return false;
}



Passed as an arg to a native function: HRESULT Result = NativeFileRead(Size, Data, UpdateStatus);


I need to wrap this function so it can be used as-is in native code but when wrapped will allow use in a graphical program by use of the background worker to get status back to a UI.

I modified the function pointer signature:
typedef	bool (APIENTRY *CB_UpdateStatus)(unsigned PercentComplete, void* context);



and its use by the calling function:
bool APIENTRY UpdateStatus(unsigned PercentComplete, void* context)
{
   printf("\r%d%%...", PercentComplete);
   return false;
}



The native function that uses the callback now has an added parameter "void* context" - it's signature is below:
DLLAPI HRESULT APIENTRY NativeFileRead(size_t DataSize, BYTE* Data, CB_UpdateStatus UpdateStatus, void* context)


In native code I just pass a NULL so it causes no harm during debug and development. In managed code I need to wrap the function and use a BGW.

In my wrapper class I have this function signature:
Wrapper::FileRead(UInt32 DataSize, String^ managedFileHandle, BackgroundWorker^ worker)



My callback in the wrapper class now looks like:
bool __stdcall UpdateStatus(unsigned PercentComplete, void* context)
{
   BackgroundWorker^ worker = static_cast<BackgroundWorker^>(&context);
   worker->ReportProgress(PercentComplete);
      //Console::Write("\r{0}...", PercentComplete);
   return false;
}



I want to pass in the worker to the native function.
HRESULT Result = NativeFileRead(DataSize, Data, UpdateStatus, context);



The problem is now handling the BGW for a function that expects a void pointer.

Thanks Nish
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 
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 

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.