|
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
|
|
|
|
|
|
Hi,
I am using Visual studio 2005 to build a small Win32 .exe. This exe is doing a very small registry operation only. So i do not want a UI for this. I tried to do a win 32 console application. but while launching that the command console pops up for a moment till the execution completes.
How to build the exe where the console will not pop up?
Thanks in advance.
Birajendu
SonicWALL
Bangalore
India
|
|
|
|
|
I am not sure how you can do that, if it's a console application then it needs to execute in a console Window.
By the way this is not a Managed C++/CLI question; please use the correct forum for your queries.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
Richard MacCutchan wrote: By the way this is not a Managed C++/CLI question; please use the correct forum for your queries.
Thank you, take a 5!
|
|
|
|
|
Thanks, I do think this has been getting worse recently, which is quite worrying when you consider what the levels of ability, initiative, etc. of people on these forums should be.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|