|
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.
|
|
|
|
|
|
Hello
I want to program a phone book using two dimension pointers. Please help me with a simple example of string two dimensional pointer.
Thanks in advance
|
|
|
|
|
|
My problem is How to start interrupted detection process again.
thanks in advance
|
|
|
|
|
What is an "interrupted detection process"?
|
|
|
|
|
|
This is the C++/CLI forum meant for managed and mixed-mode topics.
Your thread should ideally have been here:
http://www.codeproject.com/Forums/1647/C-Cplusplus-MFC.aspx[^]
That said, if you look at his code, specially the GetSession and DownloadFile methods, you'll see that he downloads the file using InternetOpenUrl and InternetReadFile .
|
|
|
|
|
sorry, I'll just move my question then. thanks for the guide.
|
|
|
|
|
I am working from a book that has me make a simple Paint program on an MFC SDI. It uses a metafile to store the GDI. I can save the metafile to disk, open the metafile from disk, but the book does not show me how to print the metafile to a printer. If I try to preview or print the document, I get a blank document.
I am saving the metafile this way...
void CPainterView::OnFileSave()
{
CPainterDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
HMETAFILE MetaFileHandle = pDoc->pMetaFileDC->Close();
CopyMetaFile(MetaFileHandle, "painter.wmf");
CMetaFileDC* ReplacementMetaFile = new CMetaFileDC();
ReplacementMetaFile->Create();
ReplacementMetaFile->PlayMetaFile(MetaFileHandle);
DeleteMetaFile(MetaFileHandle);
delete pDoc->pMetaFileDC;
pDoc->pMetaFileDC = ReplacementMetaFile;
}
I showed you how the book saves the metafile because I have a feeling printing will be very similar.
With the MFC SDI, I have the print features...
BOOL CPainterView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CPainterView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CPainterView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
Can I use any of these print features to print my metafile? Thank you
modified on Thursday, October 21, 2010 1:36 AM
|
|
|
|
|