|
thanks for the reply..I will try it today..thanks a lot.
hema.
|
|
|
|
|
Hello people,
I am trying to make an application with in- and output. Input by textboxes and output by writing it to a file.
I succeeded to get text from a TextBox^ and put it in a RichTextBox^. No problem.
I succeeded to use fstream to 'print' data (string) to a file.
Now I want to combine these by using a vector<string>.
The problem I am facing is that if I do: fout << textBox->Text; that I get the following error:
1< d:\stageappvc\terminalapp\terminalapp\Form1.h(185) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'System::String ^' (or there is no acceptable conversion)
1< C:\Program Files\Microsoft Visual Studio 9.0\VC\include\ostream(653): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)
If I want to write the
textBox->Text to my vector. I get the following error:
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(194) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'System::String ^' to 'const std::string &'
I tried to find out what this String^ actually is - but I could not find an accurate answer. I also tried:
textBox->Text.toString() which failed too.
Is anyone able to help me with this? I tried to find examples of how to put TextBox data in a string, but I couldn't find anything useful...
Kind regards,
Mikki Weesenaar
|
|
|
|
|
Don't use "System::String" instead use "std::string"
|
|
|
|
|
I have:
std::vector<string> textCode;
private: System::Windows::Forms::RichTextBox^ textBox;
private: System::Windows::Forms::TextBox^ invoerVeld;
textBox->Text += invoerVeld->Text + "\r\n"; < works fine
textCode.push_back(textBox->Text); < no-go
textCode.push_back(invoerVeld->Text);
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(194) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'System::String ^' to 'const std::string &'
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(195) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'System::String ^' to 'const std::string &'
So I think I cannot just use string in stead of String. Its a native thing of the TextBox and RichTextBox. I guess... Or if Im wrong, correct me 
|
|
|
|
|
I got this from MSDN -
String^ c = gcnew String("Change Me");
std::string str1 = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
std::wstring str2 = (const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
|
|
|
|
|
I dont see the relation between the first line and the other two.
And I need it otherwise.
I have a String^ (coming from textBox->Text) and I want to store this in a string (which I have in a vector).
|
|
|
|
|
The first line creates a String^. I believe you already have that in textBox->Text.
The second line converts that to a string.
The third line converts that to a wstring.
What else do you not see?
|
|
|
|
|
I guess I need the second then.
Currently I have
std::string str1 = (const char*)(Marshel::StringToHGlobalAnsi(textBox->Text)).ToPointer();
But it gives me the following errors:
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C2653: 'Marshel' : is not a class or namespace name
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C2228: left of '.ToPointer' must have class/struct/union
1> type is ''unknown-type''
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C3861: 'StringToHGlobalAnsi': identifier not found
So I tried to #include <marshal>, but that didn't work either.
How can I use this function?
And by the way - thanks for your fast replies 
|
|
|
|
|
Typo
It should be Marshal .
You got it wrong there.
Look at my posting.
|
|
|
|
|
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(7) : fatal error C1083: Cannot open include file: 'Marshal': No such file or directory
:p Yeh - I corrected it but copied the wrong stuff I saw...
But with A its still not working
I am using Visual C++ Express Edition. Downloaded the package yesterday :p
|
|
|
|
|
I suspect some mix-up in your code.
You do not have to include any file called Marshal .
And you can use this class only in managed applications.
|
|
|
|
|

#pragma once
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
namespace TerminalApp {
using namespace std;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
std::vector<string> textCode;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ exitButton;
private: System::Windows::Forms::Button^ addText;
private: System::Windows::Forms::RichTextBox^ textBox;
private: System::Windows::Forms::Button^ saveButton;
private: System::Windows::Forms::TextBox^ invoerVeld;
private: System::Windows::Forms::Button^ voegTextButton;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->exitButton = (gcnew System::Windows::Forms::Button());
this->addText = (gcnew System::Windows::Forms::Button());
this->textBox = (gcnew System::Windows::Forms::RichTextBox());
this->saveButton = (gcnew System::Windows::Forms::Button());
this->invoerVeld = (gcnew System::Windows::Forms::TextBox());
this->voegTextButton = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
this->exitButton->Location = System::Drawing::Point(645, 269);
this->exitButton->Name = L"exitButton";
this->exitButton->Size = System::Drawing::Size(85, 35);
this->exitButton->TabIndex = 0;
this->exitButton->Text = L"Exit";
this->exitButton->UseVisualStyleBackColor = true;
this->exitButton->Click += gcnew System::EventHandler(this, &Form1::exitApp);
this->addText->Location = System::Drawing::Point(16, 24);
this->addText->Name = L"addText";
this->addText->Size = System::Drawing::Size(85, 35);
this->addText->TabIndex = 1;
this->addText->Text = L"AddText";
this->addText->UseVisualStyleBackColor = true;
this->addText->Click += gcnew System::EventHandler(this, &Form1::addText_Click);
this->textBox->Location = System::Drawing::Point(337, 24);
this->textBox->Name = L"textBox";
this->textBox->ReadOnly = true;
this->textBox->ScrollBars = System::Windows::Forms::RichTextBoxScrollBars::None;
this->textBox->Size = System::Drawing::Size(393, 239);
this->textBox->TabIndex = 2;
this->textBox->Text = L"";
this->saveButton->Location = System::Drawing::Point(554, 269);
this->saveButton->Name = L"saveButton";
this->saveButton->Size = System::Drawing::Size(85, 35);
this->saveButton->TabIndex = 3;
this->saveButton->Text = L"Save";
this->saveButton->UseVisualStyleBackColor = true;
this->saveButton->Click += gcnew System::EventHandler(this, &Form1::saveButton_Click);
this->invoerVeld->Location = System::Drawing::Point(16, 269);
this->invoerVeld->Name = L"invoerVeld";
this->invoerVeld->Size = System::Drawing::Size(130, 20);
this->invoerVeld->TabIndex = 4;
this->voegTextButton->Location = System::Drawing::Point(178, 269);
this->voegTextButton->Name = L"voegTextButton";
this->voegTextButton->Size = System::Drawing::Size(70, 34);
this->voegTextButton->TabIndex = 5;
this->voegTextButton->Text = L"VoegToe";
this->voegTextButton->UseVisualStyleBackColor = true;
this->voegTextButton->Click += gcnew System::EventHandler(this, &Form1::voegTextButton_Click);
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(742, 316);
this->Controls->Add(this->voegTextButton);
this->Controls->Add(this->invoerVeld);
this->Controls->Add(this->saveButton);
this->Controls->Add(this->textBox);
this->Controls->Add(this->addText);
this->Controls->Add(this->exitButton);
this->MaximizeBox = false;
this->MaximumSize = System::Drawing::Size(750, 350);
this->MinimumSize = System::Drawing::Size(750, 350);
this->Name = L"Form1";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L"Applicatie";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void exitApp(System::Object^ sender, System::EventArgs^ e) {
Application::Exit();
}
private: System::Void addText_Click(System::Object^ sender, System::EventArgs^ e) {
String^ tmp = textBox->Text;
textBox->Text = "\r\n" + tmp + "Blah";
}
private: System::Void saveButton_Click(System::Object^ sender, System::EventArgs^ e) {
fstream fout("c:/ostream.txt", std::ios_base::app);
textCode.push_back("1");
textCode.push_back("2");
textCode.push_back("3");
textCode.push_back("4");
textCode.push_back("5");
fout.close();
}
private: System::Void writeToTextbox(void) {
}
private: System::Void voegTextButton_Click(System::Object^ sender, System::EventArgs^ e) {
textBox->Text += invoerVeld->Text + "\r\n";
invoerVeld->Text = "";
invoerVeld->Focus();
writeToTextbox();
std::string str1 = (const char*)(Marshel::StringToHGlobalAnsi(textBox->Text)).ToPointer();
}
};
}
That's the code I have at the moment. Im trying to make a Forms Application - Im quite new to Forms Applications and only have made console apps before... :p
|
|
|
|
|
Try adding this line -
using namespace System::Runtime::InteropServices;
|
|
|
|
|
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C2653: 'Marshel' : is not a class or namespace name
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C2228: left of '.ToPointer' must have class/struct/union
1> type is ''unknown-type''
1>d:\stageappvc\terminalapp\terminalapp\Form1.h(200) : error C3861: 'StringToHGlobalAnsi': identifier not found
Is there any other known way to get text out such TextBox into a string? One made by Microsoft themselves? :p
|
|
|
|
|
You have typed in Marshel .
The correct spelling is Marshal .
Do you see the difference?
|
|
|
|
|
How strange - because I copied it directly from your post - since first of all I hate typing and secondly its always better to copy paste :p
But it is working now - let me test the output etc.
Thanks in advance <3
|
|
|
|
|
But I really think you should not be driving now.
|
|
|
|
|
No Im not driving lol.
It works!
*cheering the hell outta it*
Adding text to the TextBox and with a button its sending the data to my vector string which can be read and send to the output
Thank you very very much! I really needed this line!
<33 ![Rose | [Rose]](https://codeproject.global.ssl.fastly.net/script/Forums/Images/rose.gif)
|
|
|
|
|
Hi there,
I got the following problem. For an application that I have already up and running I wrote something like an observer program. The observer simply checks from time to time if the application is still alive and responding. If that is, for whatever reason, no the case, the observer kills the application process (TerminateProcess()) and starts another instance.
Now that works quite well so far. Only when a windows error report or such something pops up in the trail of an application crash, the observer is unable to kill the application. So the whole mechanism breaks down.
My question is, is there a possibility to kill an application nonetheless? Or to find out what is blocking the killing and to kill that one first then?
I could get all the possible process names of the processes that might show up, but that will always leave a hole in the system.
Any ideas on that?
Souldrift
|
|
|
|
|
Souldrift wrote: My question is, is there a possibility to kill an application nonetheless?
I would have thought TerminateProcess() to be the right choice as it is used to unconditionally cause a process to exit.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
First and foremost: never use TerminateProcess to terminate your application, there should be a better method.
If you nonetheless want, at extreme stage, to kill unconditionally the application then use TerminateProcess (rules are there to be broken... ): as suggested by David, there's no worst hammer than such a function (AFAIK).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Check the return value of TerminateProcess() and get the error code, if any, using GetLastError() .
Is the application and observer running in different user contexts?
If so GetLastError should return Access Denied(5) .
|
|
|
|
|
Hey, I'm using Windows Hook, I installed the mouse hook, system-wide and its working perfectly. Now there is a problem, I need to the get window handle on which the mouse was clicked.. How do I do that? Does the Mouse hook event passes us that information?
|
|
|
|
|
Are you using WH_MOUSE ?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Well, I was using WH_MOUSE_LL earlier. I changed my code to WH_MOUSE.
Now the code is:
HWND hWnd = NULL;
HWND hDesktopListWnd = NULL;
hWnd = FindWindow(_T("ProgMan"), NULL);
hWnd = GetWindow(hWnd, GW_CHILD);
hDesktopListWnd = GetWindow(hWnd, GW_CHILD);
threadID = GetWindowThreadProcessId(hDesktopListWnd, NULL);
hookMouse = SetWindowsHookEx(WH_MOUSE, (HOOKPROC)InternalMouseHookCallback, g_appInstance, threadID);
It installs the hook successfully, but I don't get the mouse hook events now. Basically I want to get click events when any icon on the desktop is clicked.
|
|
|
|