|
The link that Victor gave you shows how to get a pointer to a managed object in order to copy data into it. So all you need is two such pointers in order to copy managed to managed.
|
|
|
|
|
Thank you very much Richard for elaborating a bit on this! I have solved my problem !
|
|
|
|
|
//Assuming int is on 4 bytes
typedef union u
{
int i;
byte b[4];
} U;
U x;
x.b[0] = 0 // Put first byte here
x.b[1] = 0 // second byte here, etc.
// Then use x.i as an integer
// Keep in mind that on Intel CPUs, integer values are represented in memory in reverse order.
// This means that hex value 0x11223344 will be represented in memory as 0x44, 0x33, 0x22, 0x11
// This is called little-endian vs. big-endian representation.
// There can be CPUs that uses the other convention.
// Make a small test project and play around with some values etc.
|
|
|
|
|
Hello all, my code requires the Serial Port to be static. Each time I make changes to the UI, I have to manually set the Serial Port to static.
like this
private: System::IO::Ports::SerialPort^ mySerialPort;
to this
private: static System::IO::Ports::SerialPort^ mySerialPort;
Is there a way to automatically be static?
|
|
|
|
|
Add it to your class manually rather than in the designer.
|
|
|
|
|
Thanks I have been adding the static keyword manually. But do you know if there is a way to automatically do this?
|
|
|
|
|
I have never tried, but I would not expect so. If you drag a control onto a form in the designer then the designer adds it to the Form's properties. But there is nothing (at least in C# Forms) that I can see that allows you to modify it to be static.
|
|
|
|
|
Hello,
I am trying to display float/double/integer to a string with custom format in Label. But I couldn't find snprintf available in managed c++. How can I achieve this?
|
|
|
|
|
Did you try ToString method?
int count = 100;
System::String^ s = count.ToString();
|
|
|
|
|
Hello, I have tried ToString(), but I could not format to the how many decimal places to display .
|
|
|
|
|
|
I am trying to set the image of a button when my Serial Port receives data.
After I receive the data, I use an Invoke to call MyForm::ProcessReceived, then from the MyForm::ProcessReceived to setAppearance. My setAppearance function is called, however the image on button is still showing the default image.
How do I show the new image in button?
System::Void MyForm::SerialPort_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e)
{
uint16_t test = 0;
test = mySerialPort->BytesToRead;
this->Invoke(gcnew EventHandler(this, &MyForm::ProcessReceived));
}
System::Void MyForm::ProcessReceived(System::Object^ sender, System::EventArgs^ e) {
my_ReceivedData^ status = gcnew my_ReceivedData;
mySerialPort->Read(status->bytes, 0, length);
mySerialPort->DiscardInBuffer();
setAppearance(status->bytes);
}
public: System::Void setAppearance(array<unsigned char>^ bytes) {
btn_Start->ImageIndex = 3;
}
Update (Problem solved):
It turned out that the buttons were disabled so the background images couldnt be updated. After enabling the buttons, everything worked accordingly!
modified 26-May-21 1:11am.
|
|
|
|
|
Caveat: I'm not a C++ programmer so any syntax below may be incorrect.
The objects, properties and method signatures look like Windows Forms in .Net. If so, try ...
public: System::Void setAppearance(array<unsigned char>^ bytes) {
btn_Start->ImageIndex = 3;
btn_Start->Refresh();
}
|
|
|
|
|
Hello, I seen that this Refresh() should force the button to redraw itself, and I tested but it still didn't work. I then guessed that the button was disabled and I was right, because the button is disabled it couldn't show the new background image.
Thanks for your help!
|
|
|
|
|
Did you try enabling, refreshing, then disabling so that the refresh happens whilst it is enabled but the chances of someone clicking it in the brief time it is enabled is slim? If you wanted to eliminate its clickablity whilst it is briefly disabled, you could change the click action to do nothing before enabling it and then restoring the normal click action after disabling it.
Not tried any of this myself, so I am just guessing but it is worth a go. It may be that the disabled visual element overrides the selected background image.
|
|
|
|
|
Hello jsc42, yes I have done this part to prevent the user from ever mistakenly clicking the button . Thanks for the suggestion!
|
|
|
|
|
I am trying to call a function defined in the parent class via the object. But I am getting the error a pointer to member is not valid for a managed class. How can I achieve what I wanted? My expected output is to display the text "Called from child"
MyChild.h
ref class MyChild
{
System:Void(*my_func_ptr)(int, char*);
typedef System:Void(*MyFuncPtrType)(int, char*);
MyFuncPtrType my_func_ptr;
public: MyChild ( System:Void(*some_func)(int, char*)){
my_func_ptr = some_func;
}
public: System::Void Child_ButtonClicked(System::Object^ sender, System::EventArgs^ e){
my_func_ptr();
}
}
MyForm.h
#include "MyChild.h"
public ref class MyForm : public System::Windows::Forms::Form
{
.
.
.
private: static System:Void test(int, char*) {
MessageBox::Show("Called from child");
}
private: System::Void MyForm::MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
MyChild^ child= gcnew MyChild( MyForm::test);
child -> test(1,"random");
}
}
Update 2, Previous problem for //type incompatible error in this line is solved using the following way below.
MyChild.h
ref class MyChild
{
private: System::Void(*my_func_ptr)(int, char*);
public: System::Void doFunction(int A, char* B) {
(*my_func_ptr)(A,B);
}
public: MyChild ( System::Void(*func)(int A, char* B)){
my_func_ptr = func;
}
public: System::Void Child_ButtonClicked(System::Object^ sender, System::EventArgs^ e){
doFunction(1,"random");
}
}
MyForm.h
#include "MyChild.h"
public ref class MyForm : public System::Windows::Forms::Form
{
.
.
.
typedef System::Void (*callback_function)(int, char*);
private: static System:Void test(int, char*) {
MessageBox::Show("Called from child");
}
private: System::Void MyForm::MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
callback_function disc;
disc = (callback_function)(MyGUI::MyForm::test);
MyChild^ child= gcnew MyChild( disc);
child -> doFunction(1,"random");
}
}
For now, everything is solved but I am still unsure to the reason for incompatibility for argument and parameter having same type. I will do more trails and see if this is a stable way of implementation. Thanks for your help Richard Andrew x64 !
modified 25-May-21 0:07am.
|
|
|
|
|
In non-managed C/C++ you cannot use a pointer to a non-static member in that way. Make the test method to static , and it should work.
|
|
|
|
|
Hello, I have made to test() to static and now I have a new error at the same line that doesn't make sense to me.
argument of type "void (*)(int, char*)" is incompatible with parameter of type "void (*)(int, char*)"
Both the types are identical.
I have also tried to change all types to void, and the error message is still the same i.e.
argument of type "void (*)()" is incompatible with parameter of type "void (*)()"
latest update: problem solved. Please see the first post! thanks!
modified 25-May-21 0:06am.
|
|
|
|
|
|
I can't figure out how to convert a native string pointer to an IntPtr.
This is how I'm creating the native string pointer:
PCWSTR pFullUrl = static_cast<PCWSTR>(static_cast<void*>(Marshal::StringToHGlobalUni(S->pFullUrl))); Now, in the destructor, I need to convert the PCWSTR pointer back to an IntPtr so I can call Marshal::FreeHGlobal(pFullUrl).
Does anyone know how to perform the conversion from a native pointer to a managed IntPtr?
NOTE: FreeHGlobal() takes IntPtr not IntPtr^, so I don't want to convert to IntPtr^.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
Thanks Richard, I was just about to delete this post because I just realized that I could do FreeHGlobal(IntPtr(pFullUrl)) as long as pFullUrl is a LONGLONG.
Thank you for your response.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Backgammon Game (Classic Backgammon(known as Erkek Tavlası in Turkish) Programming using Files.
You will create and maintain 2 files, one for Backgammon table (name must be Table.dat)(figure 2), the second table
is for Dice history and play log
Kodu bilen var mı?
|
|
|
|
|
basel33 wrote: You will create and maintain Yes, you will, not anyone else.
basel33 wrote: Kodu bilen var mı? Yok.
|
|
|
|