|
I have a function pointer with the following signature:
typedef bool (__stdcall *CB_CheckStatus)(unsigned);
When used as follows in a native app.
bool __stdcall UpdateStatus(unsigned pctCompl)
{
printf("\r%d%%...", pctCompl);
return false;
}
And as an parameter in a native function which uses it:
DLLAPI HRESULT __stdcall ReadFile(size_t Size, BYTE* Buff, CB_CheckStatus UpdateStatus)
Can I change the parameter as a void parameter? I want to map a delegate to this function pointer so I can utilize a BackgroundWorker for a mixed-mode wrapper
How would you do that?
Thanks
|
|
|
|
|
|
I know that seemed like a bit vague, let me explain. I have in native code a callback that is used for passing to a function for means of acting as a percent complete mechanism in a native console app. The signature is as follows:
typedef bool (APIENTRY *CBUpdateValue)(unsigned PercentComplete);
That is passed as a parameter to a function that uses it for file reads and writes:
DLLAPI HRESULT APIENTRY GetFile(size_t FileSize, BYTE* Data, CBUpdateValue CompletStatus)
In the native code that uses the callback, an example of the use of the callback is
bool APIENTRY UpdateStatus(unsigned PercentComplete)
{
printf("\r%d%%...", PercentComplete);
return false;
}
The goal is to use this signature, but replace the printf call to send information to a BackgroundWorker object. It takes an unsigned and returns a bool. Because of this constraint, is it possible to use this for a BGW? I realize I have to map the function pointer to a delegate. Has anyone used something like this?
Thanks in advance
|
|
|
|
|
Hi,
this article[^] of mine explains P/Invoke, including native code calling callbacks. While all examples use C# or VB.NET, similar things are possible using C++/CLI. Pay special attention to the STDCALL keyword.
|
|
|
|
|
I should have mentioned that I am using IJW. The article provided no help but thanks anyway
|
|
|
|
|
OK. Seems like IJW is a bit of a misnomer then?
Anyway, managed methods are always called in STDCALL (there is no way to change that), your native code must adhere to that.
|
|
|
|
|
Well perhaps, but compared to using a P/Invoke solution, it seems more natural to use C++/CLI especially when interoperation with native code is required.
|
|
|
|
|
So what part are you stuck on? Converting between the function pointer/delegate? Or handling the background worker?
|
|
|
|
|
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);
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
|
|
|
|
|
I don't understand why FileRead is passed a BackgroundWorker argument. Shouldn't FileRead be called from the worker thread's DoWork event?
And secondly, you cannot pass a managed reference through a void*. Instead create a delegate with managed arguments, and convert that to a function pointer before invoking the native FileRead method (so when the native FileRead invokes the native callback, what gets called eventually is your managed delegate handler). In the managed version, make the 2nd argument an Object^ (so that it can represent a BackgroundWorker). Hope that made sense.
|
|
|
|
|
FileRead is called from the DoWork event in the C# app. Just left out. As to the second...
In the wrapper.h:
delegate bool UpdateProgressDelegate(unsigned int, Object^);
Wrapper::FileRead(....)
UpdateProgressDelegate^ upd = gcnew UpdateProgressDelegate(UpdateStatus);
GCHandle gch = GCHandle::Alloc(upd);
IntPtr ip = Marshal::GetFunctionPointerForDelegate(upd);
CB_UpdateStatus update = static_cast<HD_CB_UpdateStatus>(ip.ToPointer());
Then calling the native function:
Result = NativeFileRead(DataSize, Data, UpdateStatus &context);
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
|
|
|
|
|
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:
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;
}
|
|
|
|
|
Thank you Nish, I get the gist.
The signature: typedef bool (*CB_UpdateStatus)(unsigned PercentComplete, void* context); is what I am pursuing although I appreciate the suggestion. Isn't it possible to box a managed object and get a pointer to it?
|
|
|
|
|
alleyes wrote: Isn't it possible to box a managed object and get a pointer to it?
It may be theoretically doable with some hacks, pinning, gchandles, manually marshalling/copying data-bytes etc. but I've never seen anyone marshal a managed object through a native callback via a native pointer and then marshal it back to a managed object. In your example, there's no real need to do it that way, unless what you showed me was a very simplified version, and your actual code is way more complicated and for some reason cannot access a member field?
|
|
|
|
|
gcroot/auto_gcroot templates?
struct MyStruct
{
msclr::auto_gcroot<BackgroundWorker^> worker;
}
In the native callback
bool APIENTRY UpdateStatus(unsigned PercentComplete, void* Context)
{
MyStruct* strct = context;
strct->worker->ReportProgress(PercentComplete);
return false;
}
In the call to the managed FileRead, pass the BackgroundWorker to the Context arg of the NativeFileRead
That's what I am talking about.
|
|
|
|
|
Okay, that means you are invoking a managed call from your "native" callback method. That defeats the purpose of wrapping this using C++/CLI in my opinion. Keep your native code native.
If for some reason you do not want to invoke the worker via the member field (maybe due to some peculiarity in your design), you could do this:
typedef bool (*CB_UpdateStatus)(unsigned PercentComplete, void* pContext);
HRESULT NativeFileRead(size_t DataSize, BYTE* Data, CB_UpdateStatus UpdateStatus, void* pContext)
{
UpdateStatus(100, pContext);
return 0;
}
struct MyStruct
{
auto_gcroot<BackgroundWorker^> worker;
}; In the ref class:
int FileRead(int dataSize, array<byte>^ data, ManagedUpdateStatus^ updateStatus, BackgroundWorker^ worker)
{
pin_ptr<ManagedUpdateStatus^> tmp = &updateStatus;
MyStruct myStruct;
myStruct.worker = worker;
NativeFileRead(0, NULL, (CB_UpdateStatus)Marshal::GetFunctionPointerForDelegate(updateStatus).ToPointer(), &myStruct);
return 0;
}
bool DoManagedUpdateStatus(unsigned int percentage, IntPtr pContext)
{
MyStruct* pMyStruct = (MyStruct*)pContext.ToPointer();
pMyStruct->worker->ReportProgress(percentage);
return false;
}
This way you native code does not need to know of the managed code, and you still end up invoking the worker's progress event.
|
|
|
|
|
Nish,
Please understand, I AM of the opinion to keep native code just that - native. My wrapper being a mixed mode design straddles both. That is why I place the native struct with manged object. Isn't your post of putting the native struct with managed member in native code outside of that perspective? There is really no peculiarity of design. The native function works fine when a NULL is passed to pContext. The native callback writes to stdout in a native console app. It is the wrapping of the native function that required an additional arg be added so the use of a background worker can be used.
Thanks very much for all the invaluable help. I wished more was covered on this topic in your book.
|
|
|
|
|
alleyes wrote: I wished more was covered on this topic in your book.
Well, hindsight is always 20-20
|
|
|
|
|
is this the fastest way to access the RGB values of a pixel of a bitmap file in c#?
the code is below
bm is previously declared Bitmap object.
Collapse
BitmapData bmd=bm.LockBits(new Rectangle(0, 0, 10, 10), System.Drawing.Imaging.ImageLockMode.ReadOnly, bm.PixelFormat);
int PixelSize=4;
for(int y=0; y<bmd.Height; y++)
{
byte* row=(byte *)bmd.Scan0+(y*bmd.Stride);
for(int x=0; x<bmd.Width; x++)
{
row[x*PixelSize]=255;
}
}
OR is there more faster way exists to access the RGB values of a pixel in C# or in VC++?
if in VC++ we can access the RGB values more faster than this so please help me.
and if the above method is the fastest way and no other faster method exists than this in c# or in VC++ so please comment as well.
thanks
|
|
|
|
|
|
|
i am sorry actually i am new to the codeproject. but now i got how to ask a question.
thanks a lot
|
|
|
|
|
inayathussaintoori wrote: i am sorry actually i am new to the codeproject. but now i got how to ask a question.
thanks a lot
No problem, we all need to start somewhere
|
|
|
|
|
Hi,
Please tell me how can i replace the default list box of a combo box with a custom made one. I have made a subclassed listbox. I want to show this listbox when user clicks on the down arrow of combo box.
Thanks in advance,
John.
|
|
|
|
|
|