joe_pool_is Posted April 12, 2007 Posted April 12, 2007 (edited) I'm trying to write a C++ app using a VB example. It uses the Delegate keyword, but I'm not sure exactly what it is or what the equivalent would be in C++. Here is the code:Application.DoEvents() While True If retCode = 0 Then '---either error or stop is requested Exit While Else Dim updateDelegate As New _ myDelegate(AddressOf updateMessageLog) updateDelegate.Invoke(byteArrayToString(inbuff)) ReDim inbuff(300) retCode = ReadFile(infileHandler, _ inbuff, _ inbuff.Length, _ numReadWrite, _ 0) Application.DoEvents() End If End WhileThe Delegate is defined as follows:Public Delegate Sub myDelegate(ByVal str As String) Public Sub updateMessageLog(ByVal str As String) If str.Length > 0 Then txtMessageLog.Text += str End If End SubWhile I am asking, does anyone know what Application.DoEvents() and ReDim are and what their equivalents would in C/C++? I would guess Sleep(1) and memcpy, but I would like a confirmation if someone knows this. Edited April 12, 2007 by joe_pool_is Quote Avoid Sears Home Improvement
MrPaul Posted April 12, 2007 Posted April 12, 2007 (edited) Managed or native? Are you trying to port this to managed C++ or native C++? Converting VB.Net to managed C++ is much easier as it is all just a matter of syntax, whereas converting to native C++ requires changing basically everything. For example, this is how the code might look in managed C++: (untested, don't take as gospel) Application::DoEvents(); while (retCode) { myDelegate^ updateDelegate = gcnew myDelegate(this, MyClass::updateMessageLog); updateDelegate.Invoke(byteArrayToString(inbuff)); inbuff = gcnew int[299]; //Or whatever type inbuff is retCode = ReadFile(infileHandler, inbuff, inbuff.Length, numReadWrite, 0); Application::DoEvents(); } Converting to native C++ is a whole other kettle of fish. For example, Application.DoEvents() alone would have to be written thus: (again, untested) void DoEvents() { MSG msg; ZeroMemory(&msg, sizeof(msg)); while (PeekMessage(&msg, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } } As you can see, native C++ is nothing like managed C++. If you're coding in native C++ then I don't think you should be reading VB.Net examples as the two are very different beasts. Good luck :cool: Edited April 12, 2007 by MrPaul Quote Never trouble another for what you can do for yourself.
joe_pool_is Posted April 12, 2007 Author Posted April 12, 2007 Hi again Mr. Paul, My apologies. I should have stated that I'm using native C++. I'll try my hand at fabricating a DoEvents procedure similar to what you have pointed out. Thanks for that, by the way! I did not think it was an important step to consider! I was going to code a Sleep(), and consider it done. I didn't realize it would be so important. Back to Delegate, though: Is this a CALLBACK function, or something similar? Quote Avoid Sears Home Improvement
Administrators PlausiblyDamp Posted April 12, 2007 Administrators Posted April 12, 2007 Delegates (at their simplest anyway) are really just typesafe function pointers, therefore under C / C++ there isn't a direct replacement for the delegate keyword as you can just use a normal function pointer. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
MrPaul Posted April 13, 2007 Posted April 13, 2007 DoEvents, delegates and ReDim Application.DoEvents() Thanks for that, by the way! I did not think it was an important step to consider! I was going to code a Sleep(), and consider it done. I didn't realize it would be so important. The description of Application.DoEvents() reads: [horizontalrule]-[/horizontalrule]Processes all Windows messages currently in the message queue. ... Unlike Visual Basic 6.0, the DoEvents method does not call the Thread.Sleep method. [horizontalrule]-[/horizontalrule] Of course in your own implementation you may add a call to Sleep() as you wish. Delegates As PlausiblyDamp said, delegates are basically type-safe function pointers. However, they are often tied to a specific class instance. C++ does not have this functionality natively, so you'd have to design your own delegate system to reproduce it. For most purposes I expect that regular function pointers would suffice, but it all depends on how closely you are trying to copy the VB code - as I said before, they are very different languages. ReDim ReDim resizes an array. It does not preserve the contents of the array unless the Preserve keyword is specified. In the VB code it looks like ReDim is being (mis)used merely to erase the contents of the array. In general you would probably reproduce ReDim using the new operator, although you must remember to use delete at some point to avoid memory leaks. To erase the contents of an array you could use ZeroMemory(). Again, it doesn't translate perfectly without ambiguity. Not wanting to sound like a broken record, but VB.Net is probably one of the poorest choices to translate samples into native C++ from. The differences between the two languages are so vast that all but the most fundamental algorithmic concepts will be lost or require major rewriting during the translation. I would highly recommend searching for samples in any non-managed language or failing that, C#. It will save you considerable time and effort in the long run. Good luck :) Quote Never trouble another for what you can do for yourself.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.