|
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.
|
|
|
|
|
Hello everyone,
I made a class that will do some additional configuration to the serial port. (Mainly to handle received data).
My issue is that SerialPort_DataReceived() is successfully triggered when it received data from device, however the BytesToRead is always 0. What could the reason be? I am sure the device is sending data because when I haven't use this ConfigSerialPort, everything works fine.
ConfigSerialPort.h
#pragma once
using namespace System::Windows::Forms;
ref class ConfigSerialPort: public System::Windows::Forms::Control
{
private: System::IO::Ports::SerialPort^ _serialPort;
public:
ConfigSerialPort(System::IO::Ports::SerialPort^ serialPort) {
_serialPort = serialPort;
_serialPort->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &ConfigSerialPort::SerialPort_DataReceived);
}
private: System::Void SerialPort_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e)
{
unsigned int test = 0;
test = _serialPort->BytesToRead;
}
}
MyForm.h
#pragma once
#include <stdlib.h>
#include <string.h>
#include <cstdint>
#include <stdio.h>
#include "ConfigSerialPort.h"
namespace SerialPortGUI {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Runtime::Serialization::Formatters::Binary;
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
}
.
.
.
void InitializeComponent(void)
{
.
.
.
this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
this->MyForm_SerialPort = (gcnew System::IO::Ports::SerialPort(this->components));
.
.
.
}
private: ConfigSerialPort^ test;
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
test = gcnew ConfigSerialPort(MyForm_SerialPort);
}
}
}
|
|
|
|
|
What is the serial port connected to, and how can you tell there is data to be read?
|
|
|
|
|
Hello, my other device is an Arduino that connects to PC as Com port 8. Every time I press a button on Arduino, it will transmit a text over serial to the PC. I have verified the received text using Termite software.
|
|
|
|
|
It's impossible to guess why your receiver thinks there is no data to read, if it is being transmitted.
|
|
|
|
|
The only think I could think of is inside
ConfigSerialPort.h
I should use gcnew for _serialPort
_serialPort = gcnew (System::IO::Ports::SerialPort); , and then assign it the parameter
_serialPort = serialPort
|
|
|
|
|
I made a class myGroupbox to use SerialPort, and I'm trying to update a textbox from the serial port data. But the invoke function couldn't be used, the error message is "myGroupbox has no member invoke"
here's my code
myGroupbox.h
#pragma once
using namespace System::Windows::Forms;
ref class myGroupbox
{
private: System::IO::Ports::SerialPort^ _serialPort;
public:
myGroupbox( System::IO::Ports::SerialPort^ serialPort )
{
_serialPort=serialPort;
_serialPort->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &myGroupbox::SerialPort_DataReceived);
}
private: System::Void SerialPort_DataReceived(System::Object^ sender,System::IO::Ports::SerialDataReceivedEventArgs^ e)
{
uint16_t test = 0;
test = _serialPort->BytesToRead;
if (test == 3U) {
this->Invoke(gcnew EventHandler(this, &myGroupbox::ProcessReceived));
}
else
{
_serialPort->DiscardInBuffer();
}
}
private: System::Void ProcessReceived(System::Object^ sender, System::EventArgs^ e)
{
}
}
|
|
|
|
|
Member 15150778 wrote: the invoke function couldn't be used, the error message is "myGroupbox has no member invoke"
Well, you hadn't defined any Invoke() method in your myGroupbox class. Therefore you got this error message.
|
|
|
|
|
Hello, I tried to copy my approach from the MyForm.cpp and it worked properly i.e. invoke wasn't define in MyForm.cpp but I could still call this invoke function since it's from System::Windows::Forms::Controls.
But in my own class myGroupBox, it couldn't do the same.
here's an snippet of MyForm.cpp that used the invoke and worked
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
myGUI ::MyForm form;
Application::Run(% form);
}
namespace myGUI {
System::Void MyForm::serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {
if (serialPort1->IsOpen) {
databufferA = serialPort1->ReadLine();
this->Invoke(gcnew EventHandler(this, &MyForm::DisplayText));
}
}
}
modified 20-May-21 2:41am.
|
|
|
|
|
Yes, because you have still not declared an Invoke method in your class, and your class is not a child of System.Windows.Forms.Control .
|
|
|
|
|
Hello, thank you very much for your help! I am able to compile after following your suggestion. I will test if this method works and update if this is the final solution.
Thanks again!
myGroupbox.h
#pragma once
using namespace System::Windows::Forms;
ref class myGroupbox: public System::Windows::Forms::Control
{
private: System::IO::Ports::SerialPort^ _serialPort;
.
.
.
|
|
|
|
|
Sorry if my question is unclear, but I shall explain a bit more in the following.
Suppose I am making a software to show ONLY 2 employee information (i.e. Bob and Tim), and my implementation method is to create 2 identical group box.
Each of this groupbox will have the same UI elements , textbox, checkbox, label and buttons.
In order to reduce duplicated codes , I want to create a common class for this groupbox.
Will this work?
Would there be memory leaks?
I did some google research and found something about using smart pointers instead of gcnew'ing a pointer, is it relevant to prevent memory leaks?
Thank you very much for reading, i appreciate any constructive help /example code to get this code properly working!
common_employee_groupbox.h
#pragma once
ref class common_employee_groupbox
{
private: System::Windows::Forms::CheckBox^ _cb_isAbsent = gcnew (System::Windows::Forms::CheckBox^ );
private: System::Windows::Forms::CheckBox^ _cb_isOverTime gcnew (System::Windows::Forms::CheckBox^ );
private: System::Windows::Forms::TextBox^ _tb_employeeName gcnew (System::Windows::Forms::TextBox^ );
private: System::Windows::Forms::TextBox^ _tb_employeeSalary gcnew (System::Windows::Forms::TextBox^ );
private: System::Windows::Forms::Button^ _btn_increaseSalary gcnew (System::Windows::Forms::Button^ );
private: System::Windows::Forms::Button^ _btn_decreaseSalary gcnew (System::Windows::Forms::Button^ );
public:
common_employee_groupbox(
System::Windows::Forms::CheckBox^ cb_isAbsent,
System::Windows::Forms::CheckBox^ cb_isOverTime,
System::Windows::Forms::TextBox^ tb_employeeName,
System::Windows::Forms::TextBox^ tb_employeeSalary,
System::Windows::Forms::Button^ btn_increaseSalary,
System::Windows::Forms::Button^ btn_decreaseSalary
) {
_cb_isAbsent = cb_isAbsent;
_cb_isOverTime= cb_isOverTime;
_tb_employeeName= tb_employeeName;
_tb_employeeSalary= tb_employeeSalary;
_btn_increaseSalary= btn_increaseSalary;
_btn_decreaseSalary= btn_decreaseSalary;
_btn_increaseSalary->Click += gcnew System::EventHandler(this,&common_employee_groupbox::increaseSalary_Click);
_btn_decreaseSalary->Click += gcnew System::EventHandler(this,&common_employee_groupbox::decreaseSalary_Click);
}
private: System::Void increaseSalary_Click() {
tb_employeeSalary->Text = "updatedsalary xxxx";
}
private: System::Void decreaseSalary_Click() {
tb_employeeSalary->Text = "updatedsalary xxxx";
}
};
MyForm.h
#include "common_employee_groupbox.h"
namespace MyEmployeeGUI {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Runtime::Serialization::Formatters::Binary;
public ref class MyForm : public System::Windows::Forms::Form
{
private: System::Windows::Forms::CheckBox^ cb_isAbsent_Tim;
private: System::Windows::Forms::CheckBox^ cb_isOverTime_Tim;
private: System::Windows::Forms::TextBox^ tb_employeeName_Tim;
private: System::Windows::Forms::TextBox^ tb_employeeSalary_Tim;
private: System::Windows::Forms::Button^ btn_increaseSalary_Tim;
private: System::Windows::Forms::Button^ btn_decreaseSalary_Tim;
private: System::Windows::Forms::CheckBox^ cb_isAbsent_Bob;
private: System::Windows::Forms::CheckBox^ cb_isOverTime_Bob;
private: System::Windows::Forms::TextBox^ tb_employeeName_Bob;
private: System::Windows::Forms::TextBox^ tb_employeeSalary_Bob;
private: System::Windows::Forms::Button^ btn_increaseSalary_Bob;
private: System::Windows::Forms::Button^ btn_decreaseSalary_Bob;
public:
MyForm(void)
{
InitializeComponent();
}
.
.
.
System::Void MyForm::MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
common_employee_groupbox^ tim_groupbox = gcnew common_employee_groupbox(
cb_isAbsent_Tim,
cb_isOverTime_Tim,
tb_employeeName_Tim,
tb_employeeSalary_Tim,
btn_increaseSalary_Tim,
btn_decreaseSalary_Tim
);
common_employee_groupbox^ bob_groupbox = gcnew common_employee_groupbox(
cb_isAbsent_Bob,
cb_isOverTime_Bob,
tb_employeeName_Bob,
tb_employeeSalary_Bob,
btn_increaseSalary_Bob,
btn_decreaseSalary_Bob
);
}
}
}
Update 1:
I have tried this code, and I'm having errors with this line (the bolded ampersand is highlighted as error) :
_btn_increaseSalary->Click += gcnew System::EventHandler(this,&common_employee_groupbox::increaseSalary_Click);
the error message:
invalid delegate initializer - function does not match the delegate type
How to fix this problem? Already tried googling, but the results aren't helpful
Update 2:
I have fixed the problem from update 1.
it turned out that I need to declare the increase/decrease salary function with the following arguments like below
private: System::Void increaseSalary_Click(System::Object^ sender, System::EventArgs^ e).
I have gotten the code to work as expected, however I'm still unsure if there will be any memory leaks.
Thanks all!
modified 19-May-21 22:45pm.
|
|
|
|
|
I have an pointer to an array of System::Byte pointers, and im trying to use the SerialPort.Write function that only accepts
array<unsigned char>^
It is necessary that I used System::Byte pointers because the value of byte array needs to be update from time to time during runtime.
heres my sample code
System::Void MyForm::button1_Click(System::Object^ sender, System::EventArgs^ e)
{
array<System::Byte^>^ myArray = gcnew array<System::Byte^>(10U);
myArray[0] = (System::Byte) 0x01;
myArray[1] = (System::Byte) 0x02;
myArray[2] = (System::Byte) 0x03;
myArray[3] = (System::Byte) 0x04;
myArray[4] = (System::Byte) 0x05;
myArray[5] = (System::Byte) 0x06;
myArray[6] = (System::Byte) 0x07;
myArray[7] = (System::Byte) 0x08;
myArray[8] = (System::Byte) 0x09;
myArray[9] = (System::Byte) 0x10;
SerialPort1->Write(myArray,0,10U);
object type is: System::IO::Ports::SerialPort ^
}
modified 13-May-21 0:27am.
|
|
|
|
|
|
I fixed my problem already , Updated from array<system::byte^>^
to array<system::byte>^.
|
|
|
|
|
I'm trying to build a really simple class with member variables and member functions for learning.
I am trying to achieve the following code inside btn_User1_Click, however since user1 is not globally declared, it couldn't find the variable. At the same time, if I tried to declare this
myUser^ user1 = gcnew user1 (1002); outside the function , it gives an error syntax error missing ';' before '^'
myUser.h
#pragma once
ref class myUser
{
public:
myUser(unsigned int user_id) :id(user_id) {
...
}
private:
unsigned int id{ 0 };
public:
System::Void increment_UserID() {
id = id + 1;
}
};
MyForm.cpp
namespace FACEDControllerGUI {
System::Void MyForm::MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
myUser^ user1 = gcnew user1 (1002);
myUser^ user2 = gcnew user1 (1005);
}
System::Void MyForm::btn_User1_Click(System::Object^ sender, System::EventArgs^ e) {
user1->increment_UserID();
}
System::Void MyForm::btn_User2_Click(System::Object^ sender, System::EventArgs^ e) {
user2->increment_UserID();
}
}
|
|
|
|
|
You are creating user1 and user2 inside the MyForm_Load method, so as soon as that method ends the two variables disappear. You should create them in the class definition of MyForm so they are available to all methods of the class.
|
|
|
|
|
I have mentioned this following:
At the same time, if I tried to declare this
myUser^ user1 = gcnew user1 (1002); outside the function , it gives an error syntax error missing ';' before '^'
Fortunately, I have solved my problem. It is due to #include function of the class is placed in the .cpp file, instead of the .h file
|
|
|
|
|
1. What is friend function and friend class? Class "stu-info" stores all the details related to students like (name, reg, section , marks of 5 subjects). Class "show_details" will act like a friend of class "stu-info" and perform following operations: basic_details() and result(). These functions will work on the private data members of "stu-info".
|
|
|
|
|
Is that a question or a statement?
|
|
|
|
|