|
Thanks N a v a n e e t h!
**Edit
Ok, I got it now! Thanks very much!
modified on Tuesday, April 21, 2009 10:06 PM
|
|
|
|
|
I have a project that I'm working on that generates batch files when a button is clicked. I added a close button that then cleans the subdirectory the batch files are generated in. The only problem I'm experiencing now is that when the user closes the application using the builtin x button in the upper right hand corner, it doesn't execute the code since it calls it's own destructor. Is there any way to have that close feature also clean the subdirectory by removing all the *.bat files?
|
|
|
|
|
Hi,
you might put your exit code in the mainform's FormClosed event handler.
|
|
|
|
|
I created this app using Visual C++, sorry to sound really stupid, but where exactly would I find that? I'm more of a console programmer but recently decided to try and tackle GUIs.
|
|
|
|
|
You know how to add a Button.Click handler? or a TextBox.TextChanged handler?
It is exactly the same for Form.FormClosed: in Visual Designer select the Control, right click for Properties, use Lightning icon to switch to events, double-click the event you want, that adds and opens the handler's method.
|
|
|
|
|
Awesome! That was very helpful! Thank you so much!
|
|
|
|
|
You're welcome.
|
|
|
|
|
I would like to copy the data content of pDataByte to QByte using Microsoft Visual C++ 2008 .NET
Debug Info
Name Value Type
pDataByte {System.Array} System::Object^
[0x0] 0x41 ‘A’ unsigned char
[0x1] 0x42 ‘B’ unsigned char
[0x2] 0x43 ‘C’ unsigned char
[0x3] 0x44 ‘D’ unsigned char
QByte {Length=0x4} unsigned char[]
[0x0] 0x00 ‘’ unsigned char
[0x1] 0x00 ‘’ unsigned char
[0x2] 0x00 ‘’ unsigned char
[0x3] 0x00 ‘’ unsigned char
I have tried several approaches, for example:
<br />
System::Object ^pDataByte;<br />
unsigned char QByte[4];<br />
<br />
ReadOutput(pDataByte);<br />
<br />
<br />
QByte[0] = (unsigned char)System::Convert::ToChar(pDataByte); <br />
Error Code (at runtime at line: QByte[0] = (unsigned char)System...)
An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll
Additional information: Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.
Please help, I have tried very hard to get this to work, I am used to plane C++ (not .NET Framework)
|
|
|
|
|
What is the type of the content in pDataByte ? Is that a byte array?
|
|
|
|
|
Yes, unsigned char (8-bit, byte) system array to be more specific, see "Debug Info".
Debug Info
Name Value Type
pDataByte {System.Array} System::Object^
[0x0] 0x41 ‘A’ unsigned char
[0x1] 0x42 ‘B’ unsigned char
[0x2] 0x43 ‘C’ unsigned char
[0x3] 0x44 ‘D’ unsigned char
QByte {Length=0x4} unsigned char[]
[0x0] 0x00 ‘’ unsigned char
[0x1] 0x00 ‘’ unsigned char
[0x2] 0x00 ‘’ unsigned char
[0x3] 0x00 ‘’ unsigned char
|
|
|
|
|
QByte[0] = (unsigned char)System::Convert::ToChar(pDataByte); First you need to get a unsigned char array from the object. Loop through each element and assign it to QByte . Sample code follows,
array<unsigned char>^ uChars = static_cast<array<unsigned char>^>(pDataByte);
for(int i=0; i < uChars->Length; i++)
{
QByte[i] = uChars[i];
} Hope that helps
|
|
|
|
|
Thanks a lot, that´s not enough, thank you, thank you, thank you!
I used your solution together with the solution from Mark Salsbery
array<unsigned char>^ uChars = static_cast<array<unsigned char>^>(pDataByte);
System::Runtime::InteropServices::Marshal::Copy(uChars, 0, System::IntPtr(QByte), 4);
Thank you both extremely much
modified on Monday, April 20, 2009 5:44 AM
|
|
|
|
|
Happy to see it worked
|
|
|
|
|
Of course it worked, three people working as a team should be sufficient to move 4 bytes
|
|
|
|
|
Luc Pattyn wrote: three people working as a team should be sufficient to move 4 bytes
Great! Worth for a new sig.
|
|
|
|
|
Obviously, four people would have been the most efficient team size for this project - one person per byte
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
If they all spend their time reading The Mythical Man-Month four people may not even be enough...
PS: have been reading some Joel-on-Software lately, so I couldn't join in, sorry.
|
|
|
|
|
marcusab wrote: I would like to copy the data content of pDataByte to QByte
Here's two ways:
array<Byte> ^pDataByte = gcnew array<Byte>(4);
pDataByte[0] = 0x41;
pDataByte[1] = 0x42;
pDataByte[2] = 0x43;
pDataByte[3] = 0x44;
unsigned char QByte[4];
pin_ptr<Byte> p = &pDataByte[0];
memcpy(QByte, p, 4);
Marshal::Copy(pDataByte, 0, IntPtr(QByte), 4);
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thank you very very very much , you got me going in the right direction, see my answer to Navaneeth for more info.
Almost, pDataByte is a System::Object^ {System.Array}, not an array<unsigned char=""> {Length = 0x4} as with QByte. See debug info:
Name Value Type
pDataByte {System.Array} System::Object^
[0x0] 0x41 ‘A’ unsigned char
[0x1] 0x42 ‘B’ unsigned char
[0x2] 0x43 ‘C’ unsigned char
[0x3] 0x44 ‘D’ unsigned char
QByte {Length=0x4} unsigned char[]
[0x0] 0x00 ‘’ unsigned char
[0x1] 0x00 ‘’ unsigned char
[0x2] 0x00 ‘’ unsigned char
[0x3] 0x00 ‘’ unsigned char
So I get compilation error:
<br />
error C2665: 'System::Runtime::InteropServices::Marshal::Copy' : none of the 16 overloads could convert all the argument types<br />
|
|
|
|
|
marcusab wrote: pDataByte is a System::Object^ {System.Array}, not an array
An array IS a System.Array, but I missed your declaration - sorry about that!
You need the cast
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
By the way, how do I create the variable pDataByte (now I recevie it from an external DLL).
I would like to do someting like this:
System::Object ^pDataByte = gcnew System::Object(System::Array<<System::Byte>>(4));
To get this:
Name Value Type
pDataByte {System.Array} System::Object^
[0x0] 0x00 ‘’ unsigned char
[0x1] 0x00 ‘’ unsigned char
[0x2] 0x00 '’ unsigned char
[0x3] 0x00 ‘’ unsigned char
|
|
|
|
|
Never mind ...
System::Object ^pDataByte = gcnew array<System::Byte>(4);
Again, thank you very much
|
|
|
|
|
Why not use the strong type instead of a System::Object^ ?
array<Byte> ^pDataByte = gcnew array<Byte>(4);
If you must use System::Object^...
System::Object ^pDataByte = gcnew array<Byte>(4);
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks again, you are the man
|
|
|
|
|
Hi, I can't find any decent example on how to create a DialogBox using C++/CLI and on how to deal with the different responses triggered by the diffent buttons being pressed.
I've created graphically the DialogBox (form2) which has 2 buttons "A" and "B" but I can't understand how I can pass different values to the main form (form1) when each button is selected. Is it possible to do it without creating a new class. How?
Any example code would be highly appreciated.
|
|
|
|